Programming Languages -- Spring '98 -- Homework 3: Simple Interpreters

Due Tuesday, February 3, at 11:59pm.

  1. EOPL Exercise 5.1.4
  2. EOPL Exercise 5.1.5
  3. EOPL Exercise 5.2.1
  4. EOPL Exercise 5.2.2
  5. EOPL Exercise 5.2.3

Frequently assignments, such as this one, will have multiple parts involving interpreter modification. Unless otherwise noted, in such cases you are to hand in a single interpreter incorporating all of the modifications. It is good software development practice to add the modifications of one assignment part at a time and test each before proceeding.

Here are a couple of tests for homework 3:

> (eval-exp
    (recordify
      '((if (if (+ (if 1 0 0) (if 0 0 1)) 0 1) car cdr) (cons 231
      24))))
24

> (eval-exp
    (recordify
      '((if (null emptylist) less greater) (+ 1 2) (* 1 2))))
0

> (eval-exp (recordify '(+ (null emptylist) (zero 0))))
2
For your convinience, we provide an updated recordify procedure that now handles let and if expressions.
(define-record num (val))
(define-record varref (var))
(define-record lambda (formals body))
(define-record app (operator operands))
(define-record if (test then else))
(define-record let (decls body))
(define-record decl (var exp))

(define recordify
  (lambda (exp)
    (cond
      ((number? exp) (make-num exp))
      ((symbol? exp) (make-varref exp))
      ((atom? exp) (error 'recordify "illegal expression ~s" exp))
      ((eq? (car exp) 'lambda)
       (make-lambda (cadr exp) (recordify (caddr exp))))
      ((eq? (car exp) 'if)
       (make-if (recordify (cadr exp)) (recordify (caddr exp))
	 (recordify (cadddr exp))))
      ((eq? (car exp) 'let)
       (let ((vars (map car (cadr exp))) (exps (map cadr (cadr exp))))
	 (let ((exps (map recordify exps)))
	   (make-let (map make-decl vars exps) (recordify (caddr exp)))))) 
      (else (make-app (recordify (car exp)) (map recordify (cdr exp)))))))

Submission

As usual.
milevin@cs.indiana.edu