Repository navigation
Expand file tree
/
Copy pathProject3Functions.rkt
More file actions
463 lines (409 loc) · 27.9 KB
/
Copy pathProject3Functions.rkt
File metadata and controls
463 lines (409 loc) · 27.9 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
;EECS345 Programming Project Part 2
;Professor Lewicki
;Project Partners: Peyton Turner (dpt14), Jack La Rue (jvl13), and Jessie Adkins (jsa70)
;4/12/17
;Language used: Pretty Big
(load "functionParser.scm")
(require racket/trace)
(include "HelperFunctions.rkt")
;General Project Notes:
;Empty states shall have the format: ((() ()))
;Takes a file from the local directory and, using simpleParser, generates a parse tree (in list format) of the text in the file.
;The parse tree modifies the text of the original document such that it is made suitable for scheme
;(for example, if one line was "x + y;", then simpleParser would generate said expression as (+ x y).
;Evaluate is then called on each of these parsed expressions.
;Once return has been called throughout the evaluation of the parse tree,
;call/cc will automatically take the continuation return value and output it into the interactions pane.
(define interpret
(lambda (filename)
(let ([output (call/cc
(lambda (return_from_interpret)
((evaluate (first (parser filename)) (rest (parser filename)) (empty_state_box) return_from_interpret))))])
(cond
((eq? output #t) 'true)
((eq? output #f) 'false)
(else output)))))
;Takes the first expression, the rest of the parse tree, a boxed state, and the continuation return.
;If the first expression is null, an error is thrown, as a program cannot not have a return statement.
;If the rest of the program is null, then the first expression is passed on to M_State to be evaluated further.
;Otherwise, the function calls M_State on the first expression and recursively calls M_State
;on the rest of the expressions in rest_of_program.
(define evaluate
(lambda (first_line rest_of_program boxed_state master_return)
(cond
((null? first_line) (error "Program Completed Without A Return Statement"))
((null? rest_of_program) (begin
(M_state first_line boxed_state master_return
(lambda (x) (error "Called break outside of a loop"))
(lambda (y) (error "Called continue outside of a loop"))
(lambda (z) (error "Threw an exception outside of a try block")))
(M_state '(funcall main) boxed_state master_return
(lambda (x) (error "Called break outside of a loop"))
(lambda (y) (error "Called continue outside of a loop"))
(lambda (z) (error "Threw an exception outside of a try block")))))
(else (begin
(M_state first_line boxed_state master_return
(lambda (x) (error "Called break outside of a loop"))
(lambda (y) (error "Called continue outside of a loop"))
(lambda (z) (error "Threw an exception outside of a try block")))
(evaluate (first rest_of_program) (rest rest_of_program) boxed_state master_return))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;M_State, M_Value, and M_Boolean
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes an expression, a state in a box, and the continuations for return, break, continue, throw, and block handling,
;and updates the state in the box to the state after the input expression has been evaluated.
(define M_state
(lambda (expr boxed_state master_return break continue throw)
(cond
((atom? expr) )
((null? expr) )
;Handles the two possibilities of variable declaration, either assigning an empty list if now value is given, or the given value if a value is given.
((eq? (first expr) 'var) (if (null? (rest_rest expr))
(handle_declare (first_rest expr) '() boxed_state master_return break continue throw)
(handle_declare (first_rest expr) (first_rest_rest expr) boxed_state master_return break continue throw)))
((eq? (first expr) 'function) (handle_function_declare (rest expr) boxed_state master_return break continue throw))
((eq? (first expr) '=) (handle_assign (first_rest expr) (first_rest_rest expr) boxed_state master_return break continue throw))
((eq? (first expr) 'return) (return (M_value (first_rest expr) boxed_state master_return break continue throw) master_return))
((eq? (first expr) 'if) (if* (rest expr) boxed_state master_return break continue throw))
((eq? (first expr) 'while) (call/cc
(lambda (k)
(while (first_rest expr) (first_rest_rest expr) boxed_state master_return k continue throw))))
((eq? (first expr) 'break) (break (remove_top_layer boxed_state)))
((eq? (first expr) 'continue) (continue (remove_top_layer boxed_state)))
((eq? (first expr) 'begin) (handle_begin expr boxed_state master_return break continue throw))
((eq? (first expr) 'try) (handle_try expr boxed_state master_return break continue throw))
((eq? (first expr) 'throw) (handle_throw expr boxed_state master_return break continue throw))
((eq? (first expr) 'catch) (handle_catch expr boxed_state master_return break continue throw))
((eq? (first expr) 'finally) (handle_finally expr boxed_state master_return break continue throw))
((eq? (first expr) 'funcall) (handle_function_call (rest expr) boxed_state master_return break continue throw))
((eq? (first expr) 'static-function) (handle_function_declare (rest expr) boxed_state master_return break continue throw)))))
;Takes an expression, a state, and the continuation return and returns the value of the expression evaluated in the given state.
;The evaluated expressions in M_value use math operators (i.e. + _ * / %) to produce/declare/assign values. The expression may contain assignments.
(define M_value
(lambda (expr boxed_state master_return break continue throw)
(cond
((is_boolean? expr) expr)
((atom? expr) (if (number? expr)
expr
(if (or (equal? expr 'true) (equal? expr 'false))
(if (equal? expr 'true)
#t
#f)
(get_var_value_box expr boxed_state))))
((and (unary? expr) (eq? (first expr) '-)) (* -1 (M_value (first_rest expr) boxed_state master_return break continue throw)))
;Calling M_state and M_value here is likely causing some sort of horrific problem with throws, since the throw should happen in both, but will expend the continuation on the first.
((eq? (first expr) '=) (begin
(M_state expr boxed_state master_return break continue throw)
(M_value (first_rest_rest expr) boxed_state master_return break continue throw)))
((eq? (first expr) 'funcall) (handle_function_call (rest expr) boxed_state master_return break continue throw))
((is_math_op? expr) ((get_math_op expr)
(M_value (first_rest expr) boxed_state master_return break continue throw)
(M_value (first_rest_rest expr) boxed_state master_return break continue throw)))
((is_bool_op? expr) (M_boolean expr boxed_state master_return break continue throw))
(else (error "You somehow called M_value on something without a value.")))))
;Takes an expression, a state, and the continuation return and returns the boolean value of the expression evaluated in the given state.
;The evaluated expressions in M_boolean use boolean operations (i.e. >, <, !=, ==, >=, <=, &&, ||, !) to produce/declare/assign values.
;The expression may contain assignments
(define M_boolean
(lambda (expr boxed_state master_return break continue throw)
(cond
((is_boolean? expr) expr)
((atom? expr) (if (or (equal? expr 'true) (equal? expr 'false))
(if (equal? expr 'true)
#t
#f)
(get_var_value_box expr boxed_state)))
((and (unary? expr) (eq? (first expr) '!)) (not (M_boolean (first_rest expr) boxed_state master_return break continue throw)))
((eq? (first expr) '=) (M_boolean (first_rest_rest expr) boxed_state master_return break continue throw))
((is_bool_op? expr) ((get_bool_op expr) (M_value (first_rest expr) boxed_state master_return break continue throw)
(M_value (first_rest_rest expr) boxed_state master_return break continue throw)))
(else (error "You somehow called M_boolean on something without a boolean value.")))))
;Takes a list of variables/values and the inputs to M_value and returns a list of M_value called on each of the elements of the input list.
(define M_value_list
(lambda (inputs boxed_state master_return break continue throw)
(cond
((null? inputs) '())
(else (cons (M_value (first inputs) boxed_state master_return break continue throw) (M_value_list (rest inputs) boxed_state master_return break continue throw))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Declare
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a variable, a list containing a value, a state in a box, and the continuation return value and updates the state in the box to be
;the state where the variable has been declared in the top layer in a box. If it is being declared but not initialized, use value ().
(define handle_declare
(lambda (var value boxed_state master_return break continue throw)
(let ([state (unbox boxed_state)])
(cond
((null? value) (set-box! boxed_state (cons (newfirsts var (box value) (first state)) (rest state))))
(else (set-box! boxed_state (cons (newfirsts var (box (M_value value boxed_state master_return break continue throw)) (first state)) (rest state))))))))
;Takes a function expression (name (parameters) (body)) and binds it in the state.
;The binding of a function looks much like the binding of a variable,
;but instead of the name being bound to a value in a box, the bound box contains (1) a list of parameters,
;(2) the state at the time of binding (which, since it is composed of boxes, will be updated
;as the values of variables in the state are updated, (3) and the body of the function.
(define handle_function_declare
(lambda (func boxed_state master_return break continue throw)
(let ([state (unbox boxed_state)] [name (get_function_name func)] [parameters (get_function_params func)] [body (get_function_body func)])
(set-box! boxed_state (cons (newfirsts name (box (list parameters state body)) (first state)) (rest state))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Assign
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a variable, an expression, a state in a box, and the continuation returns and
;updates the boxed state to the state where the variable is assigned to the value
;of the expression if the variable is declared. Otherwise creates an error.
(define handle_assign
(lambda (var expr boxed_state master_return break continue throw)
(update_box var (M_value expr boxed_state master_return break continue throw) boxed_state)))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Return, If, and While
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes an expression, a state, and the current continuation return value.
;The method then returns the value of the expression in the state. Once return is called, the program
;terminates, as the final value of the continuation return has been found.
(define return
(lambda (value master_return)
(master_return value)))
;Takes an expression, containing a condition, a boxed state and the M_state contitions.
;If the condition is true in the state, the M_state is called on the first expression after the condition.
;If the condition is false and the second expression after the condition is not null,
;then M_state is called on the second expression.
(define if*
(lambda (expr boxed_state master_return break continue throw)
(letrec ([truth (M_boolean (first expr) boxed_state master_return break continue throw)])
(cond
((eq? truth #t) (M_state (first_rest expr) boxed_state master_return break continue throw))
((and (eq? truth #f) (not (eq? (rest_rest expr) '()))) (M_state (first_rest_rest expr) boxed_state master_return break continue throw))))))
;Takes a condition, a loop body, a boxed state, and the M_state conditions.
;If the condition is true in the state, it recursively calls itself on the condition, the loop body, the continuations,
;and the state, after M_state has been called on the loop body.
(define while
(lambda (condition loop boxed_state master_return break continue throw)
(letrec ([truth (M_boolean condition boxed_state master_return break continue throw)])
(cond
((eq? truth #t) (begin (call/cc (lambda (k) (M_state loop boxed_state master_return break k throw))) (while condition loop boxed_state master_return break continue throw)))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Try
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes an expression in the form of a try statement and the M_state continuations and calls M_state on the body of the try block.
;Catches exceptions raised and stores them as a variable, then calls M_state on the catch and finally blocks.
;If no exception is raised, calls M_state on the finally block.
(define handle_try
(lambda (expr boxed_state master_return break continue throw)
(letrec ([result (call/cc (lambda (k) (try_helper (first_rest expr) boxed_state master_return break continue k)))])
(cond
((eq? result (void)) (M_state (first (rest_rest_rest expr)) boxed_state master_return break continue throw))
(else
;I have made the implementation decision that exceptions are declared as variables.
;Giving an exception a name that a variable already has will produce an error.
;In principle a user could access an exception outside of the catch block in which it was created.
(handle_declare (first_first_rest (first_rest_rest expr)) result boxed_state master_return break continue throw)
(M_state (first_rest_rest expr) boxed_state master_return break continue throw)
(M_state (first (rest_rest_rest expr)) boxed_state master_return break continue throw))))))
;Recursively calls M_state on the first entry of the input expression until it has exhausted the list
(define try_helper
(lambda (expr boxed_state master_return break continue throw)
(cond
((null? expr) (void))
(else
(M_state (first expr) boxed_state master_return break continue throw)
(try_helper (rest expr) boxed_state master_return break continue throw)))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Catch
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes an expression in the form of a catch statement and the M_state continuations and calls M_state on the body of the catch block.
(define handle_catch
(lambda (expr boxed_state master_return break continue throw)
(cond
((null? expr) (void))
(else (catch_helper (first_rest_rest expr) boxed_state master_return break continue throw)))))
;Recursively calls M_state on the first entry of the input expression until it has exhausted the list
(define catch_helper
(lambda (expr boxed_state master_return break continue throw)
(cond
((null? expr) (void))
(else
(M_state (first expr) boxed_state master_return break continue throw)
(catch_helper (rest expr) boxed_state master_return break continue throw)))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Finally
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes an expression in the form of a finally statement and the M_state continuations and calls M_state on the body of the finally block.
(define handle_finally
(lambda (expr boxed_state master_return break continue throw)
(cond
((null? expr) )
(else (finally_helper (first_rest expr) boxed_state master_return break continue throw)))))
;Recursively calls M_state on the first entry of the input expression until it has exhausted the list
(define finally_helper
(lambda (expr boxed_state master_return break continue throw)
(cond
((null? expr) )
(else
(M_state (first expr) boxed_state master_return break continue throw)
(finally_helper (rest expr) boxed_state master_return break continue throw)))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Throw
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes an expression and the M_state continuations and passes the value of the expression to the throw continuation
(define handle_throw
(lambda (expr boxed_state master_return break continue throw)
(throw (M_value (first_rest expr) boxed_state master_return break continue throw))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Functions
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a function, given by a name, a first line, and the rest of the function, and evaluates it in the input state.
;Returns to function_return, throws to throw.
(define evaluate_function
(lambda (name first_line rest_of_function boxed_state function_return throw)
(cond
((and (null? first_line) (eq? name 'main)) (error "Main Function Completed Without A Return Statement"))
((null? rest_of_function) (M_state first_line boxed_state function_return
(lambda (x) (error "Called break outside of a loop"))
(lambda (y) (error "Called continue outside of a loop"))
throw))
(else (begin
(M_state first_line boxed_state function_return
(lambda (x) (error "Called break outside of a loop"))
(lambda (y) (error "Called continue outside of a loop"))
throw)
(evaluate_function name (first rest_of_function) (rest rest_of_function) boxed_state function_return throw))))))
;Takes a function of the form (name parameters) and returns the result of evaluating it.
(define handle_function_call
(lambda (func boxed_state master_return break continue throw)
(letrec (
[name (first func)]
[parameter_values (M_value_list (rest func) boxed_state master_return break continue throw)]
[function_info (get_var_value_box name boxed_state)]
[parameter_names (first function_info)]
[scope (first_rest function_info)]
[body (first_rest_rest function_info)])
(if (atom? function_info)
(error "Attempted to reference a variable as a function.")
;Main gets called with the master return, other functions get called with the call/cc return.
(if (eq? name 'main)
(evaluate_function name (first body) (rest body) (bind_parameters_in_scope parameter_names parameter_values name function_info scope) master_return throw)
(call/cc
(lambda (return_from_function)
(evaluate_function name (first body) (rest body) (bind_parameters_in_scope parameter_names parameter_values name function_info scope) return_from_function throw))))))))
;Takes a list of parameter names, a list of parameter values, and the scope of a function.
;If the parameter lists are the same name, put each parameter value into a box,
;and add a new layer to the scope containing each parameter name bound to the box containing its respective value.
;Otherwise error.
;NOTE: Functions need to exist within their own scope so that they can recursively call themselves.
(define bind_parameters_in_scope
(lambda (parameter_names parameter_values name function_info scope)
(if (eq? (length parameter_names) (length parameter_values))
(box (cons (list (cons name parameter_names) (cons (box function_info) (boxify parameter_values))) scope))
(error "Incorrect number of parameters in function."))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Boxes and States
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a list and returns a same-sized list of the list entries, but in boxes
(define boxify
(lambda (l)
(cond
((null? l) ())
(else (cons (box (first l)) (boxify (rest l)))))))
;Creates an empty program state in a box
(define empty_state_box
(lambda ()
(box '((() ())))))
;Creates an empty program state in the layers form
(define empty_state
(lambda ()
'((() ()))))
;Adds an empty layer to the top of the current state
(define new_block
(lambda (state)
(cons (first (empty_state)) state)))
;Takes a state in a box and adds an empty layer to the top of the state
(define new_block_box
(lambda (state) (set-box! state (cons (first (empty_state)) (unbox state)))))
;Takes a boxed state and removes the top layer of said state, if it exists.
(define remove_top_layer
(lambda (state)
(cond
((null? (unbox state)) (error "Tried to remove a layer from the state, but no such layer exists!"))
(else (set-box! state (rest (unbox state)))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Var Value
;-----------------------------------------------------------------------------------------------------------------------------------------
;Gets the value of a variable possibly stored in a layer of a state stored in a box. Returns (boolean value),
;where boolean is true and value is the var's value if the var was present and boolean is false otherwise.
(define get_var_value_box
(lambda (var boxed_state)
(let ([state (unbox boxed_state)])
(cond
((null? state) (error "Attempting to use a variable or function not declared in the current scope."))
((first (get_var_value_layer var (first state))) (first_rest (get_var_value_layer var (first state))))
(else (get_var_value var (rest state)))))))
;Helper of get_var_value_box
;Gets the value of a given varable in a given layer state, or errors if no such variable exists.
(define get_var_value
(lambda (var state)
(cond
((null? state) (begin (write var) (error "Attempting to use a variable or function not declared in the current scope.")))
((first (get_var_value_layer var (first state))) (first_rest (get_var_value_layer var (first state))))
(else (get_var_value var (rest state))))))
;Helper of get_var_value_box
;Gets the value of a variable possibly stored in a layer. Returns (boolean value),
;where boolean is true and value is the var's value if the var was present, and boolean is false otherwise
(define get_var_value_layer
(lambda (var layer)
(cond
((null? (first layer)) (list #f '()))
((eq? var (first_first layer)) (if (not (null? (unbox (first_first_rest layer))))
(list #t (unbox (first_first_rest layer)))
(error "Attempting to use unassigned variable.")))
(else (get_var_value_layer var (removefirsts layer))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Layers
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a variable and a layer and returns true if the variable is in the layer and false otherwise.
(define in_layer
(lambda (var layer)
(cond
((null? (first layer)) #f)
((eq? var (first_first layer)) #t)
(else (in_layer var (removefirsts layer))))))
;Takes a variable and a layer and returns the box bound to the variable name, or errors if no such box exists.
(define get_box_from_layer
(lambda (var layer)
(cond
((null? (first layer)) (error "A variable or function is being referenced that does not exist in the current scope."))
((eq? var (first_first layer)) (first_first_rest layer))
(else (get_box_from_layer var (removefirsts layer))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Update
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a variable, a value, and a state in a box and sets the value of that variable in the state in that box to be the given value.
;Works on layered states.
(define update_box
(lambda (var value boxed_state)
(let ([state (unbox boxed_state)])
(cond
((null? state) (error "A variable or function is being referenced that does not exist in the current scope."))
(else (update_state var value state))))))
;Takes a variable, a value, and a state and sets the value of that variable in the state to be the given value.
;Works on layer states.
(define update_state
(lambda (var value state)
(cond
((null? state) (error "A variable or function is being referenced that does not exist in the current scope."))
((in_layer var (first state)) (set-box! (get_box_from_layer var (first state)) value))
(else (update_state var value (rest state))))))
;-----------------------------------------------------------------------------------------------------------------------------------------
;Begin
;-----------------------------------------------------------------------------------------------------------------------------------------
;Takes a block expression, a boxed state, master return, break, continue and throw, and processes said block statement in the boxed state.
(define handle_begin
(lambda (expr boxed_state master_return break continue throw)
(begin (new_block_box boxed_state) (begin_helper expr boxed_state master_return break continue throw) (remove_top_layer boxed_state))))
;Helper method for handle_begin. Takes a list of expressions, a state with a new layer, master_return, break,
;continue and throw and processes the code block within the new layer of the boxed state.
(define begin_helper
(lambda (expr boxed_state master_return break continue throw)
(cond
((null? (rest expr)))
(else
(M_state (first_rest expr) boxed_state master_return break continue throw)
(begin_helper (rest expr) boxed_state master_return break continue throw)))))