;; CSE341, Programming Languages, Homework 5 #lang racket (provide (all-defined-out)) ;; so we can put tests in a second file ;; definition of structures for MUPL programs - Do NOT change ;; (struct var (string) #:transparent) ;; a variable, e.g., (var "foo") ;; (struct int (num) #:transparent) ;; a constant number, e.g., (int 17) ;; (struct mtrue () #:transparent) ;; boolean constant true ;; (struct mfalse () #:transparent) ;; boolean constant false ;; (struct add (e1 e2) #:transparent) ;; add two expressions ;; (struct isgreater (e1 e2) #:transparent) ;; if e1 > e2 then (mtrue) else (mfalse) ;; (struct mif (e1 e2 e3) #:transparent) ;; if e1 then e2 else e3 ;; (struct fun (nameopt formal body) #:transparent) ;; a recursive(?) 1-argument function ;; (struct call (funexp actual) #:transparent) ;; function call ;; (struct mlet (var e body) #:transparent) ;; a local binding (let var = e in body) ;; (struct apair (e1 e2) #:transparent) ;; make a new pair ;; (struct first (e) #:transparent) ;; get first part of a pair ;; (struct second (e) #:transparent) ;; get second part of a pair ;; (struct munit () #:transparent) ;; unit value -- good for ending a list ;; (struct ismunit (e) #:transparent) ;; if e1 is unit then (mtrue) else (mfalse) ;; a closure is not in "source" programs; it is what functions evaluate to (struct closure (env fun) #:transparent) ;(define foo-mupl ; (mif (mtrue) (int 0) (int 3))) (define foo (if #t 0 3)) (define bar (if #t 0 3)) (define bar-quoted '(if #t 0 3)) (define (eval-under-env env e) (cond [(symbol? e) (cdr (assoc e env))] [(eq? e #t) e] [(eq? e #f) e] [(number? e) e] [(and (list? e) (not (null? e)) (eq? (car e) 'if)) (if (eval-under-env env (cadr e)) (eval-under-env env (caddr e)) (eval-under-env env (cadddr e)))] [else (error "unrecognized expression" e)]))