-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.scm
285 lines (244 loc) · 11.2 KB
/
interpreter.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
; Written by Diego Waxemberg & Aaron Neyer
(load "classParser.scm")
(define base_env '())
(define class_env '(()))
(define currentclass 'none)
; main function, loops through the parse tree and calls functions to deal with the tuples.
(define interpret
(lambda (filename classname)
(if (string? classname) (set! classname (string->symbol classname)) 1)
(fixBool (call/cc (lambda (return)
(interpret-statement-list (append (parser filename) (list (list 'funcall (list 'dot classname 'main)))) base_env))))))
(define interpret-statement-list
(lambda (parsetree environment)
(cond
((null? parsetree) environment)
(else (interpret-statement-list (cdr parsetree) (interpret-statement (car parsetree) environment))))))
(define println
(lambda (todisplay)
(display todisplay)
(display "\n")))
(define interpret-statement
(lambda (stmt environment)
(cond
((operator? stmt 'throw) (throw-stmt stmt environment))
((operator? stmt 'var) (declare-stmt stmt environment))
((operator? stmt 'static-var) (declare-stmt stmt environment))
((operator? stmt 'class) (declare-class stmt environment))
((operator? stmt 'new) (declare-new stmt environment))
((operator? stmt '=) (begin (value stmt environment) environment))
((operator? stmt 'return) ((lookup 'return environment) (value (cadr stmt) environment)))
((operator? stmt 'while) (while-stmt stmt environment))
((operator? stmt 'break) ((lookup 'break environment) (cddr environment)))
((operator? stmt 'continue) ((lookup 'continue environment) environment))
((operator? stmt 'begin) (pop-stack (begin-stmt stmt environment)))
((operator? stmt 'function) (function-stmt stmt environment))
((operator? stmt 'static-function) (function-stmt stmt environment))
((operator? stmt 'funcall) (let ((funcreturn (funcall-stmt stmt environment)))
(if (or (eq? (cadr stmt) 'main) (and (list? (cadr stmt)) (eq? (caddr (cadr stmt)) 'main))) funcreturn environment)))
((operator? stmt 'if) (if-stmt stmt environment))
(else (error (string-append "Unregonized statement: " (format "~a" (operator stmt))))))))
; throws an exception
(define throw-stmt
(lambda (stmt environment)
(error (cadr stmt))))
; declares a new instance
(define declare-new
(lambda (stmt environment)
(let ((env (cons (list (car stmt) (box class_env)) environment)))
(set-box! (lookupClass 'new env) (unbox (lookupClass (cadr stmt) env))) (display env))))
; declares a new class in the environment
(define declare-class
(lambda (stmt environment)
(set! currentclass (cadr stmt))
(let ((env (cons (list (cadr stmt) (box class_env)) environment)))
(begin (interpret-statement-list (cadddr stmt) env)
env))))
(define funcall-stmt
(lambda (stmt environment)
(define func (cadr stmt))
(define funcBody 'none)
(if (symbol? func) (set! funcBody (lookup func environment)) (set! funcBody (unbox (dotLookup (caddr func) environment (cadr func)))))
(call/cc (lambda (return)
(if (eq? (numArguments (cddr stmt)) (numArguments (car funcBody)))
(interpret-statement-list (cadr funcBody)
(declare-multiple (car funcBody)
(cddr stmt) (declare-continuation 'return return (add-stack environment))))
(error "Wrong number of arguments"))))))
(define numArguments
(lambda (args)
(cond
((null? args) 0)
((eq? (car args) '&) (numArguments (cdr args)))
(else (+ 1 (numArguments (cdr args)))))))
(define declare-multiple
(lambda (variables vals environment)
(cond
((null? variables) environment)
((eq? (car variables) '&) (declare-multiple (cddr variables) (cdr vals) (declare-continuation (cadr variables) (car vals) environment)))
(else (declare-multiple (cdr variables) (cdr vals) (declare-continuation (car variables) (value (car vals) environment) environment))))))
(define function-stmt
(lambda (stmt environment)
(declare (cadr stmt) (cddr stmt) environment)))
(define begin-stmt
(lambda (stmt environment)
(call/cc (lambda (continue)
(interpret-statement-list (cdr stmt) (declare-continuation 'continue continue (add-stack environment)))))))
(define add-stack
(lambda (environment)
(let ((env (lookupClass currentclass environment)))
(begin (set-box! env (cons '() (unbox env))) environment))))
(define pop-stack
(lambda (environment)
(let ((env (lookupClass currentclass environment)))
(begin (set-box! env (cdr (unbox env))) environment))))
(define if-stmt
(lambda (stmt environment)
(if (value (cadr stmt) environment)
(interpret-statement (caddr stmt) environment)
(if (null? (cdddr stmt))
environment
(interpret-statement (cadddr stmt) environment)))))
(define while-stmt
(lambda (stmt environment)
(call/cc (lambda (break) (while-loop (cadr stmt) (caddr stmt) (declare-continuation 'break break environment))))))
(define while-loop
(lambda (condition body env)
(if (value condition env)
(while-loop condition body (interpret-statement body env))
env)))
(define declare-stmt
(lambda (stmt environment)
(if (null? (cddr stmt))
(declare (cadr stmt) 'null environment)
(declare (cadr stmt) (value (caddr stmt) environment) environment))))
; checks if the expression is a keyword
(define operator?
(lambda (expr op)
(cond
((not (pair? expr)) #f)
(else (eq? (operator expr) op)))))
; changes #t and #f to true and false respectively
(define fixBool
(lambda (val)
(cond
((eq? val #t) 'true)
((eq? val #f) 'false)
(else val))))
; add a value declaration to the environment
(define declare
(lambda (name value environment)
(let ((env (lookupClass currentclass environment)))
(cond
((not (eq? (lookup name environment) 'none)) (error "You cannot redefine a variable!"))
(else (begin (set-box! env (cons (cons (list name (box value)) (car (unbox env))) (cdr (unbox env)))) environment))))))
; allows redeclaring of variables, for continuations
(define declare-continuation
(lambda (name value environment)
(let ((env (lookupClass currentclass environment)))
(begin (set-box! env (cons (cons (list name (box value)) (car (unbox env))) (cdr (unbox env)))) environment))))
; binds a value to a variable in the environment
(define bind
(lambda (name value environment)
(cond
((eq? (lookup name environment) 'none) (error "You must declare a variable before assigning it"))
(else (reassign name value environment)))))
(define reassign
(lambda (name value environment)
(begin
(set-box! (lookupBox name environment) value)
value)))
(define flatten-once
(lambda (l)
(cond
((null? l) l)
(else (append (car l) (flatten-once (cdr l)))))))
(define lookupClass
(lambda (name environment)
(cond
((null? environment) '())
((eq? (caar environment) name)(cadar environment))
(else (lookupClass name (cdr environment))))))
(define dotLookup
(lambda (name environment class)
(let ((env
(unbox (if (null? (lookupClass (if (eq? 'this class)
currentclass
class) environment))
(lookupClass (if (eq? 'this class)
currentclass
class) (flatten-once (unbox (lookupClass currentclass environment))))
(lookupClass (if (eq? 'this class)
currentclass
class) environment)))))
(_lookupBox name (flatten-once env)))))
; looks up a name in the environment and returns the value associated with it
(define lookup
(lambda (name environment)
(unbox (lookupBox name environment))))
(define lookupBox
(lambda (name environment)
(let ((env (unbox (lookupClass currentclass environment))))
(let ((lookupResult (unbox (_lookupBox name (flatten-once env)))))
(if (and (symbol? lookupResult) (not (eq? lookupResult 'none)) (not (eq? lookupResult 'null)))
(lookupBox lookupResult env)
(_lookupBox name (flatten-once env)))))))
(define _lookupBox
(lambda (name environment)
(cond
((null? environment) (box 'none))
((eq? (caar environment) name) (cadar environment))
(else (_lookupBox name (cdr environment))))))
(define lookupWithErr
(lambda (name environment)
(let ((val (lookup name environment)))
(cond
((eq? val 'none) (error "You must declare a variable before using it"))
((eq? val 'null) (error "You must assign a variable before using it"))
(else val)))))
; gets the operator portion of a tuple
(define operator car)
; gets the first operand portion of a tuple
(define operand1 cadr)
; gets the second operand portion of a tuple
(define operand2 caddr)
(define binaryOp
(lambda (f expr environment)
;(println expr)
;(println environment)
(f (value (operand1 expr) environment) (value (operand2 expr) environment))))
(define myand
(lambda (a b)
(and a b)))
(define myor
(lambda (a b)
(or a b)))
; recursively evaluates an expression and returns the resulting environment
(define value
(lambda (expr environment)
(cond
((operator? expr 'new) (unbox (lookupClass (cadr expr) environment)))
((operator? expr 'funcall) (funcall-stmt expr environment))
((operator? expr 'dot) (unbox (dotLookup (caddr expr) environment (cadr expr))))
((number? expr) expr)
((eq? expr 'true) #t)
((eq? expr 'false) #f)
((symbol? expr) (lookupWithErr expr environment))
((null? (cdr expr)) (value (car expr) environment))
((eq? (operator expr) '=) (bind (operand1 expr) (value (operand2 expr) environment) environment))
((eq? (operator expr) '+) (binaryOp + expr environment))
((and (eq? (length expr) 3) (eq? (operator expr) '-)) (binaryOp - expr environment))
((eq? (operator expr) '*) (binaryOp * expr environment))
((eq? (operator expr) '/) (binaryOp quotient expr environment))
((eq? (operator expr) '%) (binaryOp remainder expr environment))
((and (eq? (length expr) 2) (eq? (operator expr) '-)) (- (value (operand1 expr) environment)))
((eq? (operator expr) '>) (binaryOp > expr environment))
((eq? (operator expr) '<) (binaryOp < expr environment))
((eq? (operator expr) '>=) (binaryOp >= expr environment))
((eq? (operator expr) '<=) (binaryOp <= expr environment))
((eq? (operator expr) '==) (binaryOp eq? expr environment))
((eq? (operator expr) '!=) (not (eq? (value (operand1 expr) environment) (value (operand2 expr) environment))))
((eq? (operator expr) '!) (not (value (operand1 expr) environment)))
((eq? (operator expr) '&&) (binaryOp myand expr environment))
((eq? (operator expr) '||) (binaryOp myor expr environment))
(else (error (string-append "wat? " (format "~a" expr)))))))