July 1979. BYTE Magazine of that month had an article by Peter Grogono entitled "MOUSE: A Language for Microcomputers."
MOUSE is a stack based language like Forth but very terse (!) almost a byte code. It is lightweight; it can be implmented in under 200 lines of C. And can be quite powerful; in under 2000 lines of C.
When the mouse interpreter encounters a number, it pushes that number on the stack. In early versions of Mouse, the number must be an integer. In Mouse 2002, all variables are floating point, and the underscore operator _ changes the sign of the number on the top of the stack. This makes it easier to enter negative numbers. For example, -4 may be entered as 4_ instead of 0 4 -. If it is necessary to do integer division, this can be done using / &INT.
There are 26 predefined variables in mouse (A - Z). When the interpreter encounters a variable, it pushes its corresponding number (0-25) on the stack. Mouse-2002 includes global variables: Within a macro, lowercase variables a-z are local to the macro; uppercase variables A-Z are global. Within the main program, A-Z and a-z are the same.
You can replace the number of a variable on top of the stack with the variables value by using the period (.) operator.
To print the number on top of the stack use the bang (!) operator. This also has the effect of removing the number from the stack
To write a MOUSE program you must create a text file that contains a series of MOUSE commands. You can then run the program by using the command MI filename. Hello World in mouse is:
"HELLO, WORLD.!" $$
Later versions added comments to the MOUSE language. Anything on a line following a single quote (in Mouse 2002 the ~) is ignored as a comment. This makes MOUSE code much more readable and maintainable..
Operators are executed in order recieved, so Mouse is RPN, like Forth. To add 3 and 5 then print the result, you say:
3 5 + ! $
Two stacks are used in Mouse, a calculation stack and an environment stack. The environment stack supports local variables during macro calls. Macros are defined at the end of the program and contained between a $ and an @. For example:
$B 1% 2% + @
Defines a macro that adds two values. The 1% and 2% are replaced with the parameters passed to the macro. If we call the B macro (earlier in the file) with:
#B,3,B;
then 1% is replaced with 3 and 2% is replaced with the value of the variable B. Note that this is NOT the macro B, but the variable B. And it is not the variable, but it's value. As another example:
#Z,C.;
calls the Z macro, passing it the value of c, one of the 26 variables in the current environment.
$Z ~ ANSI clear screen (w/ specified foreground color) macro 27 !' "T" 7 48 + !' 1% 48 + !' @
The 1% references the parameter (in this case C) passed by the call to the macro.
Loops allow a program to execute a segment of code repeatedly until a specified ending condition is met. In most programming languages, this is done with a "for" loop, a "while" loop, a "repeat" loop, or some equivalent of these. In Mouse, loops are performed with the ( (parentheses) and ^ (caret) operators. A simple infinite loop has the form
( S )
where S is a statement or series of statements. The carat ( ^ ) operator is used to break out of a loop. If the boolean value on the top of the stack is false (0), the ^ operator will break out of the loop and continue execution following the right parenthesis. The carat operator may be used to construct a "while" loop. The Mouse statement
( B ^ S )
is equivalent to: while B do S , where B is a boolean value (0 or 1), and S is a series of statements. Similarly, a "repeat" loop may be constructed in the form
( S B ^ )
This is equivalent to: repeat S until (not B) .
Conditional statements are program statements that are executed only when a specified condition is true. In most programming languages, this is done with an "if" statement; in Mouse, the bracket operator ( [ ) is used. In Mouse, the expression
B [ S ]
is equivalent to: if B then S , where B is a boolean value (0 or 1), and S is a series of statements. When Mouse sees a [ operator, it executes the code between the [ and the matching ] only if the prior value is greater than zero. If it is less than or equal to zero, the code between [ and ] is skipped. The [ operator drops the stack in either case.
Mouse 2002 includes an "else" operator ( | ) for use with conditional statements. The syntax B [ S | T ] will execute statements S if boolean value B is true (1), or statements T if B is false (0).
Here are some commonly found statements in Mouse.
X: ~ store into variable X X. ~ recall variable X X. Y: ~ assign X to Y N. 1 + N: ~ increment N by 1 P. Q. P: Q: ~ swap values of P and Q ? A: ~ input a number and store in A P. ! ~ print variable P
Here is the standard program to calculate Prime numbers in mouse:
"PRIME NUMBERS!" "1 " N 3 = ( #P,N.; ' call P(N) N . 10001 - ^ ' if N >= __ break N N . 1 + = ' N++ ) "!" $P F 1 = ' F = 1 N 1 = ' N = 1 (flag 1=no divisor found) X %A = ' X = first argument ( ' F = F + 1 F F. 1 + = ' break loop when F > A/2 F. 2 X. / - 1 + ^ ' if A / F * F >= 0 F. X. / F. * X. - 1 - [ ' set N = 0 and break out of loop N 0 = 0 ^ ] ) ' if N > 0 then print X N . [ X. ! " " ] ' return @ ' end of program $$
Lunar Lander in Mouse:
"!" " ___ !" " /...\ !" " /.=.=.\ !" " |_____| !" " _/ | \_ !" " !" "Lunar Lander!" "!" F 1000 = 'Fuel A 100 = 'Altitude R 100 = 'Range I 0 = 'Horizontal Velocity J 0 = 'Vertical Velocity G 3 0 - = 'Gravity ( "!" "### Alt=" A.! " Range=" R.! " Fuel=" F.! " HV=" I.! " VV=" J.! " Grav=" G.! " ###!" F. 'if there is fuel left [ "Horizontal Thrust? " H ? = "Vertical Thrust? " V ? = ] F. 0 - 'if out of fuel [ "Out of fuel.!" 'then no thrust H 0 = V 0 = ] I I. H. + = 'Horizontal Accel J J. G. + V. + = 'Vertical Accel A A. J. + = 'Adjust Altitude R R. I. + = 'Adjust Range 'take abs val of h and v H. 0 - 'if h < 0 [ H H. 0 - = 'then h = - h ] V. 0 - 'if v < 0 [ V V. 0 - = 'then v = -v ] F H. V. F. - - = 'fuel = fuel - v - h A. 0 - 'if altitude < 0 [ "!!### You landed with!" "a horizontal velocity of " I.! "!" "a vertical velocity of " J.! "!" R.! " meters from the landing pad.!" 0 ^ ] ) $$
MOUSE Language Reference |
|
$$ | end of program In later versions, a single $ was used. |
" | print up to next " |
! | print CR/LF inside "'s |
? | read number from stdin and push |
! | pop number from stack and print it In some versions !' prints the value on the top of the stack as an ASCII character. |
n | push the integer n onto stack |
A-Z | push address of variable on stack |
. | replace address on stack with value |
= | pop value, pop address, write value at address In later versions this was changed to : |
+ | pop a, pop b, push a + b |
- | pop a, pop b, push a - b |
* | pop a, pop b, push a * b |
/ | pop a, pop b, push a / b |
[ | pop c, if c<=0 then skip to ] on same level |
) | loop back to previous ( at same level |
^ | pop c, if c!=0 then break out of ( ) loop |
#M; | substitute M with any single letter to call macro M with no arguments |
#M,args; | substitute M with any single letter and args with one to 26 comma-separated arguments to call a macro with one or more arguments. |
@ | terminates a macro definition (return) |
%A | push the value of parameter A (substitute any single letter for A) In some versions 1% pushes the first parameter (substitute any number for 1) |
$A | the beginning of macro A (substitute any single letter for A) |
Mouse Interpreter in 186 lines of C
Mouse "Debugger" (Single Step Interpreter) in C
Mouse "Compiler" in C (translates Mouse to C)
Mouse 2002 Interpreter in 1961 lines of C
Mouse : A Language for Microcomputers
by Peter Grogono
Published by Petrocelli Books
Publication date: February 1983
ISBN: 0894332015
See also: