|
25 | 25 | (lambda (program state) |
26 | 26 | (evaluate-parse-tree-with-cfuncs->retval_state program state empty-cfuncs))) |
27 | 27 |
|
| 28 | + |
28 | 29 | (define evaluate-parse-tree-with-cfuncs->retval_state |
29 | 30 | (lambda (program state cfuncsinstance) |
30 | 31 | (call/cc |
|
37 | 38 | (else (list '() (evaluate-statement-list->state program state |
38 | 39 | (cfuncs-update-return cfuncsinstance return))))))))) |
39 | 40 |
|
40 | | -;(trace evaluate-parse-tree->retval_state) |
| 41 | +;(trace evaluate-parse-tree-with-cfuncs->retval_state) |
41 | 42 |
|
42 | 43 |
|
43 | 44 | (define evaluate-statement-list->state |
|
122 | 123 | (lambda (name args state cfuncsinstance) |
123 | 124 | (cond |
124 | 125 | ((and (dot-expr? name) (eq? (dotted-class-instance (arglist-dot name)) 'this)) |
125 | | - (eval-function-post-name-eval (dotted-class-call (arglist-dot name)) args state state default-superclass cfuncsinstance)) |
| 126 | + (let* ([evaled-function (eval-function-post-name-eval (dotted-class-call (arglist-dot name)) |
| 127 | + args |
| 128 | + state |
| 129 | + state |
| 130 | + (get-base-class state cfuncsinstance) |
| 131 | + #t |
| 132 | + cfuncsinstance)] |
| 133 | + [function-return (get-value-from-pair evaled-function)] |
| 134 | + [function-state (get-state-from-pair evaled-function)]) |
| 135 | + (list function-return function-state))) |
126 | 136 | ((and (dot-expr? name) (eq? (dotted-class-instance (arglist-dot name)) 'super)) |
127 | 137 | (eval-function-post-name-eval (dotted-class-call (arglist-dot name)) |
128 | 138 | args |
129 | 139 | state |
130 | 140 | state |
131 | | - (get-value-from-pair (G-value-lookup->value_state '.class state cfuncsinstance)) |
| 141 | + (get-current-class state cfuncsinstance) |
| 142 | + #f |
132 | 143 | cfuncsinstance)) |
133 | 144 | ((dot-expr? name) |
134 | 145 | (let* ([dottedname (arglist-dot name)] |
135 | 146 | [evaled-function (eval-function-post-name-eval (evaluate-dotted-function-name dottedname state) |
136 | 147 | args |
137 | 148 | state |
138 | 149 | (construct-dotted-state dottedname state) |
139 | | - default-superclass |
| 150 | + (get-current-class (construct-dotted-state dottedname state) cfuncsinstance) |
| 151 | + #t |
140 | 152 | cfuncsinstance)] |
141 | 153 | [function-return (get-value-from-pair evaled-function)] |
142 | 154 | [function-state (get-state-from-pair evaled-function)]) |
143 | | - ;possible error, extract should be called on function state |
144 | 155 | (list function-return (update-class-instance (dotted-class-instance dottedname) (extract-new-class-instance-state function-state) state)))) |
145 | 156 |
|
146 | | - (else (eval-function-post-name-eval name args state state default-superclass cfuncsinstance))))) |
| 157 | + (else (eval-function-post-name-eval name args state state default-currentclass #t cfuncsinstance))))) |
147 | 158 | ;(trace G-eval-function->value_state) |
148 | | -(define default-superclass '()) |
| 159 | +(define get-base-class |
| 160 | + (lambda (state cfuncsinstance) |
| 161 | + (get-value-from-pair (G-value-lookup->value_state '.class state cfuncsinstance)))) |
| 162 | +(define get-current-class |
| 163 | + (lambda (state cfuncsinstance) |
| 164 | + (cond |
| 165 | + ((G-initialized? '.this state) (get-value-from-pair (G-value-lookup->value_state '.this state cfuncsinstance))) |
| 166 | + (else (get-value-from-pair (G-value-lookup->value_state '.class state cfuncsinstance)))))) |
| 167 | + |
| 168 | +(define get-super-class |
| 169 | + (lambda (currentclass state) |
| 170 | + (cond |
| 171 | + ((null? state) '()) |
| 172 | + ((null? currentclass) default-currentclass) |
| 173 | + ((declared-in-scope? (get-variable-section-state (get-top-scope state)) '.class) |
| 174 | + (cond |
| 175 | + ((and (eq? (car (get-value-section-state (get-top-scope state))) currentclass) |
| 176 | + (G-initialized? '.class (get-tail-scope state))) |
| 177 | + (get-value-from-pair (G-value-lookup->value_state '.class (get-tail-scope state) empty-cfuncs))) |
| 178 | + (else (get-super-class currentclass (get-tail-scope state))))) |
| 179 | + (else (get-super-class currentclass (get-tail-scope state)))))) |
| 180 | + |
| 181 | + |
| 182 | +(define default-currentclass '()) |
149 | 183 |
|
150 | 184 | (define eval-function-post-name-eval |
151 | | - (lambda (name args state function-state superclass cfuncsinstance) |
152 | | - (let* ([popped-state (G-add-empty-scope-to-state->state |
153 | | - (G-push-stack-divider-to-state->state superclass |
154 | | - (G-pop-scope-to-function->state |
155 | | - name |
156 | | - superclass |
157 | | - function-state)))] |
158 | | - [function-in-state (variable-value-lookup name popped-state)] |
| 185 | + (lambda (name args state function-state current-class preserve-current-class cfuncsinstance) |
| 186 | + (let* ([super-popped-state (G-add-empty-scope-to-state->state |
| 187 | + (G-push-stack-divider-to-state->state current-class |
| 188 | + (G-pop-scope-to-function->state |
| 189 | + name |
| 190 | + (if preserve-current-class |
| 191 | + current-class |
| 192 | + (get-super-class current-class state)) |
| 193 | + function-state)))] |
| 194 | + [popped-state |
| 195 | + (push-variable-as-literal->state '.this |
| 196 | + (if preserve-current-class |
| 197 | + current-class |
| 198 | + (get-value-from-pair (G-value-lookup->value_state '.class super-popped-state cfuncsinstance))) |
| 199 | + (G-add-empty-scope-to-state->state |
| 200 | + (G-push-stack-divider-to-state->state current-class |
| 201 | + (G-pop-scope-to-function-or-class->state |
| 202 | + name |
| 203 | + current-class |
| 204 | + function-state))))] |
| 205 | + |
| 206 | + [function-in-state (variable-value-lookup name super-popped-state)] |
159 | 207 | [evaluate-function-call |
160 | 208 | (evaluate-parse-tree-with-cfuncs->retval_state |
161 | 209 | (get-funcall-body function-in-state) |
|
178 | 226 | (G-pop-to-stack-divider->state |
179 | 227 | (get-state-from-pair |
180 | 228 | evaluate-function-call))))))) |
| 229 | + |
181 | 230 | ;(trace eval-function-post-name-eval) |
182 | 231 | (define evaluate-actual-args-for-state |
183 | 232 | (lambda (actual state cfuncsinstance) |
|
223 | 272 | (define extract-new-class-instance-state |
224 | 273 | (lambda (state) |
225 | 274 | (reverse (extract-new-class-instance-state-sub (get-tail-scope (reverse state)))))) |
226 | | -;(trace extract-new-class-instance-state) |
| 275 | + |
227 | 276 | (define extract-new-class-instance-state-sub |
228 | 277 | (lambda (state) |
229 | 278 | (cond |
230 | 279 | ((null? state) '()) |
231 | 280 | ((is-top-scope-class-divider? state) '()) |
232 | 281 | (else (cons (get-top-scope state) (extract-new-class-instance-state-sub (get-tail-scope state))))))) |
233 | | -;(trace extract-new-class-instance-state-sub) |
| 282 | + |
234 | 283 | (define update-class-instance |
235 | 284 | (lambda (instancename new-instance-state state) |
236 | 285 | (G-push-state->state |
|
477 | 526 | (list (get-instance-initialization-value arglist state) state)) |
478 | 527 | (else (error "not a valid atomic statement" arglist state))))) |
479 | 528 |
|
480 | | -;(trace G-eval-atomic-statement->value_state) |
| 529 | + |
481 | 530 | ; STUB |
482 | 531 | (define dot-expr? |
483 | 532 | (lambda (arglist) |
|
576 | 625 | (get-value-from-pair evaluate-assign) |
577 | 626 | (get-state-from-pair evaluate-assign)) cfuncsinstance)) |
578 | 627 | (else (error "variable undeclared args:" arglist "state" state)))))) |
579 | | -;(trace G-eval-assign->value_state) |
| 628 | + |
580 | 629 | (define evaluate-dotted-assign->value_state |
581 | 630 | (lambda (dot-expression assign-value state cfuncsinstance) |
582 | 631 | (cond |
|
600 | 649 | (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value) (construct-dotted-state dot-expression state) cfuncsinstance))) |
601 | 650 | state))))) |
602 | 651 |
|
603 | | -;(trace evaluate-dotted-assign->value_state) |
| 652 | + |
604 | 653 |
|
605 | 654 |
|
606 | 655 | ; Determines whether or not an assignment argument is reached |
|
800 | 849 | ((state-empty? state) #f) |
801 | 850 | ((declared-in-scope? (get-variable-section-state (get-top-scope state)) variable-name) #t) |
802 | 851 | (else (G-declared? variable-name (get-tail-scope state)))))) |
803 | | -;(trace G-declared?) |
| 852 | + |
804 | 853 | ; Determines if a variable was declared in a scope |
805 | 854 | (define G-declared-in-stack-frame? |
806 | 855 | (lambda (variable-name state) |
|
978 | 1027 | (G-type-lookup (variable-value-lookup variable state) state cfuncsinstance))) |
979 | 1028 |
|
980 | 1029 | ; Pops scopes off of the state until the head scope contains a function |
| 1030 | +;pops to the scope after the currentclass |
| 1031 | +(define G-pop-scope-to-function-or-class->state |
| 1032 | + (lambda (fn desiredclass state) |
| 1033 | + (cond |
| 1034 | + ((null? state) (error "function was not found in state")) |
| 1035 | + ((null? desiredclass) (pop-scope-to-function-default fn state)) |
| 1036 | + ((declared-in-scope? (get-variable-section-state (get-top-scope state)) '.class) |
| 1037 | + (cond |
| 1038 | + ((eq? (car (get-value-section-state (get-top-scope state))) desiredclass) state) |
| 1039 | + (else (G-pop-scope-to-function-or-class->state fn desiredclass (get-tail-scope state))))) |
| 1040 | + ((declared-in-scope? (get-variable-section-state (get-top-scope state)) fn) state) |
| 1041 | + (else (G-pop-scope-to-function-or-class->state fn desiredclass (get-tail-scope state)))))) |
| 1042 | + |
981 | 1043 | (define G-pop-scope-to-function->state |
982 | | - (lambda (fn superclass state) |
| 1044 | + (lambda (fn desiredclass state) |
983 | 1045 | (cond |
984 | 1046 | ((null? state) (error "function was not found in state")) |
985 | | - ((null? superclass) (pop-scope-to-function-default fn state)) |
| 1047 | + ((null? desiredclass) (pop-scope-to-function-default fn state)) |
986 | 1048 | ((declared-in-scope? (get-variable-section-state (get-top-scope state)) '.class) |
987 | 1049 | (cond |
988 | | - ((eq? (car (get-value-section-state (get-top-scope state))) superclass) (pop-scope-to-function-default fn (get-tail-scope state))) |
989 | | - (else (G-pop-scope-to-function->state fn superclass (get-tail-scope state))))) |
990 | | - (else (G-pop-scope-to-function->state fn superclass (get-tail-scope state)))))) |
| 1050 | + ((eq? (car (get-value-section-state (get-top-scope state))) desiredclass) (pop-scope-to-function-default fn state)) |
| 1051 | + (else (G-pop-scope-to-function->state fn desiredclass (get-tail-scope state))))) |
| 1052 | + ((declared-in-scope? (get-variable-section-state (get-top-scope state)) fn) state) |
| 1053 | + (else (G-pop-scope-to-function->state fn desiredclass (get-tail-scope state)))))) |
991 | 1054 |
|
992 | 1055 | (define pop-scope-to-function-default |
993 | 1056 | (lambda (fn state) |
994 | 1057 | (cond |
995 | 1058 | ((null? state) (error "function was not found in state")) |
996 | 1059 | ((declared-in-scope? (get-variable-section-state (get-top-scope state)) fn) state) |
997 | 1060 | (else (pop-scope-to-function-default fn (get-tail-scope state)))))) |
998 | | -;(trace G-pop-scope-to-function->state) |
| 1061 | + |
| 1062 | + |
| 1063 | +(define G-shortmerge-states->state |
| 1064 | + (lambda (origin-state mod-state) |
| 1065 | + (reverse (shortmerge (reverse origin-state) (reverse mod-state))))) |
| 1066 | + |
| 1067 | +(define shortmerge |
| 1068 | + (lambda (orig-state mod-state) |
| 1069 | + (cond |
| 1070 | + ((null? orig-state) '()) |
| 1071 | + (else (cons (get-top-scope mod-state) |
| 1072 | + (shortmerge (get-tail-scope orig-state)(get-tail-scope mod-state))))))) |
999 | 1073 | ; Merges an original state with the state after a funciton call, |
1000 | 1074 | ; assumes that the function call state has smaller airty than the original state |
1001 | 1075 | (define G-merge-states->state |
1002 | 1076 | (lambda (origin-state mod-state) |
1003 | 1077 | (reverse (merge (reverse origin-state) (reverse mod-state))))) |
1004 | | -;(trace G-merge-states->state) |
| 1078 | + |
1005 | 1079 | ; merges two reversed states |
1006 | 1080 | (define merge |
1007 | 1081 | (lambda (orig-state mod-state) |
|
1069 | 1143 | ((null? (G-get-class-superclass classname state)) (evaluate-closure->state classname state)) |
1070 | 1144 | (else (cons (get-top-scope |
1071 | 1145 | (evaluate-closure->state classname state)) (G-eval-class-closure->state (G-get-class-superclass classname state) state)))))) |
1072 | | -;(trace G-eval-class-closure->state) |
| 1146 | + |
1073 | 1147 | (define evaluate-closure->state |
1074 | 1148 | (lambda (classname state) |
1075 | 1149 | (push-variable-as-literal->state '.class |
|
0 commit comments