forked from AdamKBeck/Java-ish-Interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state-manipulation.scm
1444 lines (1204 loc) · 70.8 KB
/
state-manipulation.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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Brett Johnson
; Adam Beck
; Daniel Grigsby
#lang racket
(provide (all-defined-out))
(require "expression-ops.scm")
(require "state-structs.scm")
(require "helpers.scm")
(require racket/base)
(require racket/trace)
; State-empty? checks if an empty state is encountered, or if the state contains no more elements (empty)
(define state-empty?
(lambda (state)
(cond
((or (null? state) (equal? state initstate)) #t)
(else #f))))
; Interpretation loop section
; Main evaluation of parse tree function
(define evaluate-parse-tree->retval_state
(lambda (program state)
(evaluate-parse-tree-with-cfuncs->retval_state program state empty-cfuncs)))
(define evaluate-parse-tree-with-cfuncs->retval_state
(lambda (program state cfuncsinstance)
(call/cc
(lambda (return)
(cond
; not all programs/ segments must end in return
; empty list should return the state (ie: at the end of an if statement's statements)
((null? program) (error "No program"))
((not (list? program)) (error "Invalid program syntax"))
(else (list '() (evaluate-statement-list->state program state
(cfuncs-update-return cfuncsinstance return)))))))))
; Evaluates a list of statements
(define evaluate-statement-list->state
(lambda (program state cfuncsinstance)
(cond
((null? program) state)
((not (list? program)) (error "Invalid program syntax"))
((pair? (program-head program))
(evaluate-statement-list->state (program-tail program)
(evaluate-statement->state (program-head program) state cfuncsinstance)
cfuncsinstance))
(else (error "Invalid statement list syntax")))))
; Returns state updated after evaluating pair
(define evaluate-statement->state
(lambda (arglist state cfuncsinstance)
(cond
((null? arglist) (error "Not a statement"))
((eq? 'return (get-upcoming-statement-name arglist))
((cfuncs-return cfuncsinstance)
(G-eval-atomic-statement->value_state (rest-of-return-statement arglist) state cfuncsinstance)))
((eq? 'continue (get-upcoming-statement-name arglist))
((cfuncs-continue cfuncsinstance) state))
((eq? 'var (get-upcoming-statement-name arglist))
(G-evaluate-var-declare-statement->state arglist state cfuncsinstance))
((eq? 'try (get-upcoming-statement-name arglist))
(G-evaluate-try-statement->state arglist state cfuncsinstance))
((eq? 'throw (get-upcoming-statement-name arglist))
((cfuncs-catch cfuncsinstance) state (get-value-from-pair (G-value-lookup->value_state (get-contents-of-throw arglist) state cfuncsinstance))))
((eq? 'while (get-upcoming-statement-name arglist))
(G-evaluate-while-statement->state arglist state cfuncsinstance))
((eq? 'if (get-upcoming-statement-name arglist))
(G-evaluate-if-statement->state arglist state cfuncsinstance))
((eq? 'begin (get-upcoming-statement-name arglist))
(G-remove-scope-from-state->state
(evaluate-statement-list->state
(rest-of-begin-statement arglist)
(G-add-empty-scope-to-state->state state)
cfuncsinstance)))
((eq? 'break (get-upcoming-statement-name arglist))
((cfuncs-break cfuncsinstance) (G-remove-scope-from-state->state state)))
((eq? 'function (get-upcoming-statement-name arglist))
(G-define-function->state (arglist-tail arglist) state cfuncsinstance))
(else (get-state-from-pair (G-eval-atomic-statement->value_state arglist state cfuncsinstance))))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; Function definition section
(define G-define-function->state
(lambda (arglist state cfuncsinstance)
(declare-function (get-function-name arglist) (get-function-formal-args arglist) (get-function-body arglist) state)))
; evaluates a declare function
(define declare-function
(lambda (function-name function-args function-body state)
(cond
((G-declared-in-stack-frame? function-name state)
(error "function already declared"))
(else (initialize-var->state function-name
(list function-args function-body)
state)))))
; Evaluates a function
(define G-eval-function->value_state
(lambda (name args state cfuncsinstance)
(cond
((and (dot-expr? name)
(eq? (dotted-class-instance (arglist-dot name)) 'this))
(let* ([evaled-function (eval-function-post-name-eval (dotted-class-call (arglist-dot name))
args
state
state
(get-base-class state cfuncsinstance)
#t
cfuncsinstance)]
[function-return (get-value-from-pair evaled-function)]
[function-state (get-state-from-pair evaled-function)])
(list function-return function-state)))
((and (dot-expr? name) (eq? (dotted-class-instance (arglist-dot name)) 'super))
(eval-function-post-name-eval (dotted-class-call (arglist-dot name))
args
state
state
(get-current-class state cfuncsinstance)
#f
cfuncsinstance))
; single case: funcall (funcall (dot obj X))
; non-single case: (funcall (dot (funcall (dot obj X)) Y))
; dotted-class-instance: 'a (the name of the instance
; dottedname: (a f) where f is the name of the call and a is the object
((and (dot-expr? name) (list? (dotted-class-instance (arglist-dot name))))
(let* ([evaled-instance-pair (G-eval-atomic-statement->value_state (dotted-class-instance (arglist-dot name))
state
cfuncsinstance)]
[evaled-instance (get-value-from-pair evaled-instance-pair)]
[evaled-state (push-variable-as-literal->state (find-highest-var name) evaled-instance (get-state-from-pair evaled-instance-pair))]
[dottedname (list (find-highest-var name) (dotted-class-call (arglist-dot name)))]
[evaled-function (eval-function-post-name-eval (dotted-class-call dottedname)
args
evaled-state
(construct-dotted-state dottedname evaled-state)
(get-current-class (construct-dotted-state dottedname evaled-state) cfuncsinstance)
#f
cfuncsinstance)]
[function-return (get-value-from-pair evaled-function)]
[function-state (get-state-from-pair evaled-function)])
(cond
((eq? (dotted-class-instance dottedname) '.temp) (list function-return (G-merge-states->state state function-state)))
(else (list function-return
(update-class-instance (dotted-class-instance dottedname) (extract-new-class-instance-state function-state) evaled-state))))))
((dot-expr? name)
(let* ([dottedname (arglist-dot name)]
[evaled-function (eval-function-post-name-eval (dotted-class-call dottedname)
args
state
(construct-dotted-state dottedname state)
(get-current-class (construct-dotted-state dottedname state) cfuncsinstance)
#t
cfuncsinstance)]
[function-return (get-value-from-pair evaled-function)]
[function-state (get-state-from-pair evaled-function)])
(list function-return (update-class-instance (dotted-class-instance dottedname) (extract-new-class-instance-state function-state) state))))
(else (eval-function-post-name-eval name args state state default-currentclass #t cfuncsinstance)))))
; Finds the highest rightmost variable in a chained dotted expression
(define find-highest-var
(lambda (dotexpr)
(cond
((atom? dotexpr) '.temp)
((list? (dotted-class-instance (arglist-dot dotexpr)))
(cond
((eq? (get-dotted-head (dotted-class-instance (arglist-dot dotexpr))) 'new) '.temp)
(else (find-highest-var (dotted-class-instance (arglist-dot dotexpr))))))
((eq? 'this (dotted-class-instance (arglist-dot dotexpr))) '.temp)
((eq? 'super (dotted-class-instance (arglist-dot dotexpr))) '.temp)
((eq? 'funcall (arglist-head dotexpr)) '.temp)
(else (dotted-class-instance (arglist-dot dotexpr))))))
(define get-dotted-head car)
; Gets the case class a state is in
(define get-base-class
(lambda (state cfuncsinstance)
(get-value-from-pair (G-value-lookup->value_state '.class state cfuncsinstance))))
; Gets the current class a state is in
(define get-current-class
(lambda (state cfuncsinstance)
(cond
((G-initialized? '.this state) (get-value-from-pair (G-value-lookup->value_state '.this state cfuncsinstance)))
(else (get-value-from-pair (G-value-lookup->value_state '.class state cfuncsinstance))))))
; Gets a superclass for a current class in a state
(define get-super-class
(lambda (currentclass state)
(cond
((null? state) default-currentclass)
((null? currentclass) default-currentclass)
((declared-in-scope? (get-variable-section-state (get-top-scope state)) '.class)
(cond
((and (eq? (get-value-section-top-head (get-value-section-state (get-top-scope state))) currentclass)
(G-initialized? '.class (get-tail-scope state)))
(get-value-from-pair (G-value-lookup->value_state '.class (get-tail-scope state) empty-cfuncs)))
(else (get-super-class currentclass (get-tail-scope state)))))
(else (get-super-class currentclass (get-tail-scope state))))))
(define get-value-section-top-head car)
(define default-currentclass '())
; Evaluate the actual function after the name as been evaluated
(define eval-function-post-name-eval
(lambda (name args state function-state current-class preserve-current-class cfuncsinstance)
(let* ([super-popped-state (G-add-empty-scope-to-state->state (G-push-stack-divider-to-state->state current-class
(G-pop-scope-to-function->state name
(if preserve-current-class
current-class
(get-super-class current-class state))
function-state)))]
[popped-state (push-variable-as-literal->state '.this
(if preserve-current-class
current-class
(get-super-class current-class state))
(G-add-empty-scope-to-state->state (G-push-stack-divider-to-state->state current-class
(G-pop-scope-to-function-or-class->state name
current-class
function-state))))]
[function-in-state (variable-value-lookup name super-popped-state)]
[evaluate-function-call (evaluate-parse-tree-with-cfuncs->retval_state (get-funcall-body function-in-state)
(G-add-arguments-to-state->state (get-funcall-args function-in-state)
(evaluate-actual-args args state cfuncsinstance)
popped-state)
(cfuncs-wipe-all-but-catch (cfuncs-update-catch cfuncsinstance
(lambda (s e)
((cfuncs-catch cfuncsinstance)
(G-merge-states->state function-state
(G-pop-to-stack-divider->state s))
e)))))])
(list (get-value-from-pair evaluate-function-call)
(G-merge-states->state function-state
(G-pop-to-stack-divider->state (get-state-from-pair evaluate-function-call)))))))
;(trace eval-function-post-name-eval)
(define evaluate-actual-args-for-state
(lambda (actual state cfuncsinstance)
(cond
((null? actual) state)
(else (evaluate-actual-args-for-state (rest-of-args actual)
(get-state-from-pair (G-eval-atomic-statement->value_state (head-of-args actual)
state
cfuncsinstance))
cfuncsinstance)))))
; Evaluates arguments
(define evaluate-actual-args
(lambda (actual state cfuncsinstance)
(cond
((null? actual) actual)
(else
(let* ([value-lookup (G-value-lookup->value_state (head-of-args actual) state cfuncsinstance)])
(cons
(get-value-from-pair value-lookup)
(evaluate-actual-args (rest-of-args actual)
(get-state-from-pair value-lookup)
cfuncsinstance)))))))
(define head-of-args car)
(define rest-of-args cdr)
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; Dot functions section
; Evaluates a dotted function
(define evaluate-dotted-function-name
(lambda (arglist state)
(dotted-class-call arglist)))
; constructs a dotted state
(define construct-dotted-state
(lambda (dotexpr state)
(add-class-instance-to-state (dotted-class-instance dotexpr) state)))
; Adds a class instance to a state
(define add-class-instance-to-state
(lambda (instancename state)
(append (G-get-instance-state instancename state)
(G-pop-to-class-level->state state))))
; extract a new class instance
(define extract-new-class-instance-state
(lambda (state)
(reverse (extract-new-class-instance-state-sub (get-tail-scope (reverse state))))))
; extrats a new class instance from a state
(define extract-new-class-instance-state-sub
(lambda (state)
(cond
((null? state) empty-extraction-state)
((is-top-scope-class-divider? state) empty-extraction-state)
(else (cons (get-top-scope state)
(extract-new-class-instance-state-sub (get-tail-scope state)))))))
(define empty-extraction-state '())
; Updates a class instance with a new instance state
(define update-class-instance
(lambda (instancename new-instance-state state)
(G-push-state->state instancename
(list (get-instance-value-front (get-value-from-pair (G-value-lookup->value_state instancename state empty-cfuncs)))
new-instance-state)
state)))
(define get-instance-value-front car)
; Pops a state to the class level
(define G-pop-to-class-level->state
(lambda (state)
(list (get-reversedstate-head (reverse state)))))
(define get-reversedstate-head car)
; Pushes a stack divider to a state
(define G-push-class-divider-to-state->state
(lambda (classname state)
(cons `((.cf) (,classname)) state)))
; Gets a valid state for a 'this' keyword call
(define get-valid-this-call-state
(lambda (state)
(cond
((G-state-has-stack-divider? state) (G-pop-to-stack-divider->state state))
(else state))))
; Determines if the top scope in a state is the stack divider
(define is-top-scope-class-divider?
(lambda (state)
(cond
((null? (get-variable-section-head (get-top-scope state))) #f)
(else (eq? (get-scope-variable-head (get-top-scope state)) '.cf)))))
; TODO add side effects
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; if statement section
; Returns the value yielded from an if statement and the updated state
(define G-evaluate-if-statement->state
(lambda (arglist state cfuncsinstance)
(let* ([eval-ifcond (G-eval-atomic-statement->value_state (get-if-cond arglist) state cfuncsinstance)])
(cond
; If the if condition is true, evaluate the statements inside of it.
((get-value-from-pair eval-ifcond)
(evaluate-statement-list->state (list (get-if-then arglist))
(get-state-from-pair eval-ifcond)
cfuncsinstance))
((has-else? arglist)
(evaluate-statement-list->state (list (get-if-else arglist))
(get-state-from-pair eval-ifcond)
cfuncsinstance))
(else (get-state-from-pair eval-ifcond))))))
; Checks if an arglist has an if-else branch
(define get-if-else
(lambda (arglist)
(cond
((not (has-else? arglist)) (error "no else statement"))
(else (get-args-after-if-else arglist)))))
; Checks if an arglist has an else branch
(define has-else?
(lambda (arglist)
(pair? (get-else-from-if-else arglist))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; try catch section
; Eval try statement
(define G-evaluate-try-statement->state
(lambda (arglist state cfuncsinstance)
(call/cc
(lambda (throw)
(evaluate-inner-try-statement arglist
state
(lambda (s)
(throw (G-remove-scope-from-state->state (evaluate-statement-list->state (get-finally-from-try arglist)
(G-add-empty-scope-to-state->state s)
cfuncsinstance))))
cfuncsinstance)))))
; Evaluate an inner try statement
(define evaluate-inner-try-statement
(lambda (arglist state finally cfuncsinstance)
(finally
(G-remove-scope-from-state->state (evaluate-statement-list->state (get-statements-from-try arglist)
(G-add-empty-scope-to-state->state state)
(cfuncs-update-catch cfuncsinstance
(lambda (s e)
(finally
(G-remove-scope-from-state->state (evaluate-statement-list->state
(get-statements-from-catch (get-catch-from-try arglist))
(G-push-state->state (get-exception-from-catch (get-catch-from-try arglist))
e
(G-add-empty-scope-to-state->state (G-remove-scope-from-state->state s)))
cfuncsinstance))))))))))
; Gets the catch statement from try
(define get-catch-from-try
(lambda (arglist)
(cond
((null? (get-catch-wrapper arglist)) '())
(else (get-inner-catch-statement (get-contents-of-catch arglist))))))
; Gets the finally statement from try
(define get-finally-from-try
(lambda (arglist)
(cond
((null? (get-finally-wrapper arglist)) '())
(else (get-inner-finally-statement (get-contents-of-finally arglist))))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; while loop section
; Returns the value yielded from a while statement and the updated state
(define G-evaluate-while-statement->state
(lambda (arglist state cfuncsinstance)
(call/cc
(lambda (break)
(evaluate-recursive-while arglist state (cfuncs-update-break cfuncsinstance break))))))
; Evaluate a while statement
(define evaluate-recursive-while
(lambda (arglist state cfuncsinstance)
(call/cc
(lambda (endcontinue)
(let* ([eval-while-cond (G-eval-atomic-statement->value_state (get-while-cond arglist) state cfuncsinstance)])
(cond
((get-value-from-pair eval-while-cond)
(evaluate-recursive-while arglist
(evaluate-statement-list->state (list (get-while-statement arglist))
(get-state-from-pair eval-while-cond)
(cfuncs-update-continue cfuncsinstance
(lambda (s)
(endcontinue (evaluate-recursive-while arglist
s
cfuncsinstance)))))
cfuncsinstance))
; If the while condition is false, return '() for the return value, and also return the updated state after evaluating the condition (side effects challenge)
(else (get-state-from-pair eval-while-cond))))))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; Variable declaration section
; Returns updated state after a declaration or initialization
(define G-evaluate-var-declare-statement->state
(lambda (arglist state cfuncsinstance)
(cond
((null? (arglist-tail arglist)) (error "Nothing after the var"))
((G-declared-in-stack-frame? (get-var-name-from-declare-args arglist) state)
(error "variable already declared" (get-var-name-from-declare-args arglist) state))
((only-declare? arglist) (declare-var->state (get-var-name-from-declare-args arglist) state))
(else (let* ([evaluate-assign (G-eval-atomic-statement->value_state (truncate-var-name-from-declare arglist) state cfuncsinstance)])
(initialize-var->state (get-var-name-from-declare-args arglist)
(get-value-from-pair evaluate-assign)
(get-state-from-pair evaluate-assign)))))))
; Adds an instance to the state. The value of the instance is '(type (instancestate))
; value is a list: e.g. (new A). Prereq: the name of the instance has not been declared in the current stack frame
(define get-instance-initialization-value
(lambda (value state)
(let* ([cn (get-instance-init-value-head value)])
(cond
(else (list (list 'classname cn)
(G-eval-class-closure->state cn state)))))))
(define get-instance-init-value-head cadr)
; Pushes the declaration statement to the state
(define declare-var->state
(lambda (name state)
(initialize-var->state name empty-value-for-declare state)))
(define empty-value-for-declare '())
; Pushes and initializes the variable to the state
(define initialize-var->state
(lambda (name value state)
(cons (append-head-scope-to-scope (list (list name)
(list value))
(get-top-scope state))
(get-tail-scope state))))
; Determines if an argument is a declare (and not an assign).
(define only-declare?
(lambda (arglist)
(null? (get-declare-from-assign arglist))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; atomic statement evaluator
; atomic statements are statements that are valid inside of a conditional statement/assignment statement or on their own
; at the moment, this is just assign statements and expressions
; returns a pair of (value, state)
; (e.g. (> x (+ y 1)) is an atomic statement)
; (e.g. (== 3 (= x (+ x 1)) is an atomic statement)
; Atomic statements can be put
(define G-eval-atomic-statement->value_state
(lambda (arglist state cfuncsinstance)
(cond
((single-atom? arglist) (G-value-lookup->value_state arglist state cfuncsinstance))
((single-value-list? arglist) (G-value-lookup->value_state (arglist-head arglist) state cfuncsinstance))
((dot-expr? arglist) (evaluate-dotted-expr->value_state (arglist-dot arglist) state cfuncsinstance))
((G-expr? arglist) (G-eval-expr->value_state arglist state cfuncsinstance))
((G-assign? arglist) (G-eval-assign->value_state arglist state cfuncsinstance))
((is-funcall? arglist) (eval-funcall->value_state (arglist-tail arglist) state cfuncsinstance))
((is-initialization? arglist) ; arglist = (new classname). Value should be '((classname name) state), state should be original state
(list (get-instance-initialization-value arglist state) state))
((raw-class-instance arglist) (list arglist state))
(else (error "not a valid atomic statement" arglist state)))))
(define raw-class-instance
(lambda (arglist)
(eq? (get-value-from-pair (get-value-from-pair arglist)) 'classname)))
; checks if an arglist is a dot-expression
(define dot-expr?
(lambda (arglist)
(cond
((not (list? arglist)) #f)
((not (eq? (length arglist) 3)) #f)
((eq? (arglist-head arglist) 'dot) #t)
(else #f))))
(define arglist-dot
(lambda (arglist)
(arglist-tail arglist)))
; evaluates a dotted expression
(define evaluate-dotted-expr->value_state
(lambda (arglist state cfuncsinstance)
(cond
((eq? (dotted-class-instance arglist) 'this)
(list (get-value-from-pair (G-value-lookup->value_state (dotted-class-call arglist)
(get-valid-this-call-state state)
cfuncsinstance))
state))
((eq? (dotted-class-instance arglist) 'super)
(list (get-value-from-pair (G-value-lookup->value_state (dotted-class-call arglist)
(get-tail-scope (get-valid-this-call-state state))
cfuncsinstance))
state))
(else
(list (get-value-from-pair (G-value-lookup->value_state (dotted-class-call arglist)
(G-get-instance-state (dotted-class-instance arglist) state)
cfuncsinstance))
state)))))
; FUNCALL Section
; eval function atomic statement section
(define is-funcall?
(lambda (arglist)
(eq? (arglist-head arglist) 'funcall)))
(define is-initialization?
(lambda (arglist)
(eq? (arglist-head arglist) 'new)))
; Evaluates the function call
(define eval-funcall->value_state
(lambda (arglist state cfuncsinstance)
(G-eval-function->value_state (get-function-name arglist) (get-function-actual-args arglist) state cfuncsinstance)))
; Determines if an argument is a single atom
(define single-atom?
(lambda (arglist)
(not (list? arglist))))
; Determines if an argument is of length 1
(define single-value-list?
(lambda (arglist)
(eq? (length arglist) 1)))
; Determines if an argument is an atomic statement
(define G-atomic-statement?
(lambda (arglist state)
(cond
((single-atom? arglist) #t)
((single-value-list? arglist) #t)
((G-expr? arglist) #t)
((G-assign? arglist) #t)
(else #f))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; eval-assign section
; this function evaluates assignment statements
; will returns value state pair
; (e.g. (= x 1) will return (1 (updated-state)))
(define G-eval-assign->value_state
(lambda (arglist state cfuncsinstance)
(let* ([evaluate-assign (G-eval-atomic-statement->value_state (get-arg2-from-expr arglist) state cfuncsinstance)])
(cond
((not (G-assign? arglist)) (error "not an assignment"))
((dot-expr? (get-arg1-from-expr arglist))
(evaluate-dotted-assign->value_state (arglist-dot (get-arg1-from-expr arglist))
(get-value-from-pair evaluate-assign)
(get-state-from-pair evaluate-assign)
cfuncsinstance))
((G-declared? (get-arg1-from-expr arglist) state)
(G-value-lookup->value_state (get-arg1-from-expr arglist)
(G-push-state->state (get-arg1-from-expr arglist)
(get-value-from-pair evaluate-assign)
(get-state-from-pair evaluate-assign)) cfuncsinstance))
(else (error "variable undeclared args:" arglist "state" state))))))
; Evaluates a dotted assignment expression
(define evaluate-dotted-assign->value_state
(lambda (dot-expression assign-value state cfuncsinstance)
(cond
((eq? (dotted-class-instance dot-expression) 'super)
(let* ([evaled-assign (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value)
(get-tail-scope (get-valid-this-call-state state))
cfuncsinstance)]
; tail-scope is called to remove .cf pointer
[evaled-state (get-state-from-pair evaled-assign)]
[evaled-value (get-value-from-pair evaled-assign)])
(list evaled-value (G-merge-states->state state evaled-state))))
((eq? (dotted-class-instance dot-expression) 'this)
(let* ([evaled-assign (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value)
(get-valid-this-call-state state)
cfuncsinstance)]
; tail-scope is called to remove .cf pointer
[evaled-state (get-state-from-pair evaled-assign)]
[evaled-value (get-value-from-pair evaled-assign)])
(list evaled-value (G-merge-states->state state evaled-state))))
(else
(update-class-instance (dotted-class-instance dot-expression)
(extract-new-class-instance-state (get-state-from-pair G-eval-assign->value_state
`(= ,(dotted-class-call dot-expression) ,assign-value)
(construct-dotted-state dot-expression state)
cfuncsinstance)))
state))))
; Determines whether or not an assignment argument is reached
(define G-assign?
(lambda (arglist)
(cond
((eq? (get-op-from-expr arglist) '=) #t)
(else #f))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; eval-expression section
; this function evaluates all expressions
; expressions are defined as built in math, boolean, or comparison operators
; expressions cover the scope of math expressions and boolean expressions (or conditional statements)
; Returns (value, updated-state)
(define G-eval-expr->value_state
(lambda (arglist state cfuncsinstance)
(cond
((not (G-expr? arglist)) (error "given invalid expression operation"))
((eq? (length arglist) 2)
(eval-expr-uni->value_state (get-op-from-expr arglist)
(get-arg1-from-expr arglist)
state
cfuncsinstance))
((eq? (length arglist) 3)
(eval-expr-multi->value_state (get-op-from-expr arglist)
(get-arg1-from-expr arglist)
(get-arg2-from-expr arglist)
state
cfuncsinstance))
(else (error "invalid number of arguments")))))
; this function evaluates all 1 argument expressions
; it currently only evaluates ints and booleans
(define eval-expr-uni->value_state
(lambda (op arg1 state cfuncsinstance)
(cond
((math-expr? op) (eval-math-expr-uni->value_state op arg1 state cfuncsinstance))
((boolean-expr? op) (eval-boolean-expr-uni->value_state op arg1 state cfuncsinstance))
(else (error "unsupported expression")))))
; this function evaluates booleans
; this function is for 1 argument boolean expressions
(define eval-boolean-expr-uni->value_state
(lambda (op arg1 state cfuncsinstance)
(cond
((eq? (G-type-lookup arg1 state cfuncsinstance) 'boolean)
(cons ((boolean-operator-to-function-uni op)
(get-value-from-pair (G-value-lookup->value_state arg1 state cfuncsinstance)))
(list (get-state-from-pair (G-value-lookup->value_state arg1 state cfuncsinstance)))))
(else (error "boolean operator not valid for non boolean types")))))
; this function evaluates math expressions
; it currently only supports integers
; this function is for 1 argument math expressions
(define eval-math-expr-uni->value_state
(lambda (op arg1 state cfuncsinstance)
(cond
((eq? (G-type-lookup arg1 state cfuncsinstance) 'integer)
(eval-math-expr-int-uni->value_state op arg1 state cfuncsinstance))
(else (error "invalid type for math expression")))))
; this function evaluates math expressions of integers
; this function is for 1 argument math expressions
(define eval-math-expr-int-uni->value_state
(lambda (op arg1 state cfuncsinstance)
(let* ([lookup-arg1 (G-value-lookup->value_state arg1 state cfuncsinstance)])
(cons ((math-operator-to-function-uni op #t)
(get-value-from-pair lookup-arg1))
(list (get-state-from-pair lookup-arg1))))))
; this function evaluates all 2 argument expressions
; it currently only evaluates ints and booleans
(define eval-expr-multi->value_state
(lambda (op arg1 arg2 state cfuncsinstance)
(cond
((compare-expr? op) (eval-compare-expr-multi->value_state op arg1 arg2 state cfuncsinstance))
((math-expr? op) (eval-math-expr-multi->value_state op arg1 arg2 state cfuncsinstance))
((boolean-expr? op) (eval-boolean-expr-multi->value_state op arg1 arg2 state cfuncsinstance))
(else (error "unsupported expression")))))
; this function evaluates comparisons
; this function is for 2 argument comparison expressions
(define eval-compare-expr-multi->value_state
(lambda (op arg1 arg2 state cfuncsinstance)
; We return a (value, state), hence the cons for the value and the state
; The value is derived from applying the operator on arg1 and arg2
; To handle side effects, the state passed into arg2 is the state after evaluating arg1
(let* ([lookup-arg1 (G-value-lookup->value_state arg1 state cfuncsinstance)]
[lookup-arg2 (G-value-lookup->value_state arg2 (get-state-from-pair lookup-arg1) cfuncsinstance)])
(cons ((compare-operator-to-function-multi op) (get-value-from-pair lookup-arg1)
(get-value-from-pair lookup-arg2))
(list (get-state-from-pair lookup-arg2))))))
; this function evaluates booleans
; this function is for 2 argument boolean expressions
; Returns (value, updated->state)
(define eval-boolean-expr-multi->value_state
(lambda (op arg1 arg2 state cfuncsinstance)
(let* ([lookup-arg1 (G-value-lookup->value_state arg1 state cfuncsinstance)]
[lookup-arg2 (G-value-lookup->value_state arg2 (get-state-from-pair lookup-arg1) cfuncsinstance)])
(cond
; We return a (value, state), hence the cons for the value and the state
; The value is derived from applying the operator on arg1 and arg2
; To handle side effects, the state passed into arg2 is the state after evaluating arg1
((and (eq? (G-type-lookup arg1 state cfuncsinstance) 'boolean)
(eq? (G-type-lookup arg2 state cfuncsinstance) 'boolean))
(cons ((boolean-operator-to-function-multi op)
(get-value-from-pair lookup-arg1)
(get-value-from-pair lookup-arg2))
(list (get-state-from-pair lookup-arg2))))
(else (error "not valid types for boolean expression"))))))
; this function evaluates math expressions
; it currently only supports integers
; this function is for 2 argument math expressions
(define eval-math-expr-multi->value_state
(lambda (op arg1 arg2 state cfuncsinstance)
(cond
((and (eq? (G-type-lookup arg1 state cfuncsinstance) 'integer)
(eq? (G-type-lookup arg2 state cfuncsinstance) 'integer))
(eval-math-expr-int-multi->value_state op arg1 arg2 state cfuncsinstance))
(else (error "invalid types for math expression")))))
; this function evaluates math expressions of integers
; this function is for 2 argument math expressions
; returns updated state
(define eval-math-expr-int-multi->value_state
(lambda (op arg1 arg2 state cfuncsinstance)
; We return a (value, state), hence the cons for the value and the state
; The value is derived from applying the operator on arg1 and arg2
; To handle side effects, the state passed into arg2 is the state after evaluating arg1
(let* ([lookup-arg1 (G-value-lookup->value_state arg1 state cfuncsinstance)]
[lookup-arg2 (G-value-lookup->value_state arg2 (get-state-from-pair lookup-arg1) cfuncsinstance)])
(cons ((math-operator-to-function-multi op #t)
(get-value-from-pair lookup-arg1)
(get-value-from-pair lookup-arg2))
(list (get-state-from-pair lookup-arg2))))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; state-interface section
; this function takes values (integers, strings, variables, expressions, ...) and returns their actual value
; for now it only handles int and bolean literals and expressions of the two
(define G-value-lookup->value_state
(lambda (value state cfuncsinstance)
(cond
; if its an expression, evaluate to get value
((list? value) (G-eval-atomic-statement->value_state value state cfuncsinstance))
((this-expr? value) (handle-this-expr state))
((super-expr? value) (error "not implemented"))
((integer? value) (cons value (list state)))
((boolean? value) (cons value (list state)))
((java-boolean? value) (cons (lookup-java-boolean value) (list state)))
((G-initialized? value state) (cons (variable-value-lookup value state) (list state)))
(else (error "unsupported value lookup" value "state" state)))))
; check if an arglist is a super-expression
(define super-expr?
(lambda (arglist)
(equal? arglist 'super)))
; check if an arglist is a this-expression
(define this-expr?
(lambda (arglist)
(equal? arglist 'this)))
; evaluate a this-expression
(define handle-this-expr
(lambda (state)
(list (list `(classname ,(get-base-class state empty-cfuncs))
(G-pop-to-stack-divider->state (extract-new-class-instance-state state)))
state)))
; Determines whether a boolean in java boolean notation was encountered
(define java-boolean?
(lambda (value)
(cond
((eq? value 'true) #t)
((eq? value 'false) #t)
(else #f))))
; Converts java to scheme boolean
(define lookup-java-boolean
(lambda (value)
(cond
((eq? value 'true) #t)
((eq? value 'false) #f)
(else (error "not a java boolean")))))
; tests whether variable is declared
; this function will need to check inputs and error
; as it will recieve bogus inputs
(define G-declared?
(lambda (variable-name state)
(cond
((state-empty? state) #f)
((declared-in-scope? (get-variable-section-state (get-top-scope state)) variable-name) #t)
(else (G-declared? variable-name (get-tail-scope state))))))
; Determines if a variable was declared in a scope
(define G-declared-in-stack-frame?
(lambda (variable-name state)
(cond
((state-empty? state) #f)
((is-top-scope-stack-divider? state) #f)
((declared-in-scope? (get-variable-section-state (get-top-scope state)) variable-name) #t)
(else (G-declared-in-stack-frame? variable-name (get-tail-scope state))))))
; tests whether a variable is declared in a given scope, which is a state in the list of states we have.
(define declared-in-scope?
(lambda (lis variable)
(cond
((null? lis) #f)
((eq? (get-variable-section-head lis) variable) #t)
(else (declared-in-scope? (get-variable-section-tail lis) variable)))))
; tests whether variable is declared and initialized
(define G-initialized?
(lambda (variable-name state)
(cond
((not (G-declared? variable-name state)) #f)
((null? (variable-value-lookup variable-name state)) #f)
(else #t))))
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------
; Adding/removing state section and scope access functions
; This section helps other sections affect the state with scoping rules
(define G-add-empty-scope-to-state->state
(lambda (state)
(cons (get-top-scope initstate) state)))
; removes the top scope from a state
(define G-remove-scope-from-state->state
(lambda (state)
(cond
((state-empty? state) (error "The main scope can't be removed!"))
((null? (get-tail-scope state)) initstate)
(else (get-tail-scope state)))))
; Gets the head of a scope (e.g. ((a) (b)) is the head of ((a b c) (b d e)))