home *** CD-ROM | disk | FTP | other *** search
- ~
- ~ Here is a program in Mouse which prints out the numbers from 0
- ~ to 99. It skips to a new line each time it has printed five
- ~ numbers.
- ~
- ~
- ~ ~ NUMBERS.MSE
- ~ "!! Numbers, Numbers, Numbers ... !!!"
- ~ 0 n:
- ~ (
- ~ 100 n. - ^
- ~ n. ! 9 !'
- ~ 1 n. 5 \ 4 - +
- ~ [ "!" ]
- ~ n. 1 + n:
- ~ )
- ~ $
- ~
- ~ The ~ introduces a comment in Mouse. The "print character string"
- ~ command is " . Imbedding a ! in a quoted string is done to produce
- ~ carriage returns, ie. blank lines.
- ~
- ~ The program sets n to 0. Mouse is a "stack" oriented language. When
- ~ the Mouse interpreter sees
- ~
- ~ 0 n :
- ~
- ~ the following takes place. 0 is placed on the stack, then n's address,
- ~ which happens to be 14. Then the colon causes the 0 to be stored in
- ~ location 14, ie. at n . The 0 and the 14 are "popped" off the stack.
- ~
- ~ The ( introduces a loop. The expression to the left of a ^ must be
- ~ positive for the loop to continue.
- ~
- ~ ( 100 n . - ^ etc. )
- ~
- ~ is equivalent to
- ~
- ~ while n < 100 do; or perhaps more closely,
- ~ while 100 - n > 0 do;
- ~
- ~ The . is the "dereferencing" command of Mouse, causing the top of the
- ~ stack to be replaced by n's value.
- ~
- ~ n . ! 9 !'
- ~
- ~ prints the value of n and then tabs over 8 spaces. The 9 is the ASCII
- ~ value for TAB and the two character command !' means print the ASCII
- ~ character on the stack, and then "pop" it off the stack.
- ~
- ~ The logic to skip to a new line every five numbers is
- ~
- ~ 1 n . 5 \ 4 - + ["!"]
- ~
- ~ Here's how the code works (it took me a few tries to get this routine to
- ~ work).
- ~
- ~ Place 1 on the stack, then the address of n, then replace the top of
- ~ the stack with this number's value modulo 5. (In our case, n starts at
- ~ 0). Next, take away 4, add this to the 1 and, if ([...]) this value
- ~ is positive, issue a carriage return, line feed ie. generate a blank
- ~ line. If you trace this as n increases due to the code
- ~
- ~ n . 1 + n :
- ~
- ~ you will see that the new lines occur at just the right moment, ie.
- ~ every five numbers!
- ~
- ~ The $ signals the end of a Mouse program.
- ~
- ~ All "macros", if any, (there are none in this example) would follow
- ~ the $. If for example we wanted to ask for the upper limit to print
- ~ to (instead of hard-coding the 100), and print, and loop until an
- ~ upper limit of 0 is entered, we'd turn the code explained above into
- ~ a "macro" (subroutine) and pass it the entered value. Here's the
- ~ code.
- ~
-
- (
- "!Upper limit ? "
- ? u :
- u. ^
- "!!"
- #N,u;
- )
-
- $N
-
- 0 n :
- (
- 1%. n. - ^
- n. ! 9 !'
- 1 n. 5 \ 4 - +
- ["!"]
- n. 1 + n :
- )
- "!"
-
- @ ~ end of macro
-
- ~
- ~ The ? u : is the "input and assign to u" code.
- ~
- ~ #N,u;
- ~
- ~ calls the macro N, passing it the address of the entered value.
- ~ If you responded with a 6 then 17 then 0, this is what would get
- ~ printed:
- ~
- ~ Upper limit? 6
- ~
- ~ 0 1 2 3 4
- ~ 5
- ~
- ~ Upper limit? 17
- ~
- ~ 0 1 2 3 4
- ~ 5 6 7 8 9
- ~ 10 11 12 13 14
- ~ 15 16
- ~
- ~ Upper limit? 0
- ~
- ~ This ends (a rather exhaustive I'm afraid) explanation of the Numbers
- ~ program. Most of the key concepts in Mouse programming are illustrated
- ~ by this program.
- ~
- ~ To interrupt a running program, you may use CTRL-C. Mouse has a
- ~ trace feature which is invaluable during debug. It is turned on by
- ~ including a { in the code and turned off with a } . When on, the
- ~ 80 characters surrounding the character about to be executed are
- ~ displayed. Hitting (SPACE BAR) single steps you thru the code. An
- ~ arrow always points to the code about to be executed. If you hit any
- ~ character other than the (SPACE BAR) during debug, the trace will be
- ~ turned off and your code will run at full speed (or go haywire).
- ~
- ~ The first thing that happens during the interpretation process is this:
- ~ the source code of your Mouse program gets loaded. The "macros", if any,
- ~ are "compiled". If you are interested in the details, look over the
- ~ source code of the Mouse interpreter.
-
- ~ The interpreter then carries out the instructions, branching to the
- ~ appropriate code routine depending on the character encountered.
- ~
- ~ Blanks are not important in Mouse code. NB however: there must be
- ~ no blank between the $ and the macro letter. Control codes
- ~ (ie. codes less than 32 ASCII) are ignored as well. The interpreter
- ~ executes Mouse programs very quickly. Nesting "macros" and
- ~ recursive code will slow things down.
- ~
- ~ The Numbers programs discussed above was written to be as readable as
- ~ possible. It could have all been written on two to three lines. The
- ~ interpreter could care less about the carriage returns, line feeds and
- ~ blanks. I tend to put loop delimiters on lines by themselves (LISP
- ~ style). Use what you think makes your code readable.
- ~
- nks. I tend to put loop delimiters on lines by themselves (LISP
- ~ style). Use