List/Sublist Projects: Extended Calculator (eval
pt. 2)
((to remind you of the context of all this.....))
These projects revolve around the program for reading and printing
lists and sublists, as discussed in class.
(Note: this incudes an updated Tokenizer -- please use the new copy).
(Note: requires Java 1.5 at least)
Overview:
- First learn about how the provided program and data structure
works
- Provide a mechanism for handling the allocation of Token objects,
leading eventually to a kind of "garbage-collection" scheme.
- Implement some arithmetic functions, leading eventually to a
simple programming language with variables.
Due dates:
- #13: phase 1, week of Nov 26
- #15: phase 2, Wednesday Dec 5
- #16 phase 3, or #17, eval pt 2: Wednesday
Dec 12
- Final date for any homework: Friday, Dec 14
After Friday, you should be concentrating on exams for this and your
other classes. No homework will be accepted after Friday, Dec 14,
2007.
- Final exam, Tuesday Dec. 18, 3-6 pm, M-1-619
This page documents the augmented eval project (#17).
Alternatively, you
can work on the garbage collector project (#16).
Assignment 17: Extended Cacluator for Item/List
In this phase, we extend the simple "eval" logic previously added to
the Item/List framework, to provide a calculator language with
variables and conditionals.
Variables
Once we have variables working, "eval" should be able to handle input
such as
(plus 3 (times apple (plus core 1)))
The Item/List architecture can easily accomodate identifiers (words) in
the list structure, as well as numbers. The Token type has ways
you can determine what you're looking at (number, word, special
character) and get to the appropriate information (string, numeric
value). So much of the framework is already in place.
Seems like we'll need:
- A place to store variables and their current values
- A way to assign a [new] value to a variable. (what
are the values of "apple" and "core" in the previous example?)
- Logic to recognize when a variable appears in an expression and
fetch its current value.
For a storage place, one of the java.util.Collection libraries should
be tailor-made for the job, so there need not be too much work.
You'll want a "map" -- like a "HashMap" -- that not only has "keys"
(the identifiers) but also associated "values". (The values will
be of type "Integer" .. I believe Java will let you move easily between
"int" and "Integer").
To assign values, we'll need to invent a new operator. Call it
something like "assign", thus
(assign apple 123)
(assign core (plus 3 9))
("assign" should also return the value it's assigned, just as Java does
with an embedded assignment statement.)
Formal Definition: Sublist, first Token is
"assign", second is name of variable to receive a new value, third Item
(Atom or IList) will be evaluated (call "eval") to produce value to be
assigned.
Conditions
To make things a bit more interesting, let's have a conditional
capability, like Java's "if" - "else". Actually, since we're
dealing with expressions (which have values), it will be more closely
like the "?" - ":" construct.
We can easily provide functions for testing conditions: equal,
less.
We then add an "if" operation.
Here's how it might look:
(if (less apple core) (plus 7 core) (times 3 apple))
Of course, we should have a Formal Definition:
Sublist, first token is "if". Second item is
condition: feed it to "eval", if value is 0 then condition is
"false", otherwise it's "true". (this convention is
followed by many languages, like C/C++). If condition is
true, then evaluate third item and return that value, do not evaluate
fourth item. If condition is false, vice-versa.
Let's not get carried away!
How about something like this?
(assign sum 0)
(for
( (assign j 0) (less j 10) (assign j (plus j 1)) )
(assign sum (plus sum j))
)
sum
- What will this "program" print for the last thing
evaluated? (hint: the current value of "sum").
- Can you draw a picture of the internal data structures created
when this is read in?
- Could you offer a "Formal Definition" of the "for" construct?
hint: these are good practice questions for the final exam.
;-)
Notice how easy it is to define a small "programming language", if you
start with the right building blocks!