-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rkt
164 lines (138 loc) · 6.05 KB
/
solution.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#lang plait
(module+ test
(print-only-errors #t))
(define-type Exp
(varE [x : Symbol])
(numE [n : Number])
(binopE [e1 : Exp] [op : (Value Value -> Value)] [e2 : Exp])
(ifzE [e1 : Exp] [e2 : Exp] [e3 : Exp])
(letE [x : Symbol] [e1 : Exp] [e2 : Exp])
(funE [name : Symbol] [xs : (Listof Symbol)] [e : Exp])
(appE [name : Symbol] [es : (Listof Exp)])
(defineE [es : (Listof Exp)] [e : Exp]))
(define (parse [s : S-Exp]) : Exp
(local [(define (parse-body [s : S-Exp]) : Exp
(cond
[(s-exp-match? `SYMBOL s)
(varE (s-exp->symbol s))]
[(s-exp-match? `NUMBER s)
(numE (s-exp->number s))]
[(s-exp-match? `{ANY SYMBOL ANY} s)
(binopE
(parse-body (first (s-exp->list s)))
(parse-op (s-exp->symbol (second (s-exp->list s))))
(parse-body (third (s-exp->list s))))]
[(s-exp-match? `{ifz ANY then ANY else ANY} s)
(ifzE
(parse-body (second (s-exp->list s)))
(parse-body (fourth (s-exp->list s)))
(parse-body (list-ref (s-exp->list s) 5)))]
[(s-exp-match? `{let SYMBOL be ANY in ANY} s)
(letE
(s-exp->symbol (second (s-exp->list s)))
(parse-body (fourth (s-exp->list s)))
(parse-body (list-ref (s-exp->list s) 5)))]
[(s-exp-match? `{SYMBOL {ANY ...}} s)
(appE
(s-exp->symbol (first (s-exp->list s)))
(map parse-body (s-exp->list (second (s-exp->list s)))))]
[else (error 'parse (string-append "Syntax error: Expression other than that available in the body detected: " (to-string s)))]))
(define (parse-fun [s : S-Exp]) : Exp
(cond
[(s-exp-match? `{fun SYMBOL {SYMBOL ...} = ANY} s)
(funE
(s-exp->symbol (second (s-exp->list s)))
(map s-exp->symbol (s-exp->list (third (s-exp->list s))))
(parse-body (list-ref (s-exp->list s) 4)))]
[else (error 'parse (string-append "Syntax error: An expression other than function is detected: " (to-string s)))]))]
(if (s-exp-match? `{define {ANY ...} for ANY} s)
(defineE
(map parse-fun (s-exp->list (second (s-exp->list s))))
(parse-body (fourth (s-exp->list s))))
(error 'parse (string-append "Syntax error: An expression other than define is detected" (to-string s))))))
(define prim-ops '(+ - * <=))
(define (parse-op [op : Symbol])
(if (member op prim-ops)
(cond
[(eq? op '+) +]
[(eq? op '-) -]
[(eq? op '*) *]
[(eq? op '<=) (lambda (x y) (if (<= x y) 0 42))])
(error 'parse (string-append "Unknown operator: " (to-string op)))))
(define-type-alias Value Number)
(define-type Binding
(bind [name : Symbol]
[val : Value])
(bindF [name : Symbol]
[param : (Listof Symbol)]
[body : Exp]))
(define-type-alias Env (Listof Binding))
(define mt-env empty)
(define (extend-env [env : Env] [x : Symbol] [v : Value]) : Env
(cons (bind x v) env))
(define (extend-env-fun [env : Env] [x : Symbol] [xs : (Listof Symbol)] [f : Exp]) : Env
(cons (bindF x xs f) env))
(define (lookup-env [n : Symbol] [env : Env]) : Value
(type-case (Listof Binding) env
[empty (error 'lookup (string-append "Unbound variable: " (to-string n)))]
[(cons b rst-env)
(type-case Binding b
[(bind name val)
(if (eq? n name)
val
(lookup-env n rst-env))]
[else (lookup-env n rst-env)])]))
(define (lookup-env-fun [n : Symbol] [env : Env]) : ((Listof Symbol) * Exp)
(type-case (Listof Binding) env
[empty (error 'lookup (string-append "Undefined function: " (to-string n)))]
[(cons b rst-env)
(type-case Binding b
[(bindF name xs fun)
(if (eq? n name)
(pair xs fun)
(lookup-env-fun n rst-env))]
[else (lookup-env-fun n rst-env)])]))
(define (apply [xs : (Listof Symbol)] [vs : (Listof Value)] [env : Env]) : Env
(type-case (Listof Symbol) xs
[(cons x xs)
(apply xs (rest vs) (extend-env env x (first vs)))]
[empty (type-case (Listof Value) vs
[(cons x xs) (error 'apply "Incorrect number of parameters and arguments")]
[empty env])]))
(define (eval [e : Exp] [env : Env]) : Value
(local [(define global-env env)
(define (eval-body [e : Exp] [env : Env]) : Value
(type-case Exp e
[(numE n) n]
[(varE x)
(lookup-env x env)]
[(binopE e1 op e2)
(op (eval-body e1 env) (eval-body e2 env))]
[(ifzE e0 e1 e2)
(if (= (eval-body e0 env) 0)
(eval-body e1 env)
(eval-body e2 env))]
[(letE x e1 e2)
(let ([v1 (eval-body e1 env)])
(eval-body e2 (extend-env env x v1)))]
[(appE x es)
(let ([f (lookup-env-fun x global-env)])
(eval-body (snd f)
(apply (fst f)
(map (lambda (x) (eval-body x env)) es)
global-env)))]
[else (error 'eval "Syntax error: Expression other than that available in the body detected")]))
(define (initialize [es : (Listof Exp)] [env : Env]) : Env
(type-case (Listof Exp) es
[(cons e es)
(type-case Exp e
[(funE x xs e) (initialize es (extend-env-fun env x xs e))]
[else (error 'eval "Syntax error: An expression other than define is detected")])]
[else (begin
(set! global-env env)
env)]))]
(type-case Exp e
[(defineE ds e) (eval-body e (initialize ds env))]
[else (error 'eval "Syntax error: An expression other than define is detected")])))
(define (run [s : S-Exp]) : Value
(eval (parse s) mt-env))