-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.scm
688 lines (638 loc) · 19.6 KB
/
state.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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
;define memory / set memory --------------
(define memory '())
;sets the memory to specified memory structure, and prints it
(define setMemory
(lambda (memoryStructure)
(set! memory memoryStructure)
(display memory)
(display "\n")
)
)
;structure defns / and abstractions for those structures ------------
;memory structure and abstractions
(define a_memoryStructure
(lambda (working defns)
(list working defns)
)
)
(define a_getWorkingMem car)
(define a_getClassDefns cadr)
;class structure for holding uninitialized classes
(define a_class
(lambda (type extends body)
(list type extends body)
)
)
(define a_getClassType car)
(define a_getClassExtends cadr)
(define a_getClassBody caddr)
;function structure for holding functions
(define a_fxn
(lambda (name args body)
(list name args body)
)
)
(define a_getFxnName car)
(define a_getFxnArgs cadr)
(define a_getFxnBody caddr)
;state structure - states are basically init classes
(define a_state
(lambda (name type layers)
(list name type layers)
)
)
(define a_getStateName car)
(define a_getStateType cadr)
(define a_getStateLayers caddr)
(define topState car)
(define subStates cdr)
(define a_getTopState topState)
;for our scoping implementation
(define a_currentState topState)
(define a_previousState cadr)
;layer abstractions
;layer structure
(define a_layer
(lambda (var val throw fxn)
(list var val throw fxn)
)
)
(define a_getLayerVars car)
(define a_getLayerVals cadr)
(define a_getLayerThrow caddr)
(define a_getLayerFxns cadddr)
;abstractions for layers
(define a_topLayer car)
(define a_subLayers cdr)
(define newLayer (a_layer () () () ()))
(define newLayers (cons newLayer '()))
;abstractions for different parts of the layer
(define a_varList car)
(define a_valList cadr)
(define a_throwVal caddr)
(define a_fxnList cadddr)
;simple layer functions
(define pushLayer
(lambda (layer)
(setMemory (a_memoryStructure (cons (pushLayerHelper layer (topState (a_getWorkingMem memory))) (subStates (a_getWorkingMem memory))) (a_getClassDefns memory)))
)
)
(define pushLayerHelper
(lambda (layer state)
(a_state (a_getStateName state) (a_getStateType state) (cons layer (a_getStateLayers state)))
)
)
(define popLayer
(lambda ()
(setMemory (a_memoryStructure (cons (popLayerHelper (topState (a_getWorkingMem memory))) (subStates (a_getWorkingMem memory))) (a_getClassDefns memory)))
)
)
(define popLayerHelper
(lambda (state)
(if (null? (cdr (a_getStateLayers state))) (error "popLayerHelper: cannot pop layer:" state))
(a_state (a_getStateName state) (a_getStateType state) (cdr (a_getStateLayers state)))
)
)
;marks off current execution as a substate, for entering functions
(define pushSubState
(lambda ()
(pushLayer 'substate)
(pushLayer newLayer)
)
)
(define popSubState
(lambda ()
(cond
((null? (a_topLayer (a_getStateLayers (topState (a_getWorkingMem memory))))) (error "popSubState: no subState to pop"))
((eq? (a_topLayer (a_getStateLayers (topState (a_getWorkingMem memory)))) 'substate) (popLayer))
(else (begin (popLayer) (popSubState)))
)
)
)
;Overall memory interactions
;(defineClass (class 'type 'extends 'body))
;this will define the structure of a class for later initialization
(define defineClass
(lambda (classObj)
(setMemory (a_memoryStructure (a_getWorkingMem memory) (cons classObj (a_getClassDefns memory))))
)
)
(define getClassBody
(lambda (type)
(getClassBodyHelper type (a_getClassDefns memory))
)
)
(define getClassBodyHelper
(lambda (type classDefns)
(cond
((null? classDefns) (error "getClassBodyHelper: class undefined:" type))
((eq? (a_getClassType (car classDefns)) type) (a_getClassBody (car classDefns)))
(else (getClassBodyHelper type (cdr classDefns)))
)
)
)
;returns the parent of specified type
(define getClassParent
(lambda (type)
(getClassParentHelper type (a_getClassDefns memory))
)
)
(define getClassParentHelper
(lambda (type classDefns)
(cond
((null? classDefns) '())
((eq? type 'this) (getClassParent (a_getStateName (a_currentState (a_getWorkingMem memory))))) ;more bandaids
((eq? (a_getClassType (car classDefns)) type) (a_getClassExtends (car classDefns)))
(else (getClassParentHelper type (cdr classDefns)))
)
)
)
;(defineState (state 'name 'class 'body)
;for init classes
(define defineState
(lambda (stateObj)
(setMemory (a_memoryStructure (cons stateObj (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
;I really don't think this works anymore and I hope isn't used by anything.
;Therefore, lets just assume is depreciated.
;sike its used, but I dont really know how or why
(define getStateType
(lambda (name)
(getStateTypeHelper name (a_getWorkingMem memory))
)
)
(define getStateTypeHelper
(lambda (name workingMem)
(cond
((null? workingMem) (error "getStateTypeHelper: State undefined:" name))
((eq? name 'this) (getStateType (a_getStateName (a_currentState (a_getWorkingMem memory))))) ;bandaid x2
((eq? (a_getStateName (car workingMem)) name) (a_getStateType (car workingMem)))
(else (getStateTypeHelper name (cdr workingMem)))
)
)
)
(define getStateLayers
(lambda (name)
(getStateLayersHelper name (a_getWorkingMem memory))
)
)
(define getStateLayersHelper
(lambda (name workingMem)
(cond
((null? workingMem) (error "getStateLayersHelper: State undefined:" name))
((eq? (a_getStateLayers (topState workingMem)) name) (a_getStateLayers (car workingMem)))
(else (getStateLayersHelper name (subStates workingMem)))
)
)
)
;for scoping implementation
(define getState
(lambda (state type)
(getStateWM state type (a_getWorkingMem memory))
)
)
(define getStateWM
(lambda (state type workingMem)
(cond
((null? workingMem) (error "getStateWM: State undefined: " state type))
((checkStateType state type workingMem) (topState workingMem))
(else (getStateWM state type (subStates workingMem)))
)
)
)
;for scoping implementation
(define removeState
(lambda (state type)
(setMemory (a_memoryStructure (removeStateWM state type (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
(define removeStateWM
(lambda (state type workingMem)
(cond
((null? workingMem) (error "removeStateWM: State undefined:" state type))
((checkStateType state type workingMem) (subStates workingMem))
(else (cons (topState workingMem) (removeStateWM state type (subStates workingMem))))
)
)
)
;for scoping implementation
(define setCurrentState
(lambda (state type)
(setMemory (a_memoryStructure (setCurrentStateWM state type (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
(define setCurrentStateWM
(lambda (state type workingMem)
(cons (getState state type) (removeStateWM state type workingMem))
)
)
;adds var to specified state and type
(define addVar
(lambda (name value state type)
(setMemory (a_memoryStructure (addVarWM name value state type (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
;this should return the ENTIRETY of workingMem, w modified state
(define addVarWM
(lambda (name value state type workingMem)
(cond
((null? workingMem) (error "addVarWM: State undefined:" name value state type))
((checkStateType state type workingMem) (cons (addVarWM2 name value (topState workingMem)) (subStates workingMem)))
(else (cons (topState workingMem) (addVarWM name value state type (subStates workingMem))))
)
)
)
;this should return the modified state
(define addVarWM2 ;fix setmemory
(lambda (n v state)
(a_state (a_getStateName state) (a_getStateType state) (addVarHelper n v (a_getStateLayers state)))
)
)
(define addVarHelper
(lambda (n v state)
(if (varExistsSubState? n state) (error "addVarHelper: Variable already defined:" n v))
(cons (a_layer (cons n (a_varList (a_topLayer state))) (cons v (a_valList (a_topLayer state))) (a_throwVal (a_topLayer state)) (a_fxnList (a_topLayer state))) (a_subLayers state))
)
)
(define setVar
(lambda (name value state type)
(setMemory (a_memoryStructure (setVarWM name value state type (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
;this should return the ENTIRETY of workingMem, w modified state
(define setVarWM
(lambda (name value state type workingMem)
(cond
((null? workingMem) (error "setVarWM: State undefined:" name value state type))
((checkStateType state type workingMem) (cons (setVarWM2 name value (topState workingMem)) (subStates workingMem)))
(else (cons (topState workingMem) (setVarWM name value state type (subStates workingMem))))
)
)
)
;this should return the modified state
(define setVarWM2
(lambda (name value state)
(a_state (a_getStateName state) (a_getStateType state) (setVarHelper name value (a_getStateLayers state)))
)
)
;this should return the modified layers
(define setVarHelper
(lambda (n v state)
(cond
((varExistsSubState? n state) (setVarSubstate n v state))
((varExistsLayer? n (getGlobalLayer state)) (setGlobalVarHelper (setVarLayer n v (getGlobalLayer state)) state)) ;check if global vars work right
(else (error "setVarHelper: Variable not defined:" n v))
)
)
)
(define setGlobalVarHelper
(lambda (layer state)
(cond
((null? (a_subLayers state)) (cons layer '()))
(else (cons (a_topLayer state) (setGlobalVarHelper layer (a_subLayers state))))
)
)
)
(define setVarSubstate
(lambda (n v state)
(cond
((varExistsLayer? n (a_topLayer state))
(cons (setVarLayer n v (a_topLayer state)) (a_subLayers state)))
(else (cons (a_topLayer state) (setVarSubstate n v (a_subLayers state))))
)
)
)
(define setVarLayer
(lambda (n v layer)
(a_layer (cons n (a_varList (removeVarLayer n layer))) (cons v (a_valList (removeVarLayer n layer))) (a_throwVal layer) (a_fxnList layer))
;a_layer used to cause errors and only list would work here - now it doesn't? if this causes errors, revert it to list
)
)
(define removeVar
(lambda (name state type)
(setMemory (a_memoryStructure (removeVarWM name state type (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
;this should return the ENTIRETY of workingMem, w modified state
(define removeVarWM
(lambda (name state type workingMem)
(cond
((null? workingMem) (error "removeVarWM: State undefined:" name state type))
((checkStateType state type workingMem) (cons (removeVarWM2 name (topState workingMem)) (subStates workingMem)))
(else (cons (topState workingMem) (removeVarWM name state type (subStates workingMem))))
)
)
)
;this should return modified state
(define removeVarWM2
(lambda (name state)
(a_state (a_getStateName state) (a_getStateType state) (removeVarHelper name (a_getStateLayers state)))
)
)
;returns modified layers
(define removeVarHelper
(lambda (n state)
(cond
((substate? (a_topLayer state)) (removeVarHelper n (a_subLayers state)))
((eq? (removeVarLayer n (a_topLayer state)) 'error)
(cons (a_topLayer state) (removeVarHelper n (a_subLayers state))))
((null? (a_subLayers state)) (list (removeVarLayer n (a_topLayer state))))
(else (cons (removeVarLayer n (a_topLayer state)) (a_subLayers state)))
)
)
)
(define removeVarLayer
(lambda (n layer)
(cond
((not (varExistsLayer? n layer)) 'error)
(else (a_layer (removeIndex (a_varList layer) (getVarIndex n (a_varList layer) 0) 0) (removeIndex (a_valList layer) (getVarIndex n (a_varList layer) 0) 0) (a_throwVal layer) (a_fxnList layer)))
)
)
)
(define checkVar
(lambda (name state type)
(checkVarWM name state type (a_getWorkingMem memory))
)
)
(define checkVarWM
(lambda (name state type workingMem)
(cond
((null? workingMem) #f)
((checkStateType state type workingMem) (checkVar2 name (topState workingMem)))
(else (checkVarWM name state type (subStates workingMem)))
)
)
)
(define checkVar2
(lambda (x state)
(varExists? x (a_getStateLayers state))
)
)
;will return var of a specified state USE CHECKVAR BEFORE GETTING
(define getVar
(lambda (name state type global)
(getVarWM name state type global (a_getWorkingMem memory))
)
)
(define getVarWM
(lambda (name state type global workingMem)
(cond
((null? workingMem) (error "getVarWM: State undefined:" name state type global))
((checkStateType state type workingMem) (getVar2 name global (topState workingMem)))
(else (getVarWM name state type global (subStates workingMem)))
)
)
)
(define getVar2
(lambda (x global state)
(getVarHelper x global (a_getStateLayers state))
)
)
(define getVarHelper
(lambda (x global state)
(cond
((and (eq? global 'global) (varExistsLayer? x (getGlobalLayer state))) (getVarLayer x (getGlobalLayer state)))
((varExistsLayer? x (a_topLayer state)) (getVarLayer x (a_topLayer state)))
((varExistsSubState? x (a_subLayers state)) (getVarHelper x global (a_subLayers state)))
((varExistsLayer? x (getGlobalLayer state)) (getVarLayer x (getGlobalLayer state)))
(else '()) ;requested that it returns an empty list if it cant find the var - to prevent execution termination
)
)
)
(define getVarLayer
(lambda (x layer)
(cond
((getVarHelper2 x (a_varList layer) (a_valList layer)))
)
)
)
(define getVarHelper2
(lambda (x var val)
(cond
((null? var) 'error)
((eq? x (car var)) (car val))
(else (getVarHelper2 x (cdr var) (cdr val)))
)
)
)
;fxn stuff
(define defineFxn
(lambda (fxnObj state type)
(setMemory (a_memoryStructure (defineFxnWM fxnObj state type (a_getWorkingMem memory)) (a_getClassDefns memory)))
)
)
;should return all states
(define defineFxnWM
(lambda (fxnObj state type workingMem)
(cond
((null? workingMem) (error "defineFxnWM: State undefined:" fxnObj state type))
((checkStateType state type workingMem) (cons (defineFxnWM2 fxnObj (topState workingMem)) (subStates workingMem)))
(else (cons (topState workingMem) (defineFxnWM fxnObj state type (subStates workingMem))))
)
)
)
;this should return modified state
(define defineFxnWM2
(lambda (fxnObj state)
(a_state (a_getStateName state) (a_getStateType state) (defineFxnHelper fxnObj (a_getStateLayers state)))
)
)
;returns modified layer(s)
(define defineFxnHelper
(lambda (fxnObj layer)
(cond
((null? (a_subLayers layer)) (list (a_layer (a_varList (a_topLayer layer)) (a_valList (a_topLayer layer)) (a_throwVal (a_topLayer layer)) (cons fxnObj (a_fxnList (a_topLayer layer))))))
((fxnExistsLayer? (a_getFxnName fxnObj) (a_topLayer layer)) (error "defineFxnHelper: Function already defined:" fxnObj layer))
(else (cons (a_topLayer layer) (defineFxnHelper fxnObj (a_subLayers layer))))
)
)
)
;returns specified function
(define getFxn
(lambda (name state type)
(getFxnWM name state type (a_getWorkingMem memory))
)
)
(define getFxnWM
(lambda (name state type workingMem)
(cond
((null? workingMem) (error "getFxnWM: State undefined:" name state type))
((checkStateType state type workingMem) (getFxn2 name (topState workingMem)))
(else (getFxnWM name state type (subStates workingMem)))
)
)
)
(define getFxn2
(lambda (x state)
(getFxnHelper x (a_getStateLayers state))
)
)
(define getFxnHelper
(lambda (x state)
(cond
((null? state) '())
((fxnExistsFxnList? x (a_fxnList (a_topLayer state))) (getFxnLayer x (a_fxnList (a_topLayer state))))
(else (getFxnHelper x (a_subLayers state)))
)
)
)
(define getFxnLayer
(lambda (name fxnlist)
(cond
((null? fxnlist) (error "getFxnLayer: Function not/no longer defined:" name fxnlist))
((eq? name (car (car fxnlist))) (list (a_getFxnArgs (car fxnlist)) (a_getFxnBody (car fxnlist)))) ;returns as (args body)
(else (getFxnLayer name (cdr fxnlist)))
)
)
)
;helper functions
(define checkStateType
(lambda (state type workingMem)
(cond
((eq? state 'this) (checkStateType (a_getStateName (topState workingMem)) type workingMem))
((and (eq? state (a_getStateName (topState workingMem))) (eq? type (a_getStateType (topState workingMem)))) #t)
(else #f)
)
)
)
;returns true if state exists
(define stateExists?
(lambda (state)
(stateExistsWM? state (a_getWorkingMem memory))
)
)
(define stateExistsWM?
(lambda (state workingMem)
(cond
((null? workingMem) #f)
((eq? state (a_getStateName (a_getTopState workingMem))) #t)
(else (stateExistsWM? state (subStates workingMem)))
)
)
)
(define getVarIndex
(lambda (n a_varList i)
(cond
((eq? n (car a_varList)) i)
(else (getVarIndex n (cdr a_varList) (+ i 1)))
)
)
)
;(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? x layers)
;Returns whether or not a variable has been initialized.
(define varExists?
(lambda (x layers)
(cond
((null? layers) #f)
((varExistsLayer? x (a_topLayer layers)) #t)
(else (varExists? x (a_subLayers layers)))
)
)
)
;(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? (a_varList layer)) (varExistsLayer? x (a_varList layer)))
((eq? x (car layer)) #t)
(else (varExistsLayer? x (cdr layer)))
)
)
)
(define getGlobalLayer
(lambda (state)
(cond
((null? (a_subLayers state)) (a_topLayer state))
(else (getGlobalLayer (a_subLayers state)))
)
)
)
(define varExistsSubState?
(lambda (x state)
(cond
((null? state) #f)
((substate? (a_topLayer state)) #f)
((varExistsLayer? x (a_topLayer state)) #t)
(else (varExistsSubState? x (a_subLayers state)))
)
)
)
(define fxnExists?
(lambda (name state)
(cond
((null? state) #f)
((substate? (a_topLayer state)) (fxnExists? name (a_subLayers state)))
((fxnExistsLayer? name (a_topLayer state)) #t)
(else (fxnExists? name (a_subLayers state)))
)
)
)
(define fxnExistsLayer?
(lambda (name layer)
(cond
((null? layer) #f)
((fxnExistsFxnList? name (a_fxnList layer)) #t)
(else #f)
)
)
)
(define fxnExistsFxnList?
(lambda (name fxnlist)
(cond
((null? fxnlist) #f)
((eq? name (car (a_getFxnName fxnlist))) #t)
(else (fxnExistsFxnList? name (cdr fxnlist)))
)
)
)
;(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))))
)
)
)
;this initializes the memory properly
(define initMem
(lambda ()
(setMemory (a_memoryStructure '() '()))
(display "state.scm initialized\n")
)
)
;basic function usage/testing
(initMem)
(defineClass (a_class 'x null '()))
(defineClass (a_class 'y 'x '()))
(defineState (a_state 'name 'x newLayers))
(defineState (a_state 'name2 'y newLayers))
(addVar 'x 5 'name 'x)
(pushSubState)
(addVar 'y 4 'name2 'y)
(pushLayer newLayer)
(setVar 'y 5 'name2 'y)
(getVar 'y 'name2 'y 'notglobal) ;should work, not really tested
(setVar 'x 4 'name 'x)
(removeVar 'y 'name2 'y)
(defineFxn (a_fxn 'name 'args 'body) 'name 'x)
(getFxn 'name 'name 'x)
(checkVar 'x 'name 'y)
(getVar 'x 'name 'x 'global)