Hey, guys
I want to testing the code in page 72 in DrRacket, but I can't find the definition of scan&parse function before page 72.
After download the EOPL3's source code, I found scan&parse defined in chapter3\let-lang\lang.scm.
I tried to require the lang.scm in my code, but it seems that some code in lang.scm conflict with my testing code.
There's a 'program' in lang.scm:
(define the-grammar
'((program (expression) a-program)
...
But there's another similar definition in page 69:
(define-datatype program program?
(a-program
(exp1 expression?)))
I think I can solve this problem by spending a lot of time on modify and merge these files, but I don't think that's a good way to lean EOPL.
So I was wondering is there any easy way to debug the code in page 72?
Any help would be appreciated:)
My testing code was:
#lang eopl
(define-datatype program program?
(a-program
(exp1 expression?)))
(define-datatype expression expression?
(const-exp
(num number?))
(diff-exp
(exp1 expression?)
(exp2 expression?))
(zero?-exp
(exp1 expression?))
(if-exp
(exp1 expression?)
(exp2 expression?)
(exp3 expression?))
(var-exp
(var identifier?))
(let-exp
(var identifier?)
(exp1 expression?)
(body expression?)))
(define-datatype expval expval?
(num-val
(num number?))
(bool-val
(bool boolean?)))
(define empty-env
(lambda ()
'()))
(define extend-env
(lambda (var val env)
(list var val env)))
(define apply-env
(lambda (env search-var)
(cond
((null? env) (report-no-binding-found search-var))
((eqv? (car env) search-var) (cadr env))
(else
(apply-env (caddr env) search-var)))))
(define report-no-binding-found
(lambda (search-var)
(eopl:error 'apply-env "No binding for ~s" search-var)))
(define identifier? symbol?)
(define init-env
(lambda ()
(extend-env
'i (num-val 1)
(extend-env
'v (num-val 5)
(extend-env
'x (num-val 10)
(empty-env))))))
(define expval->num
(lambda (val)
(cases expval val
(num-val (num) num)
(else (eopl:error 'expval->num "expval num extractor error:~s" val)))))
(define expval->bool
(lambda (val)
(cases expval val
(bool-val (bool) bool)
(else (eopl:error 'expval->bool "expval bool extractor error:~s" val)))))
(define run
(lambda (string)
(value-of-program (scan&parse string))))
(define value-of-program
(lambda (pgm)
(cases program pgm
(a-program (exp1)
(value-of exp1 (init-env))))))
(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)))))))