-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterp.rkt
63 lines (57 loc) · 1.29 KB
/
interp.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#lang racket
(provide interp interp-env interp-prim1)
(require "ast.rkt"
"env.rkt"
"interp-prims.rkt")
;; type Answer = Value | 'err
;; type Value =
;; | Integer
;; | Boolean
;; | Character
;; | Eof
;; | Void
;; | '()
;; | (cons Value Value)
;; | (box Value)
;; type REnv = (Listof (List Id Value))
;; Expr -> Answer
(define (interp e)
(interp-env e '()))
;; Expr Env -> Answer
(define (interp-env e r)
(match e
[(Int i) i]
[(Bool b) b]
[(Char c) c]
[(String s) s]
[(Eof) eof]
[(Empty) '()]
[(Var x) (lookup r x)]
[(Prim0 'void) (void)]
[(Prim0 'read-byte) (read-byte)]
[(Prim0 'peek-byte) (peek-byte)]
[(Prim1 p e)
(match (interp-env e r)
['err 'err]
[v (interp-prim1 p v)])]
[(Prim2 p e1 e2)
(match (interp-env e1 r)
['err 'err]
[v1 (match (interp-env e2 r)
['err 'err]
[v2 (interp-prim2 p v1 v2)])])]
[(If p e1 e2)
(match (interp-env p r)
['err 'err]
[v
(if v
(interp-env e1 r)
(interp-env e2 r))])]
[(Begin e1 e2)
(match (interp-env e1 r)
['err 'err]
[_ (interp-env e2 r)])]
[(Let x e1 e2)
(match (interp-env e1 r)
['err 'err]
[v (interp-env e2 (ext r x v))])]))