MOUSE |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
MOUSE-2002Mouse-2002 in an enhanced version of Mouse-83 described in the book Mouse: A Language for Microcomputers by Peter Grogono [2].
ContentsThe Stack and ArithmeticInput and Output Simple Programs Variables Conditional Statements Loops Macros Extensions to Mouse-83 Common Idioms Sample Programs Bibliography Web Sites
The Stack and ArithmeticThe 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 (18781956). 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:
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:
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:
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 OutputMouse-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.
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
Simple ProgramsA 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 + ! $
VariablesThe 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:
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:
Conditional StatementsConditional 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 expressionB [ 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:
But if E=0, we get the following:
LoopsLoops 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.
MacrosMacros 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-83Mouse-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.
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:
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:
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.
Interactive mode.
The Mouse interpreter may be used to interpret a file, in the conventional
manner: 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 IdiomsThese 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 ProgramsThis 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
Web sites
Contact InformationI may be contacted at:![]() |
Copyright © 2006 David G. Simpson
http://Mouse.DavidGSimpson.com