
The eopl language is now racket-based rather than mzscheme-based. This test-suite, which was originally distributed on the book's web-site has been re-written in the new language. Changes include dropping all drscheme-init.scm and top.scm files. Remaining files were renamed to use the .rkt extension and edited to use the #lang syntax (instead of modulue). Require and provide forms were changed to reflect racket's syntax instead of mzscheme's (eg, only-in vs. only). Several occurrences of one-armed ifs were changed to use when and unless. All tests have been run successfully.
68 lines
2.0 KiB
Racket
Executable File
68 lines
2.0 KiB
Racket
Executable File
#lang eopl
|
|
|
|
;; interpreter for the LET language. The \commentboxes are the
|
|
;; latex code for inserting the rules into the code in the book.
|
|
;; These are too complicated to put here, see the text, sorry.
|
|
|
|
(require "lang.rkt")
|
|
(require "data-structures.rkt")
|
|
(require "environments.rkt")
|
|
|
|
(provide value-of-program value-of)
|
|
|
|
;;;;;;;;;;;;;;;; the interpreter ;;;;;;;;;;;;;;;;
|
|
|
|
;; value-of-program : Program -> ExpVal
|
|
;; Page: 71
|
|
(define value-of-program
|
|
(lambda (pgm)
|
|
(cases program pgm
|
|
(a-program (exp1)
|
|
(value-of exp1 (init-env))))))
|
|
|
|
;; value-of : Exp * Env -> ExpVal
|
|
;; Page: 71
|
|
(define value-of
|
|
(lambda (exp env)
|
|
(cases expression exp
|
|
|
|
;;\commentbox{ (value-of (const-exp \n{}) \r) = \n{}}
|
|
(const-exp (num) (num-val num))
|
|
|
|
;;\commentbox{ (value-of (var-exp \x{}) \r) = (apply-env \r \x{})}
|
|
(var-exp (var) (apply-env env var))
|
|
|
|
;;\commentbox{\diffspec}
|
|
(diff-exp (exp1 exp2)
|
|
(let ((val1 (value-of exp1 env))
|
|
(val2 (value-of exp2 env)))
|
|
(let ((num1 (expval->num val1))
|
|
(num2 (expval->num val2)))
|
|
(num-val
|
|
(- num1 num2)))))
|
|
|
|
;;\commentbox{\zerotestspec}
|
|
(zero?-exp (exp1)
|
|
(let ((val1 (value-of exp1 env)))
|
|
(let ((num1 (expval->num val1)))
|
|
(if (zero? num1)
|
|
(bool-val #t)
|
|
(bool-val #f)))))
|
|
|
|
;;\commentbox{\ma{\theifspec}}
|
|
(if-exp (exp1 exp2 exp3)
|
|
(let ((val1 (value-of exp1 env)))
|
|
(if (expval->bool val1)
|
|
(value-of exp2 env)
|
|
(value-of exp3 env))))
|
|
|
|
;;\commentbox{\ma{\theletspecsplit}}
|
|
(let-exp (var exp1 body)
|
|
(let ((val1 (value-of exp1 env)))
|
|
(value-of body
|
|
(extend-env var val1 env))))
|
|
|
|
)))
|
|
|
|
|