CS 22
CS 22 -- Homework 33
Due: Monday, Apr. 15
- Review 365-380
- Make your interpreter able to handle programs like this sample:
MC-EVAL==>
(begin
(sum := 0)
(sumsqs := 0)
(i := 1)
(while (< i 6)
(display i)
(sum := (+ sum i))
(display " ")
(isq := (* i i))
(sumsqs := (+ sumsqs isq))
(display isq)
(newline)
(i := (+ i 1))
)
(display "sum is: ") (display sum)
(newline)
(display "sum of squares is: ") (display sumsqs) " "
) ;;; end of the program
1 1 ;;; beginning of the output
2 4
3 9
4 16
5 25
;;; end of while loop
sum is: 15
sum of squares is: 55
MC-EVAL==>
The above program is written in OASwatScheme (Only at Swarthmore Scheme). To
your interpreter, you need to add
1) an infix assignment operator :=. You may assume that there is always a
space
before and after := and that its left operand is a simple symbol. The :=
operator
should be able to define a variable if it does not already exist or mutate
a variable if it
does already exist.
2) sequence ( begin ) if you have not already done so.
3) newline and display as primitive procedures.
4) strings as self-evaluating. There is a predicate, string?, in Drscheme
that helps.
5) the while iteration construct. The semantics of the OASwatScheme while
construct
(while )
are as follows. You may assume that
is a
legal OASwatScheme predicate (i.e. it evaluates to #t or #f).
is
a nonempty
sequence of legal OASwatScheme expressions.
The while construct evaluates the
predicate
If evaluates to #t,
then is evaluated
sequentially.
Then is evaluated again.
If evaluates to #t, then is
evaluated
again. This is repeated until evaluates to #f
at which time the while
statement terminates.
Your task is to modify our interpreter as requested and to email me the
changes
necessary and some test runs. Do not email me the whole interpreter, just
send me
what is new.