## MOUSE Computer Programming Language

 Home Mouse-79 Mouse-83 Mouse-2002 Contact Me David Simpson

### MOUSE-2002

Mouse-2002 in an enhanced version of Mouse-83 described in the book Mouse: A Language for Microcomputers by Peter Grogono .

#### Contents

The Stack and Arithmetic
Input and Output
Simple Programs
Variables
Conditional Statements
Loops
Macros
Extensions to Mouse-83
Common Idioms
Sample Programs
Bibliography
Web Sites

#### The Stack and Arithmetic

The Mouse programming language performs its data manipulations using a stack and what is called Reverse Polish Notation (RPN). RPN is a mathematical notation developed by the Polish logician Jan Łukasiewicz (1878–1956). It allows complex mathematical expressions to be written unambiguously without the use of parentheses, and is well suited for efficient programming. It has been used on most of the popular Hewlett-Packard calculators since the early 1970s, and is also currently used by a few other programming languages like Forth and PostScript.

In standard mathematical notation, we write a mathematical operator between its two operands; for example:

2 + 3

In RPN, we write the operator after the two operands:

2 3 +

In computer implementations of RPN, each of the operators is stored on a stack before the operation takes place, and the operator operates on the numbers stored on the stack. The stack may be thought of as an array of storage registers, each of which can hold a single number. Placing a number on the stack bumps each of the other numbers further into the stack, and the last number placed on the stack is the first one taken off. By convention, the register on the "top" of the stack is called X; the next one is called Y; the next one Z; and the next one T.

The operation of the stack is perhaps best illustrated with an example. Suppose we wish to compute 2 + 3 using RPN and a stack. We would perform the following steps:

StepStackDescription
XYZT
22    Put 2 on the stack.
332   Put 3 on the stack;
2 is bumped up to Y.
+5    Add the numbers in X and Y;
put the result in X.

More complex operations may be performed similarly. For example, suppose we wish to compute

(2 + 3) * (4 + 6)

Here the * operator indicates multiplication. This calculation would be performed in RPN as follows:

2 3 + 4 6 + *

The following table shows how the stack changes with each step:

StepStackDescription
XYZT
22    Put 2 on the stack.
332   Put 3 on the stack;
2 is bumped up to Y.
+5    Add the numbers in X and Y;
put the result in X.
445   Put 4 on the stack;
5 is bumped to Y.
6645  Put 6 on the stack;
4 is bumped up to Y;
5 is bumped to Z.
+105   Add the numbers in X and Y;
put the result in X;
5 drops down to Y.
*50    Multiply the numbers in X and Y;
put the result in X.

Notice that the + operator always adds the numbers in X and Y, leaves the result in X, and "drops" the stack so that each number moves down one. Similarly, the * operator multiplies the numbers in X and Y, leaves the result in X, and drops the stack.

Expressions of considerable complexity may be performed in RPN in this fashion, leaving intermediate results on the stack. Parentheses are never needed. With a little practice, you will find that RPN is a very efficient means of evaluating expressions.

Five mathematical operators are available in Mouse-2002:

OperatorDescription
- Subtraction (subtract Y - X)
* Multiplication (multiply Y × X)
/ Division (divide Y ÷ X)
\ Remainder (of Y ÷ X)

Notice the order of operations for subtraction and division. For example, to perform

(2 - 3)/(4 - 5)

in RPN, would would use the sequence

2 3 - 4 5 - /

The Mouse-2002 language operates only with integers. Consequently, the division operator (/) performs integer division: only the integer part of the quotient is returned, and any remainder is discarded. (The \ operator may be used to compute the remainder.)

#### Input and Output

Mouse-2002 has operators to input a number or character from the terminal, output a number or character to the terminal, and print a string to the terminal.

OperatorDescription
? Input a number from the terminal
?' Input a character from the terminal
! Print a number to the terminal
!' Print a character to the terminal
" Print a string to the terminal

For example, to read a number from the terminal and print it to the terminal, we would use the sequence

? !

(Spaces are not significant in Mouse, so we could eliminate the space between the operators and write ?!.)

To be more specific, the ? operator reads a number (an integer) from the terminal, and pushes it onto the top of the stack (the X register); any other numbers already on the stack are "bumped" further into the stack. The ! operator prints the number in the X register to the terminal, then "drops" the stack.

The operators ?' and !' work the same way for character input and output. When a charcter is input from the terminal, its ASCII code is pushed onto the stack. The !' operator prints the character whose ASCII code is on the top of the stack.

To print the string "Hello" to the terminal, we would use

"Hello"

The " operator will print all of the following characters to the terminal until it finds a matching ". If there is an exclamation mark (!) embedded in the string, a carriage return will be executed, and the following text will appear on the next line. For example,

"Hello!Hello again"

will print

Hello
Hello again

#### Simple Programs

A Mouse-2002 program is essentially a sequence of Mouse operations stored in a file, with the character \$ at the end of the file to mark the end of the program.

For example, a simple program to input a number from the terminal and print it at the terminal would be:

?!\$

Within a mouse program, two numbers are separated by a space. For example, the following program adds 2 + 3:

2 3 + \$

The following program reads a number from the terminal, adds 3 to it, and prints the result to the terminal:

? 3 + ! \$

#### Variables

The Mouse-2002 language has 26 variables, called A, B, C, ..., Z. You may store any number into any of these variables, then recall the number from the variable at any later time within a program.

When Mouse encounters a variable name in a program, is pushes that variable's address onto the stack. To print the address of variable D, for example, we could use the program

D ! \$\$

The addresses of variables A through Z are the integers 0 through 25, respectively, so this program would print 3 for the address of D.

To store a number into a variable, you use the : (colon) operator. The : operator takes the number in the stack's Y register, and stores it into the variable whose address is in the X register. For example, we could write a program to store a 37 into variable D as follows:

37 D: \$

This program works as follows:

StepStackDescription
XYZT
3737    Put 37 on the stack;
address of D is bumped up to Y.
D337   Put address of D on the stack.
:     Store 37 into D.
\$     End of program.

To recall a number from a variable, you use the . (period) operator. The . operator takes the address of a variable from the X register, and returns the number stored in that variable. The result replaces the address in the X register.

For example, the sequence

D. !

Prints the value stored in variable D to the terminal.

The following program stores a 23 into variable G, then recalls the value from G and prints it to the terminal:

23 G: G. ! \$

Here's a step-by-step stack diagram for this program:

StepStackDescription
XYZT
2323    Put 23 on the stack;
address of G is bumped up to Y.
G623   Put address of G on the stack.
:     Store 23 into G.
G6    Put address of G on the stack.
.23    Fetch contents of G.
!     Print contents of G.
\$     End of program.

#### Conditional Statements

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-2002, 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.

Descrition in detail. When Mouse sees a [ operator, it executes the code between the [ and the matching ] only if the number in the X register is greater than zero. If X is less than or equal to zero, the code between [ and ] is skipped. The [ operator drops the stack in either case. For example, the following sequence adds 3 to variable A if variable E is greater than zero:

A. E. [ 3 + ]

Here's a step-by-step stack diagram for this sequence, assuming A=7 and E=1:

StepStackDescription
XYZT
A0    Put address of A on the stack.
.7    Fetch contents of A.
E47   Put address of E on the stack.
.17   Fetch contents of E.
[7    Test X register and drop stack.
337   Condition true; put 3 on stack.
]10    End of conditional.

But if E=0, we get the following:

StepStackDescription
XYZT
A0    Put address of A on the stack.
.7    Fetch contents of A.
E47   Put address of E on the stack.
.07   Fetch contents of E.
[7    Test X register and drop stack;
]7    End of conditional.

#### Loops

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-2002 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) .

See the section on sample programs for examples on the use of loops in Mouse.

#### Macros

Macros are the equivalent of subroutines in Mouse-2002. Macro names may be only a single letter, A through Z.

To call the macro, the main program uses the # operator. For example, the macro A may be called from the main program using the instructions

#A;

Here the # operator marks a call to macro A, and the semicolon marks the end of the macro call.

A macro is defined using the \$ operator. A simple macro (named A in this example) has the form

\$A <instructions> @

Here the \$ operator defines the start of macro A and the @ operator returns exectution from the macro back to the main program.

A program may also pass parameters to a macro. For example, suppose we wish to write a macro B that will add two numbers together. To add 3 and 5, we call the macro using

#B,3,5;

A comma is used to separate the macro name from its parameters, and to separate the parameters.

The % operator is used to define parameters in a macro. In this example, macro B may be defined using

\$B 1% 2% + @

Here 1% and 2% are dummy variables that hold the values of the parameters. In this example, 1% will be assigned the value 3, and 2% will be 5.

#### Extensions to Mouse-83

Mouse-2002 includes several enhancements to Mouse-83, as described below.

"Else" operator. As suggested in Mouse: A Language for Microcomputers, 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). (In other words, it is equivalent to if B then S else T .)

Global variables. Also as suggested in Mouse: A Language for Microcomputers, 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.

Change-sign operator. The new 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 -.

Floating-point support. Whereas Mouse-83 uses integers as its only numeric data type, Mouse-2002 uses only floating-point numbers. This is generally compatible with Mouse-83, except that the division operator ( / ) now performs floating-point division, rather than integer division. If it is necessary to do integer division, this can be done using / &INT.

• Input:
? Reads in a floating-point number. It may be entered in either fixed-point notation or standard scientific notation. (E.g. 1.23 or -1.23E-45.)
• Output:
! Prints a floating-point number according to the current display setting (&FIX, &SCI, or &GEN (the default)).
• Constants:
Floating-point constants in a program are entered in Mouse notation, using _ for negative numbers and &EEX for scientific notation. (E.g. 1.23 or 1.23_ 45_ &EEX.)

Function operator. This new operator provides a means for extending the language indefinitely. The & operator is immediately followed by a character string that identifies its function. For example:

• &SQRT  Square root
• &LN    Natural logarithm
• &SIN   Sine
• &PI    Pi
The string for the "& function" must be terminated by a blank space.

Arrays. Arrays are now supported through a single large, one-dimensional "universal" array. Multidimensional arrays can be stored in one-dimensional form, and multiple arrays can be stored in different areas. For example, three 3x3 arrays can be stored at indices 0-8, 9-17, and 18-26. Two functions are needed:

• &STO   Stores an array element from the stack
• &RCL   Recalls an array element to the stack
The array space may also be used for additional variable storage, if desired.

File I/O. Mouse-2002 includes some rudimentary file I/O capability. File names may only be of the form MOUSE.nnn, where nnn is an integer.

• Open a file: nnn mode &FOPEN (Opens MOUSE.nnn)
• Close a file: nnn &FCLOSE (Closes MOUSE.nnn)
• Write to a file: nnn &F! (Writes to MOUSE.nnn)
Other file I/O functions: &F?' &F!' &F" &FEOF &FREWIND

Interactive mode. The Mouse interpreter may be used to interpret a file, in the conventional manner:

MOUSE filename

Or it may be started in an "interactive mode":

MOUSE

Enter &EXIT or &QUIT to leave the Mouse interpreter's interactive mode.

Other changes. The @ operator has been modified to discard all environment stack frame entries until it finds a "macro" tag. This allows it to discard all "loop" entries and immediately return from the macro, even from inside a loop.

#### Common Idioms

These expressions appear frequently in Mouse programs.
```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
```

#### Sample Programs

This short program prints 'Hello world.'

```"Hello world."
\$
```

This program displays the squares of the integers from 1 to 10.

```1 N:              ~ initialize N to 1
( N. N. * ! " "   ~ begin loop; print squares of numbers
N. 10 - 0 < ^   ~ exit loop if N >= 10
N. 1 + N: ) \$   ~ increment N and repeat loop
```

#### Bibliography

1. Grogono, Peter. "Mouse: A Language for Microcomputers", Byte Magazine, July 1979, pp. 198 ff.
2. Grogono, Peter. Mouse: A Language for Microcomputers. Petrocelli Books, New York, 1983.

#### Web sites

1. Grogono, Peter.   The Mouse Programming Language
2. Fuller, Sean.   The Great Mouse Programming Language Revival
3. Hunt, Tom.   Friends of the Mouse
4. Bradley, Lee.   Mouse, the Language
5. Wikipedia article

#### Contact Information

I may be contacted at: 