Finish 2.rkt

This commit is contained in:
2025-04-24 09:41:05 -03:00
parent 6c1c698514
commit 185ac8aa81

View File

@ -8,6 +8,7 @@
[errorE (msg : String)] ;; New
[if0E (test : Exp) (then : Exp) (else : Exp)]
[minus1E (n : Exp)]
[timesE (lhs : Exp) (rhs : Exp)]
)
(define-type Value
@ -45,6 +46,11 @@
[doMinusK (v1 : Value)
(k : Continuation)]
[doMinus1K (k : Continuation)]
[timesSecondK (r : Exp)
(env : Env)
(k : Continuation)]
[doTimesK (v1 : Value)
(k : Continuation)]
[appArgK (arg-expr : Exp)
(env : Env)
(k : Continuation)]
@ -85,6 +91,7 @@
(case (s-exp->symbol (sx-ref sx 0))
[(+) (plusE (px 1) (px 2))]
[(-) (minusE (px 1) (px 2))]
[(*) (timesE (px 1) (px 2))]
[(if0) (if0E (px 1) (px 2) (px 3))]
[else (parse-error sx)])]
[else (parse-error sx)])))
@ -116,6 +123,7 @@
[(if0E test-expr then-expr else-expr)
(interp test-expr env (doIfK then-expr else-expr env k))]
[(minus1E n) (interp n env (doMinus1K k))]
[(timesE l r) (interp l env (timesSecondK r env k))]
))
(define (continue [k : Continuation] [v : Value]) : Value
@ -131,6 +139,10 @@
(continue next-k (arith-op - v1 v))]
[(doMinus1K next-k)
(continue next-k (arith-op - v (numV 1)))]
[(timesSecondK r env next-k)
(interp r env (doTimesK v next-k))]
[(doTimesK v1 next-k)
(continue next-k (arith-op * v1 v))]
[(appArgK arg-expr env next-k)
(interp arg-expr env (doAppK v next-k))]
[(doAppK fun-val next-k)