For this and all programming project’s, you are welcome to work in groups of up to three. The namesof all group members
Submit Assignment
For this and all programming project’s, you are welcome to work in groups of up to three. The names of all group members should appear at the top of the file, and every member should submit the project on blackboard. All team members are responsible for understanding the code submitted in their name. You do not have to keep the same group as the previous interpreter parts.
Here is solution code for the interpreter, part 2. These solutions do not use boxes and do not support side effects. They are the same except that one has the M_state functions tail recursive (but not the M_value functions) and uses (lambda (v) v) type continuations, and the other uses “normal” recursion and call/cc for the continuations.
Both solutions are written to work with R5RS scheme. If you are using racket instead of scheme, you need to add #lang racket to the top of the file and change the (load “simpleParser.scm”) to (require “simpleParser”).
Solution 1: interpreter2tailrecursionnoboxes.scm
Solution 2: interpreter2callccnoboxes.scm
This interpreter needs a new parser: functionParser.scm
As with the previous parser, this one is written for R5RS scheme, and you will need to comment/uncomment some lines to use it with racket.
The same lex.scm file will work with the new parser.
In this homework, you will expand on the interpreter of part 2 adding function definitions. We still assume all variables store integers and boolean. Likewise, all functions will only return integers and boolean.
While normal C does not allow nested functions, the gcc compiler does allow nested functions as an extension to C, so let’s implement them!
For those seeking a small extra challenge: try implementing both the callbyreference and the callbyvalue
parameter passing styles.
An example program that computes the greatest common divisor of two numbers is as follows:
var x = 14; var y = 3 * x - 7; function gcd(a,b) { if (a < b) { var temp = a; a = b; b = temp; } var r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } function main () { return gcd(x,y); }Note that only assignment statements are allowed outside of functions. Functions do not have to return a value. The parser will have the following additional constructs:
function a(x, y) { => (function a (x y) ((return (+ x y))) return x + y; } function main () { => (function main () ((var x 10) (var y 15) (return (funcall gcd x y)))) var x = 10; var y = 15; return gcd(x, y); }Nested functions can appear anywhere in the body of a function. Any name in scope in a function body will be in scope in a function defined inside that body.