Due by class time Thursday, November 7
Download the file assign13.scm and use it as your starting point for this assignment. This is the version of the interpreter we developed in class today with support for multiple-body with-expressions and a read-eval-print loop.
Unfortunately, there isn't an auto-tester program available for this assignment, so you will have to test your code manually. However, I have included several test expressions at the end of assign13.scm to make this easier. For example, to run the test1 expression in the interpreter, just type (run test1) at the DrRacket prompt. Check the comments at the end of assign13.scm to see what the expected output for each test should be.
Think of a few possible good names for our new language.
Modify the interpreter code so that fun expressions with one or more body expressions are allowed. When a multiple-body fun is applied to some arguments, the body expressions should be executed in sequential order, and the value of the final expression should be returned, similar to the way multiple-body with expressions work. Hint: use m-sequential as a helper. Test cases:
(run test1) (run test2) (run test3) (run test4)
If you weren't able to get your (or exp1 exp2) expressions to work for the previous homework assignment, add support for them now to the current version of the interpreter, using the supporting code we developed in class for and expressions as a guide (which is included in the assign13.scm file). Test cases:
(run test5)
Add support for do-expressions to the interpreter. A do-expression has the syntax (do exp ...). That is, it is just a list that starts with the keyword do, followed by one or more body expressions. A do-expression simply evaluates each of its body expressions in sequential order, from left to right, and returns the result of the final expression. You will need to define a new helper function (do-expression? x) that checks if x is a do-expression, and then add some code to the interpreter to handle do-expressions. Hint: use m-sequential as a helper. Also, don't forget to add do to the list of reserved keywords, to prevent the interpreter from misclassifying a do-expression as a function application. Test cases:
(run test6) (run test7)
Generalize and and or expressions to take one or more subexpressions (not just two). Hint: define a helper function that takes a list of the subexpressions (along with the current environment) as input, and evaluates them in order, similar to the way m-sequential works. In the case of and, if a subexpression is encountered that evaluates to false, no further subexpressions should be evaluated, and #f should be returned immediately. Only if all subexpressions in the list evaluate to true should #t be returned as the final value for the and expression. Likewise, in the case of or, if a subexpression is encountered that evaluates to true, no further subexpressions should be evaluated, and #t should be returned immediately. Only if all subexpressions in the list evaluate to false should #f be returned as the final value for the or expression. Test cases:
(run test8) (run test9)
Add support for repeat-loops to your interpreter, with the following syntax:
(repeat exp times : body ...)
One or more body expressions may appear after the : symbol. To evaluate a repeat, the exp expression is evaluated once. The result should be a nonnegative number n. The body expressions are then evaluated in order, n times, after which the loop terminates and the symbol done is returned as the final result. If n < 0, the loop should immediately return done without evaluating any of the body expressions. Hint: you can use m-sequential to evaluate the body expressions in order, but you will need to define an additional helper function to control the looping process. Don't forget to add repeat to the list of reserved keywords. Test cases:
(run test10) (run test11) (run test12) (run test13)
Submit your completed assign13.scm file using the Homework Upload Site. Make sure to include your name and the assignment number in a comment at the top of your file.
If you have questions about anything, don't hesitate to ask!