forked from alirezadaghigh99/PL-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.rkt
303 lines (264 loc) · 10.4 KB
/
interpreter.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
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
#lang racket
(require racket/include)
(require "environment.rkt")
(require "parser-lexer.rkt")
(require "operations.rkt")
(require (except-in eopl #%module-begin))
(define-namespace-anchor anc)
(define ns (namespace-anchor->namespace anc))
(define value-of
(lambda (tree env)
(let ((action (car tree)))
(cond
[(equal? action 'command)
(let ((result (value-of (cadr tree) env)))
(if (equal? (caddr result) 'END)
(list (car result) (cadr result) 'END)
(value-of (caddr tree) (cadr result)))
)]
[(equal? action 'keyword)
(let ((result (value-of (cadr tree) env)))
(if (equal? (caddr result) 'NOTEND)
(list (car result) (cadr result) 'NOTEND)
(value-of (cadr tree) (cadr result)))
)]
[(equal? action 'if_statement) (value-of (cadr tree) env)]
[(equal? action 'while_statement) (value-of (cadr tree) env)]
[(equal? action 'assignment_statement) (value-of (cadr tree) env)]
[(equal? action 'return_statement) (value-of (cadr tree) env)]
[(equal? action 'print_statement) (value-of (cadr tree) env)]
[(equal? action 'switch_statement) (value-of (cadr tree) env)]
[(equal? action 'if)
(let* ((exp-result (value-of (cadr tree) env))
(new-env (cadr exp-result)))
(if (car exp-result)
(value-of (caddr tree) new-env)
(value-of (cadddr tree) new-env))
)
]
[(equal? action 'while)
(let ((exp-result (value-of (cadr tree) env)))
(if (and (car exp-result) (not (eqv? (caddr exp-result) 'END)))
(let* ((new-env (cadr exp-result))
(command-result (value-of (caddr tree) new-env)))
(if (not (eqv? (caddr command-result) 'END))
(let ((condition-new-env (cadr command-result)))
(value-of tree condition-new-env))
command-result)
)
exp-result
)
)
]
[(equal? action 'return)
(let ((result (value-of (cadr tree) env)))
(list (car result) (cadr result) 'END)
)
]
[(equal? action 'assign)
(let ((result (value-of (caddr tree) env)))
(list (car result) (extend-env (cadr tree) (car result) (cadr result)) 'NOTEND)
)
]
[(equal? action 'print)
(let ((result (value-of (cadr tree) env)))
(display (car result))
(display "\n")
(list (car result) (cadr result) 'NOTEND)
)
]
[(equal? action 'switch)
(let* ((exp-val (car (value-of (cadr tree) env)))
(case-result (value-of (caddr tree) env))
(cases-list (caddr tree))
)
(let ((x (do ((case-result (value-of (caddr tree) env) (if (null? (cdddr cases-list)) (value-of cases-list env) (value-of (cadddr cases-list) env))) (cases-list (caddr tree) (if (null? (cdddr cases-list)) cases-list (cadddr cases-list))))
((or (equal? (car case-result) exp-val) (equal? (car cases-list) 'single-case)) (list cases-list case-result))
)))
(let* ((cases-list (car x))
(case-result (cadr x)))
(if (equal? (car cases-list) 'single-case)
(if (equal? (car case-result) exp-val)
(value-of (caddr cases-list) env)
(value-of (cadddr tree) env))
(value-of (caddr cases-list) env)
))))
]
[(equal? action 'single-case)
(let ((result (value-of (cadr tree) env)))
(list (car result) (cadr result) 'NOTEND)
)
]
[(equal? action 'multi-case)
(let ((result (value-of (cadr tree) env)))
(list (car result) (cadr result) 'NOTEND)
)
]
[(equal? action 'aexp) (value-of (cadr tree) env)]
[(equal? action 'greater?)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (greater? left-operand right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'less?)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (less? left-operand right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'equals?)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (equality? left-operand right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'not-equals?)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (inequality? left-operand right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'bexp) (value-of (cadr tree) env)]
[(equal? action 'subtract)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (arithmetic-op left-operand "-" right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'add)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (arithmetic-op left-operand "+" right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'cexp) (value-of (cadr tree) env)]
[(equal? action 'mul)
(cond
((equal? (car (value-of (cadr tree) env)) 0) (list 0 env 'NOTEND))
((equal? (car (value-of (cadr tree) env)) #f) (list #f 'NOTEND))
(else (list (arithmetic-op (car (value-of (cadr tree) env)) "*" (car (value-of (caddr tree) env))) (cadr (value-of (caddr tree) env)) 'NOTEND)))
]
[(equal? action 'div)
(let* ((valu (value-of (cadr tree) env))
(left-operand (car valu))
(new-env (cadr valu))
(val-left (value-of (caddr tree) env))
(right-operand (car val-left))
(second-new-env (cadr val-left)))
(list (arithmetic-op left-operand "/" right-operand) second-new-env 'NOTEND)
)
]
[(equal? action 'minus)
(let* ((valu (value-of (cadr tree) env))
(oper (car valu))
(new-env (cadr valu)))
(list (negate oper) new-env 'NOTEND)
)
]
[(equal? action 'par-exp) (value-of (cadr tree) env)]
[(equal? action 'number) (list (cadr tree) env 'NOTEND)]
[(equal? action 'null) (list '() env 'NOTEND)]
[(equal? action 'bool) (list (cadr tree) env 'NOTEND)]
[(equal? action 'string) (list (cadr tree) env 'NOTEND)]
[(equal? action 'var) (list (apply-env env (cadr tree)) env 'NOTEND)]
[(equal? action 'list) (value-of (cadr tree) env)]
[(equal? action 'element-of-list)
(let* ((result-indices (value-of (caddr tree) env))
(indices (car result-indices))
(new-env (cadr result-indices))
(input-list (apply-env env (cadr tree))))
(list (get-list-item input-list indices) new-env 'NOTEND)
)
]
[(equal? action 'list-values) (value-of (cadr tree) env)]
[(equal? action 'empty-list) (list '() env 'NOTEND)]
[(equal? action 'list-val)
(let ((result (value-of (cadr tree) env)))
(list (list (car result)) (cadr result) 'NOTEND)
)
]
[(equal? action 'list-vals)
(let* ((result-val (value-of (cadr tree) env))
(result-vals (value-of (caddr tree) (cadr result-val))))
(list (cons (car result-val) (car result-vals)) (cadr result-vals) 'NOTEND)
)
]
[(equal? action 'list-member)
(let ((result (value-of (cadr tree) env)))
(list (list (car result)) (cadr result) 'NOTEND)
)
]
[(equal? action 'list-members)
(let* ((result-val (value-of (cadr tree) env))
(result-vals (value-of (caddr tree) (cadr result-val))))
(list (cons (car result-val) (car result-vals)) (cadr result-vals) 'NOTEND)
)
]
))))
(define one-d-list-ref
(lambda (lst place)
(cond
[(not (list? lst)) (report-not-a-list lst)]
[(<= (length lst) place) (report-index-out-of-bound lst place)]
[else (if (= place 0)
(car lst)
(one-d-list-ref (cdr lst) (- place 1)))]
)
)
)
(define report-index-out-of-bound
(lambda (lst index)
(eopl:error 'apply-env "List ~s Out of Bounds for Index ~s" lst index)
)
)
(define report-not-a-list
(lambda (lst)
(eopl:error 'apply-env "~s Is Not a List" lst)
)
)
(define get-list-item
(lambda (input-list indices)
(if (zero? (length indices))
input-list
(get-list-item (one-d-list-ref input-list (car indices)) (cdr indices))
)
)
)
(define (while condition body)
(when condition
body
(while condition body)))
(define evaluate
(lambda (path)
(define input-string (file->string path))
(define lex-this (lambda (lexer input) (lambda () (lexer input))))
(define my-lexer (lex-this lexer_compiler (open-input-string input-string)))
(let
((parser-res (parser_compiler my-lexer)))
(car (value-of parser-res (empty-env))))))
(evaluate "a.txt")