prev | toc | next
 

3.2 Overview: MUF (cont'd)

The Parts of a MUF Program:

1 ( tinker.muf. A practice program )
2
3 : main
4     me @ "Hello, world!" notify
5 ;

Line 1 is a comment. In MUF, anything in parentheses is a comment. The compiler ignores it, and it doesn't add to the compiled size of your program. Comments are used to tell people what the program does, and they are important. Tinker.muf is a very simple program at this point, but you should go ahead and get into the habit of including comments. They explain what's going on to people who are trying to maintain your code, including you. Code that makes perfect sense to you when you write it will look like gobbledygook in a couple days (here's an example). You'll thank yourself for including frequent, detailed comments. The comment here is a header comment. In a full-blown program, we would put notes here about who wrote the program, when, how to install it, how to use it, and permissions for porting the program to other MUCKs.

Lines 3-5 compose a `word' or `function', a block of code that is executed as one piece. Functions are the building blocks of your program. You make a MUF program by making different functions that do different things, and calling the appropriate function when needed (more on this later). This function is called `main', and it's our only function so far. A function begins with a colon, a space, and the name of the function (the space between the colon and the name is essential). It ends with a semi-colon. Unlike C/C++, there's nothing special about the function name `main' in MUF. We could call the function anything we want. A MUF program begins with the last function in the program, no matter what it's called (when we add more functions, they'll need to go above main). In this case, main is the only function, so it's the last function, so the program begins execution there.

Line 4 is the heart of our program, and it includes four different kinds of MUF stuff.

me is a variable. It defines a space in the computer's memory that holds some information... holds a value. In this case, it's a predefined variable (MUF has already staked out this variable name and declared what it's going to hold). The variable me holds the dbref of whoever's using the program. When you trigger tinker.muf by typing `test', me holds your dbref. If someone else typed `test', it would hold his. You can also add your own variables.

@ is an operator. Operators do stuff to (operate upon) other things. + and - are some other operators. They add and subtract, and they only work on numbers. @ is the `fetch' operator, and it only works on variables. The @ fetches the value out the variable named right before it, and puts the information that was in the variable onto the top of the stack (we'll cover the stack next). Here, the @ is getting a dbref out of the variable me.

"Hello, world!" is a string. A string is a set of characters. Here the characters are the letters that form the words - Hello, world -. The quotation marks are important. They tell MUF that this is a string. Anything inside double quote marks is treated as a string, a set of characters. The word - me - by itself is a variable, but "me" is a string. 4 by itself is a number, an integer, but "4" is a string holding one character, the numeral 4. #123 is a dbref, but "#123" is a string of the corresponding characters. These differences — variable vs. string vs. integer vs. dbref — are called `data types', and they're important in MUF. Most operations can only be performed on one type of data. We have an example of that coming up...

NOTIFY is a primitive, a function that has been defined in MUF. NOTIFY needs two things to do its job, a dbref and a string. It takes the string and displays it to the character defined by the dbref (much like a {tell} in MPI). If it doesn't have a dbref and a string on top of the stack to work with, the program won't work.

Incidently, the collective term for all these different kinds of MUF stuff (variables, integers, strings, and dbrefs... operators and primitives... function names and the symbols that mark their beginning and end) is `statement'. NOTIFY, @, 123, : , and main are all statements.

prev | toc | top | next