Com S 342 meeting -*- Outline -*- * Conditional Evaluation (3.3) ------------------------------------------ CONDITIONALS (3.3) What should be the concrete syntax for if-expressions in the defined language? ------------------------------------------ ... ::= if then else give examples: if less?(2,3) then 5 else 4 Q: What would this be like if we had a distinction between statements and expressions? ------------------------------------------ FOR YOU TO DO IN GROUPS Close your books. 1. What should the abstract syntax be? 2. What type should the test have? 3. Write the part you need to add to the interpreter, and any needed code. 4. Do any primitive procedures need to be built-in to make this useful? Write the necessary changes for them. ------------------------------------------ The interpreter's grammar in the-grammar: (expression ("if" expression "then" expression "else" expression) if-exp) The ASTs ... (define-datatype expression expression? ... (if-exp (test-exp expression?) (true-exp expression?) (false-exp expression?))) Case in eval-expression ... (cases expression exp ... (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)))) ;;; Anonymous figure : page 80 (deftype true-value? (-> (expressed-value) boolean)) (define true-value? (lambda (x) (not (zero? (expressed->number x))))) Be sure to clarify what the actual syntax is that will be used from here on in the defined language. But it isn't necessary to use that. ... need primitives like equal, positive, less, etc. Can we have "not" as a primitive? ** semantic implications and variations Q: What options do we have for representing truth values? we discussed 0 = false, not 0 = true vs. 0 = false, 1 = true, everything else is an error vs. adding a boolean type explicitly ** example, adding unless ------------------------------------------ ADDING UNLESS EXPRESSIONS new syntax: (expression ("unless" expression "do" expression) unless-exp) new ASTs: (unless-exp (test-exp expression?) (false-exp expression?)) New clauses in eval-expression: ------------------------------------------ ... (unless-exp (test-exp true-exp) (if (true-exp? (eval-expression test-exp env)) (number->expressed 0) (eval-expression true-exp env)))