|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Due 2016-Jul-19 17:45. I strongly recommended downloading Q0 and getting it running, and then completing the required test cases, by Jul-14.
Over the course of several homeworks, we'll implement a “tower” of languages (Q0, Q1, …, Q6) each language incorporating more features than the previous. Q0 is provided for you.
(15pts) Implement Q1 in both racket, and Java. Q1 is just like Q0, but with two additional types of expressions:
Expr ::= … | IfZeroExpr IfZeroExpr ::= if Expr is zero then Expr else Expr # BinOp ::= … | mod |
Be sure to write test cases first; To ensure everybody makes test cases to cover the basics, I've spelled out these Q2-test-requirements—Q2: 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 Q1: both racket, and java. (For Q2 and beyond, you can choose which implementation to continue.)
(25pts) Implement Q2 in either racket or Java (your choice). Q2 adds identifiers to Q1:
Expr ::= … | Id | LetExpr LetExpr ::= say Id be Expr in Expr mates |
Update your three methods
In order to write
Observe that when evaluating a (legal) Q2 program,
In order to make a substitution in an
For example:
You can choose implement Q2 in either in Racket, or in Java.
Repeating the steps above in more words: For Q2, 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 Q2 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 Q3, 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("say x be 5 in (x add 3) mates")) = eval(parse("(5 add 3)")) = eval(parse("8")) |
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
©2015, Ian Barland, Radford University Last modified 2016.Jul.10 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |