-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scm
executable file
·304 lines (243 loc) · 6.59 KB
/
main.scm
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env gosh
(add-load-path "." :relative)
(use signal-handler)
(define prompt "espresso>> ")
(define gauche-apply apply)
(define gauche-eval eval)
; apply {{{
(define (apply procedure args)
(display "apply: ")
(print procedure)
(cond ((primitive-procedure? procedure)
(gauche-apply (cdr procedure) args))
((extend-procedure? procedure)
(extend-apply (cdr procedure) args))
(else (error "Unknown procedure -- APPLY" procedure))))
(define (extend-apply proc-info args)
(let ((proc-args (car proc-info))
(proc-body (caadr proc-info))
(proc-base-env (caddr proc-info)))
(let
((env (extend-frame (make-frame) proc-base-env)))
(define (define-loop variables values)
(if (null? variables)
'()
(begin
(define-variable! (car variables) (car values) env)
(define-loop (cdr variables) (cdr values)))))
(define-loop proc-args args)
(eval proc-body env))))
; }}}
; eval {{{
(define (eval exp env)
(display "eval: ")
(print exp)
(cond ((self-evaluating? exp) exp)
((variable? exp) (find-variable exp env))
((quoted? exp) (eval-quote exp))
((if? exp) (eval-if exp env))
((and? exp) (eval-and exp env))
((or? exp) (eval-or exp env))
((lambda? exp) (eval-lambda exp env))
((definition? exp) (eval-definition exp env))
((assignment? exp) (eval-assignment exp env))
((application? exp)
(apply (eval (operator exp) env)
(extract-operands (operands exp) env)))
(else
(error "EVAL ERROR" exp))))
; }}}
; detector {{{
(define (self-evaluating? exp)
(cond ((number? exp) #t)
((string? exp) #t)
(else #f)))
(define (variable? exp) (symbol? exp))
(define (quoted? exp)
(check-element? exp 'quote))
(define (if? exp)
(check-element? exp 'if)
)
(define (primitive-procedure? exp)
(check-element? exp 'primitive)
)
(define (extend-procedure? exp)
(check-element? exp 'procedure)
)
(define (definition? exp)
(check-element? exp 'define))
(define (assignment? exp)
(check-element? exp 'set!))
(define (lambda? exp)
(check-element? exp 'lambda))
(define (true? x)
(not (eq? x #f)))
(define (false? x)
(eq? x #f))
(define (check-element? exp tag)
(if (pair? exp)
(eq? (car exp) tag)
#f
))
(define (application? exp)
(pair? exp))
(define (and? exp)
(check-element? exp 'and))
(define (or? exp)
(check-element? exp 'or))
; }}}
(define (eval-quote exp)
(cadr exp))
(define (eval-if exp env)
(if (true? (eval (if-predicate exp) env))
(eval (if-consequent exp) env)
(eval (if-alternative exp) env)))
(define (if-predicate exp)
(cadr exp))
(define (if-consequent exp)
(caddr exp))
(define (if-alternative exp)
(if (not (null? (cdddr exp)))
(cadddr exp)
#f))
(define (eval-and exps env)
(define (condition-loop conditions)
(let ((ret (eval (car conditions) env)))
(if (null? (cdr conditions))
(if ret ret #f)
(if ret
(condition-loop (cdr conditions))
#f))))
(condition-loop (and-condition exps)))
(define (and-condition exps)
(cdr exps)
)
(define (eval-or exps env)
(define (condition-loop conditions)
(let ((ret (eval (car conditions) env)))
(if ret ret
(if (null? (cdr conditions))
#f
(condition-loop (cdr conditions))))))
(condition-loop (or-condition exps)))
(define (or-condition exps)
(cdr exps))
(define (eval-definition exp env)
(define-variable! (definition-variable exp)
(eval (definition-value exp) env) env))
(define (eval-assignment exp env)
(define (assignment-loop search-env)
(if (null? search-env) (error "Unbound variable -- SET!"))
(let ((frame (first-frame search-env))
(variable (definition-variable exp))
(value (eval (definition-value exp) env)))
(if (hash-table-exists? frame variable)
(add-binding-to-frame! variable value frame)
(assignment-loop (cdr search-env)))))
(assignment-loop env))
(define (definition-variable exp)
(if (symbol? (cadr exp))
(cadr exp)
(caadr exp)))
(define (definition-value exp)
(if (symbol? (cadr exp))
(caddr exp)
(make-lambda (func-param exp) (func-body exp))))
(define (func-param exp)
(cdr (car (cdr exp))))
(define (func-body exp)
(cdr (cdr exp)))
(define (make-lambda param body)
(cons 'lambda (cons param body)))
(define (define-variable! var val env)
(let ((frame (first-frame env)))
(add-binding-to-frame! var val frame)))
(define (eval-lambda exp env)
(list 'procedure (cadr exp) (cddr exp) env))
(define (find-variable var env)
(if (eq? env '()) (error "Unbound variable" var))
(let ((frame (first-frame env)))
(if (frame-variable-exists? frame var)
(frame-get frame var)
(find-variable var (cdr env)))))
(define (extract-operands exps env)
(if (null? exps)
'()
(cons (eval (car exps) env) (extract-operands (cdr exps) env))
))
(define (operator exp)
(car exp))
(define (operands exp)
(cdr exp))
; primitive {{{
(define primitive-procedures
'(
+
-
*
/
=
>
<
car
cdr
cons
eq?
)
)
(define (install-primitive-procedures frame)
(define (install procedures)
(if (null? procedures)
'()
(begin
(add-binding-to-frame!
(car procedures)
(cons 'primitive
(gauche-eval (car procedures) (interaction-environment))) frame)
(install (cdr procedures)))
)
)
(install primitive-procedures))
; }}}
; frame & environment {{{
(define empty-frame '())
(define (make-frame)
(make-hash-table))
(define (first-frame frame)
(car frame))
(define (frame-get frame var)
(hash-table-get frame var #f))
(define (frame-variable-exists? frame var)
(hash-table-exists? frame var))
(define (add-binding-to-frame! var val frame)
(hash-table-put! frame var val))
(define (extend-frame frame base-frame)
(cons frame base-frame))
(define (init-environment)
(let ((frame (make-frame)))
(install-primitive-procedures frame)
(add-binding-to-frame! 'true #t frame)
(add-binding-to-frame! 'false #f frame)
(extend-frame frame empty-frame)
))
(define global-environment (init-environment))
; }}}
; main {{{
(define (repl)
(display prompt)
(flush)
(let ((input (read)))
(if (eof-object? input)
(begin
(print "(╯°□°)╯︵ ┻━┻")
(exit)))
(let ((output (eval input global-environment)))
(print output)
(repl)
)
)
)
(define (main args)
(repl)
0)
; }}}