-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_static.scm
422 lines (362 loc) · 9.37 KB
/
state_static.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
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
; _______________________________________________ _________
; / _____/\__ ___/ _ \__ ___/\_ _____// _____/
; \_____ \ | | / /_\ \| | | __)_ \_____ \
; / \ | |/ | \ | | \/ \
;/_______ / |____|\____|__ /____| /_______ /_______ /
; \/ \/ \/ \/
;----------------------------------------
; VARIABLES
;----------------------------------------
;Uses two manually syncronzied lists to hold variable - value pairs
;Stores variable declaration name
(define S '())
;----------------------------------------
; ABSTRACTIONS
;----------------------------------------
(define varList car)
(define valList cadr)
(define getThrowLayer caddr)
(define getThrow
(lambda ()
(getThrowLayer (topLayer S))
)
)
(define fxnList cadddr)
(define newLayer '(() () () ()))
(define topLayer car)
(define pushLayerList
(lambda (l)
(setState (cons l S))
)
)
(define pushLayer
(lambda ()
(pushLayerList newLayer)
)
)
(define popLayer
(lambda ()
(setState (subLayers S))
)
)
(define setTopLayer
(lambda (l)
(popLayer)
(pushLayerList l)
)
)
(define setTopLayerState
(lambda (l state)
(cons l (subLayers state))
)
)
(define firstElement car)
(define subLayers cdr)
(define layer
(lambda (var val throw fxn)
(list var val throw fxn)
)
)
(define fxn
(lambda (name args body)
(list name args body)
)
)
(define pushSubState
(lambda ()
(pushLayerList 'substate)
(pushLayer)
)
)
(define popSubState
(lambda ()
(setState (popSubStateHelper S))
)
)
(define popSubStateHelper
(lambda (state)
(cond
((null? state) state)
((eq? (topLayer state) 'substate) (subLayers state))
(else(popSubStateHelper (subLayers state)))
)
)
)
;----------------------------------------
; HELPER FUNCTIONS
;----------------------------------------
;(atom? x)
;Returns whether or not x is an atom.
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
(define substate?
(lambda (l)
(cond
((eq? l 'substate) #t)
(else #f)
)
)
)
;(varExists? n s)
;Returns whether or not a variable has been initialized.
(define varExistsLayer?
(lambda (x layer)
(cond
((null? layer) #f)
((substate? layer) #f)
((list? (varList layer)) (varExistsLayer? x (varList layer)))
((eq? x (car layer)) #t)
(else (varExistsLayer? x (cdr layer)))
)
)
)
;(varExists? x state)
;Returns whether or not a variable has been initialized.
(define varExists?
(lambda (x state)
(cond
((null? state) #f)
((varExistsLayer? x (topLayer state)) #t)
(else (varExists? x (subLayers state)))
)
)
)
(define varExistsSubState?
(lambda (x state)
(cond
((null? state) #f)
((substate? (topLayer state)) #f)
((varExistsLayer? x (topLayer state)) #t)
(else (varExistsSubState? x (subLayers state)))
)
)
)
(define fxnExistsLayer?
(lambda (name fxnlist)
(cond
((null? fxnlist) #f)
((eq? name (car (car fxnlist))) #t)
(else (fxnExistsLayer? name (cdr fxnlist)))
)
)
)
(define fxnExists?
(lambda (name state)
(cond
((null? state) #f)
((substate? (topLayer state)) (fxnExists? name (subLayers state)))
((fxnExistsLayer? name (fxnList (topLayer state))) #t)
(else (fxnExists? name (subLayers state)))
)
)
)
(define fxnExistsSubState?
(lambda (name state)
(cond
((null? state) #f)
((substate? (topLayer state)) #f)
((fxnExistsLayer? name (fxnList (topLayer state))) #t)
(else (fxnExists? name (subLayers state)))
)
)
)
;(removeIndex l i 0)
(define removeIndex
(lambda (l i c)
(cond
((= i c) (cdr l))
(else (cons (car l) (removeIndex (cdr l) i (+ c 1))))
)
)
)
;----------------------------------------
; STATE FUNCTIONS
;----------------------------------------
(define throw
(lambda (v)
(setTopLayer (layer (varList (topLayer S)) (valList (topLayer S)) v (fxnList (topLayer S))))
)
)
;(setState s v)
;Sets the S and V list to s and v respectively.
(define setState
(lambda (s)
(set! S s)
)
)
;(addVar n v)
;Adds a variable and a value (which can be nothing) to the lists.
(define addVar
(lambda (n v state)
(if (varExistsSubState? n state) (error "Variable already defined"))
(setTopLayer (layer (cons n (varList (topLayer state))) (cons v (valList (topLayer state))) (getThrowLayer (topLayer state)) (fxnList (topLayer state))))
)
)
;(setVar n v)
;Sets a defined variable to a value.
(define setVar
(lambda (n v state)
(cond
((varExistsSubState? n state) (setState (setVarHelper n v state)))
((varExistsLayer? n (getGlobalLayer state)) (setState (setGlobalVarHelper (setVarLayer n v (getGlobalLayer state)) state)))
(else (error "Variable not defined"))
)
)
)
(define setGlobalVarHelper
(lambda (layer state)
(cond
((null? (subLayers state)) (cons layer '()))
(else (cons (topLayer state) (setGlobalVarHelper layer (subLayers state))))
)
)
)
(define setVarHelper
(lambda (n v state)
(cond
((substate? (topLayer state)) (error "This condition should never occur."))
((varExistsLayer? n (topLayer state))
(cons (setVarLayer n v (topLayer state)) (subLayers state)))
(else (cons (topLayer state) (setVarHelper n v (subLayers state))))
)
)
)
(define setVarLayer
(lambda (n v layer)
(list (cons n (varList (removeVarLayer n layer))) (cons v (valList (removeVarLayer n layer))) (getThrowLayer layer) (fxnList layer))
;LAYER DOESNT WORK HERE FOR SOME REASON
)
)
;(removeVar n)
;Removes variable n from the lists.
(define removeVar
(lambda (n state)
(cond
((substate? (topLayer state)) (error "This shouldn't ever occur either."))
((eq? (removeVarLayer n (topLayer state)) 'error)
(cons (topLayer state) (removeVar n (subLayers state))))
((null? (subLayers state)) (list (removeVarLayer n (topLayer state))))
(else (setState (list (removeVarLayer n (topLayer state)) (subLayers state))))
)
)
)
(define getVarIndex
(lambda (n varlist i)
(cond
((eq? n (firstElement varlist)) i)
(else (getVarIndex n (cdr varlist) (+ i 1)))
)
)
)
(define removeVarLayer
(lambda (n layer)
(cond
((not (varExists? n layer)) 'error)
(else (list (removeIndex (varList layer) (getVarIndex n (varList layer) 0) 0) (removeIndex (valList layer) (getVarIndex n (varList layer) 0) 0) (getThrowLayer layer)))
)
)
)
;(getVar x state)
;Returns the value of variable x in state S, if it exists.
(define getVar
(lambda (x state)
(cond
((varExistsLayer? x (topLayer state)) (getVarLayer x (topLayer state)))
((varExistsSubState? x (subLayers state)) (getVar x (subLayers state)))
((varExistsLayer? x (getGlobalLayer state)) (getVarLayer x (getGlobalLayer state)))
(else (error "Variable does not exist"))
)
)
)
(define getVarLayer
(lambda (x layer)
(cond
((getVarHelper x (varList layer) (valList layer)))
)
)
)
(define getVarHelper
(lambda (x var val)
(cond
((null? var) 'error)
((eq? x (car var)) (car val))
(else (getVarHelper x (cdr var) (cdr val)))
)
)
)
(define getGlobalLayer
(lambda (state)
(cond
((null? (subLayers state)) (topLayer state))
(else (getGlobalLayer (subLayers state)))
)
)
)
(define executingMain
(lambda (state)
(cond
((null? state) #f)
((eq? 'substate (topLayer state)) (executingMainHelper (subLayers state)))
(else (executingMain (subLayers state)))
)
)
)
(define executingMainHelper
(lambda (state)
(cond
((null? state) #t)
((eq? 'substate (topLayer state)) #f)
(else (executingMainHelper (subLayers state)))
)
)
)
;Clears ALL defined variables from the list
(define clearVars
(lambda ()
(setState '())
)
)
(define defineFxn
(lambda (name args body state)
(if (fxnExists? name state) (error "Function already defined"))
(setTopLayer (layer (varList (topLayer state)) (valList (topLayer state)) (getThrowLayer (topLayer state)) (cons (fxn name args body) (fxnList (topLayer state)))))
)
)
(define getFxnLayer
(lambda (name fxnlist)
(cond
((null? fxnlist) (error "Function not/no longer defined"))
((eq? name (car (car fxnlist))) (list (cadr (car fxnlist)) (caddr (car fxnlist))))
(else (getFxnLayer name (cdr fxnlist)))
)
)
)
(define getFxn
(lambda (name state)
(cond
((null? state) (error "Function not/no longer defined"))
((substate? (topLayer state)) (getFxn name (subLayers state)))
((fxnExistsLayer? name (fxnList (topLayer state))) (getFxnLayer name (fxnList (topLayer state))))
(else (getFxn name (subLayers state)))
)
)
)
;----------------------------------------
; LAYER FUNCTIONS
;----------------------------------------
(pushLayer)
(addVar 'x 5 S)
;(addVar 'w 3 S)
;(addVar 'y 6 S)
;(addVar 'z 7 S)
(pushLayer)
;(addVar 'w 3 S)
;(pushSubState)
(addVar 'w 3 S)
;(addVar 'v 9 S)
;(addVar 'w 8 S)
(setVar 'x 10 S)
(throw 'exception)
(defineFxn 'test '() '() S)
(defineFxn 'main '() '() S)