Post date: Jul 13, 2009 8:20:59 PM
Deadlines:
All students must adhere to Academic Code of Conduct. You are welcome to brainstorm ideas with your fellow students, but you are not allowed to share code.
Here is a quick rundown of a list of functions you need to implement or functions of interest. The provided functions may help you with the functions you need to implement, so you should study them.
For a total of 200 points.
If you cannot finish this homework, try to concentrate on what you do know how to do. Try not to work on many incomplete functions for partial credit.
Forth is a programming language for manipulating and performing computation on stack. In this assignment, you'll implement a simplified Forth interpreter.
A Forth program is read from standard input (Eclipse console) as a sequence of tokens separated by whitespace (space, tab, new line, etc). Each token can either be an integer or the name of an operator. If the token is an integer, it is pushed on the stack. If the token is an operator, then some computation is performed. In particular, there is a special operator "." (just the period) which pops an item from the stack and prints it to the standard output.
Since the program operands are in stack order, "0 1 . ." will first print "1" then "0".
You will need to implement the following operators.
ForthInterpreterMain.java hooks the standard input to a scanner, which converts a sequence of characters to a sequence of input tokens. The code for doing that is provided for you, as well as the ability to push integer tokens on the stack and the implementation of the "." operator. Before you can run this program, you need to implement Stack.java. Once this is done, you should be able to enter some numbers and use "." to see that integer tokens are printed in LIFO order.
Once you implement more operators, you can test them similarly, using "." to observe the output.
If you enter "3 dup * ." (push 3 to the stack, duplicate 3, multiply 3 by 3, and print the result) you'd see "9" printed as the result. If you enter "3 dup * 4 dup * + ." ("3 dup *" becomes 9, which is saved to the stack; "4 dup *" becomes 16, which is saved to the stack; add 9 and 16, and print the result), you'd see "25" printed as the result.