|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Due 2016-Nov-18 15:00 I strongly recommended downloading R0 and getting it running, and then completing the required test cases, by Nov-14 (Mon).
Over the course of several homeworks, we'll implement a “tower” of languages (R0, R1, …, R6) each language incorporating more features than the previous. R0 is provided for you.
(15pts) Implement R1 in both racket, and Java. R1 is just like R0, but with two additional types of expressions:
Expr ::= … | IfZeroExpr
IfZeroExpr ::= [:0 Expr Expr Expr 0:]
Op ::= … | ;% (“mod”)
|
Be sure to write test cases first; To ensure everybody makes test cases to cover the basics, I've spelled out these R2-test-requirements—R2: initial tests.
The only method which cares what these new expressions mean
(their semantics)
is
For the expression
Note that you are provided sufficient test cases for
For
You must make your own test cases for
Complete two versions of R1: both racket, and java. (For R2 and beyond, you can choose which implementation to continue.)
(25pts) Implement R2 in either racket or Java (your choice). R2 adds identifiers to R1:
Expr ::= … | Id | LetExpr LetExpr ::= :o Id Expr :U Expr |
Update your three methods
In order to write
Observe that when evaluating a (legal) R2 program,
In order to make a substitution in an
For example:
You can choose implement R2 in either in Racket, or in Java.
Repeating the steps above in more words: For R2, you will:
1
Because we don't need to check for bad inputs,
it's fine to have your interpreter crash if y=0.
If you prefer to "control" crash — creating a meaningful error message
and calling
2 For comparison, here is what comparable constructs look like in other languages:
ML-like: | let x = 2+3 in x*9 end; |
lisp-like: | (let {[x (+ 2 3)]} (* x 9)) |
lisp-like, simplified: | (let x (+ 2 3) (* x 9)) |
C#-like: | using (var x = 2+3) { return x*9; } |
javascript-like: | var x = 2+3; return x*9; |
Java-like: | { int x = 2+3; return x*9; } |
Haskell-like: | * x 9 \n where x = + 2 3 \n |
Note that you can (and should) test and write a “substitute” function w/o
worrying about the exact syntax of a
3
Note that our different implementations are now varying by
more than just precision of arithmetic:
in a Java implementation,
5
For example: what if a R2 programmer uses a variable
named “
4 All our real code should work on the parse tree itself. String-substitution (like C pre-processor macros) can't be generalized to handle shadowed variables (scope) for R3, and is in general fraught with error. A local-variable construct which requires globally-unique names isn't very impressive! ↩
6
The notation
“
eval(parse!(":o x 5 :U |x 3 ;)|")) = eval(parse!("|5 3 ;) |")) = eval(parse!("8")) |
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
©2016, Ian Barland, Radford University Last modified 2016.Nov.22 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |