Environment Passing Interpreters II Conditional Evaluation
Continue working on Assignment 4.
- interp3-1.scm implements the simplest environment passing interpreter, using code in 3-1 and 3-2. The language has variables as well as numbers and primitive applications.
Character syntax grammar:
It contains definitions with some examples of[program] ::= [expression] [expression] ::= [number] | [symbol] | [primitive]([expression]{, [expression]}*) [primitive] ::= + | - | * | add1 | sub1
environmentprogramexpressionprimitiveeval-programeval-expressionparse-programparse-expressionunparse-programunparse-expressionrun-list-syntaxlex-specgrammar-3-1runfor character syntaxread-eval-printfor character syntaxList syntax expressions can be interpreted with
run-list-syntax, which exercisesunparse-programas well asparse-program.We would like to maintain the ability to parse and run list syntax as well as character syntax in our interpreters.(define run-list-syntax (lambda (datum) (pretty-print (unparse-program (parse-program datum))) (display " => ") (eval-program (parse-program datum))))
- Adding conditional evaluation necessitates adding an
if-expexpression and a definition of boolean values.The text uses 0, 1 for boolean values, but we will introduce a
booltype and correspondingexpression. In interpreting boolean values, we can use(define-datatype bool bool? (true-value) (false-value))The clause for(define true-value? (lambda (x) (cases bool x (true-value () #t) (false-value () #f) (else (error 'bool "not a bool" x)))))if-expineval-expressionthen becomesAn example primitive(if-exp (test-exp true-exp false-exp) (if (true-value? (eval-expression test-exp env)) (eval-expression true-exp env) (eval-expression false-exp env)))equal?which hasboolvalue is also added, to give an example of adding a primitive.Modifications in the grammar are needed to parse character syntax conditional expressions and boolean constants
trueandfalse, say togrammar3-3.We also need to modify
eval-expressioneval-expresssionexpressionparse-expressionunparse-expression
- The result is interp3-3.scm
You can use it as a guide for extending interp3-1, but use it only to help correct errors.