Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
235 lines (200 sloc) 9.17 KB
#lang eopl
;; This code is a implementation of an extended version of the PROC language.
;; Environments.
(define empty-env-record? null?)
(define environment?
(lambda (x)
(or (empty-env-record? x)
(and (pair? x)
(symbol? (car (car x)))
(expval? (cadr (car x)))
(environment? (cdr x))))))
(define empty-env?
(lambda (x)
(empty-env-record? x)))
(define extended-env-record->sym
(lambda (r)
(car (car r))))
(define extended-env-record->val
(lambda (r)
(cadr (car r))))
(define extended-env-record->old-env
(lambda (r)
(cdr r)))
(define apply-env
(lambda (env search-sym)
(if (empty-env? env)
(eopl:error 'apply-env "No binding for ~s" search-sym)
(let ([sym (extended-env-record->sym env)]
[val (extended-env-record->val env)]
[old-env (extended-env-record->old-env env)])
(if (eqv? search-sym sym)
val
(apply-env old-env search-sym))))))
(define extended-env-record
(lambda (sym val old-env)
(cons (list sym val) old-env)))
(define extend-env
(lambda (sym val old-env)
(extended-env-record sym val old-env)))
(define empty-env-record
(lambda ()
'()))
(define empty-env
(lambda ()
(empty-env-record)))
(define init-env empty-env)
;; Data structures.
(define the-lexical-spec
'([whitespace (whitespace) skip]
[comment ("%" (arbno (not #\newline))) skip]
[identifier (letter (arbno (or letter digit "_" "-" "?"))) symbol]
[number (digit (arbno digit)) number]
[number ("-" digit (arbno digit)) number]))
(define the-grammar
'([program (expression) a-program]
[expression (number) const-exp]
[expression ("-" "(" expression "," expression ")") diff-exp]
[expression ("zero?" "(" expression ")") zero?-exp]
[expression ("if" expression "then" expression "else" expression) if-exp]
[expression (identifier) var-exp]
[expression ("let" identifier "=" expression "in" expression) let-exp]
[expression ("proc" "(" (separated-list identifier ",") ")" expression) proc-exp]
[expression ("traceproc" "(" (separated-list identifier ",") ")" expression) traceproc-exp]
[expression ("(" expression (arbno expression) ")") call-exp]))
(sllgen:make-define-datatypes the-lexical-spec the-grammar)
(define-datatype proc proc?
[procedure [vars (list-of symbol?)]
[body expression?]
[env environment?]]
[trace-procedure [vars (list-of symbol?)]
[body expression?]
[env environment?]])
(define-datatype expval expval?
[num-val [value number?]]
[bool-val [boolean boolean?]]
[proc-val [proc proc?]])
(define expval-extractor-error
(lambda (variant value)
(eopl:error 'expval-extractors "Looking for a ~s, found ~s"
variant value)))
(define expval->num
(lambda (v)
(cases expval v
[num-val (num) num]
[else (expval-extractor-error 'num v)])))
(define expval->bool
(lambda (v)
(cases expval v
[bool-val (bool) bool]
[else (expval-extractor-error 'bool v)])))
(define expval->proc
(lambda (v)
(cases expval v
[proc-val (proc) proc]
[else (expval-extractor-error 'proc v)])))
;; Helpers.
(define (filter-env env bound-vars exp)
(let loop ([result (empty-env)]
[bound-vars bound-vars]
[exp exp])
(cases expression exp
[const-exp (num) result]
[var-exp (var) (if (memv var bound-vars)
result
(extend-env var (apply-env env var) result))]
[diff-exp (exp1 exp2) (loop (loop result bound-vars exp1) bound-vars exp2)]
[zero?-exp (exp1) (loop result bound-vars exp1)]
[if-exp (exp1 exp2 exp3) (loop (loop (loop result
bound-vars
exp1)
bound-vars
exp2)
bound-vars
exp3)]
[let-exp (var exp1 body) (loop (loop result bound-vars exp1)
(cons var bound-vars)
body)]
[proc-exp (vars body) (loop result (append vars bound-vars) body)]
[traceproc-exp (vars body) (loop result (append vars bound-vars) body)]
[call-exp (rator rands) (let loop2 ([result (loop result bound-vars rator)]
[rands rands])
(if (null? rands)
result
(loop2 (loop result bound-vars (car rands))
(cdr rands))))])))
(define apply-procedure
(lambda (proc1 vals)
(cases proc proc1
[procedure (vars body saved-env) (value-of body
(let loop ([env saved-env]
[vars vars]
[vals vals])
(if (null? vars)
(if (null? vals)
env
(eopl:error 'apply-procedure "Too many arguments."))
(if (null? vals)
(eopl:error 'apply-procedure "Not enough arguments.")
(loop (extend-env (car vars) (car vals) env)
(cdr vars)
(cdr vals))))))]
[trace-procedure (vars body saved-env) (let ([env (let loop ([env saved-env]
[vars vars]
[vals vals])
(if (null? vars)
(if (null? vals)
env
(eopl:error 'apply-procedure "Too many arguments."))
(if (null? vals)
(eopl:error 'apply-procedure "Not enough arguments.")
(loop (extend-env (car vars) (car vals) env)
(cdr vars)
(cdr vals)))))])
(display "Entering procedure.")
(newline)
(let ([result (value-of body env)])
(display "Exiting procedure.")
(newline)
result))])))
;; Interpreter.
(define value-of
(lambda (exp env)
(cases expression exp
[const-exp (num) (num-val num)]
[var-exp (var) (apply-env env var)]
[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))))]
[zero?-exp (exp1) (let ([val1 (value-of exp1 env)])
(let ([num1 (expval->num val1)])
(if (zero? num1)
(bool-val #t)
(bool-val #f))))]
[if-exp (exp1 exp2 exp3) (let ([val1 (value-of exp1 env)])
(if (expval->bool val1)
(value-of exp2 env)
(value-of exp3 env)))]
[let-exp (var exp1 body) (let ([val1 (value-of exp1 env)])
(value-of body (extend-env var val1 env)))]
[proc-exp (vars body) (proc-val (procedure vars body (filter-env env vars body)))]
[traceproc-exp (vars body) (proc-val (trace-procedure vars body (filter-env env vars body)))]
[call-exp (rator rands) (let ([proc (expval->proc (value-of rator env))]
[args (map (lambda (rand)
(value-of rand env))
rands)])
(apply-procedure proc args))])))
;; Interfaces.
(define value-of-program
(lambda (pgm)
(cases program pgm
(a-program (exp1)
(value-of exp1 (init-env))))))
(define scan&parse
(sllgen:make-string-parser the-lexical-spec the-grammar))
(define run
(lambda (string)
(value-of-program (scan&parse string))))
(provide num-val bool-val proc-val run)