Commit | Line | Data |
---|---|---|
9e8f5211 JM |
1 | GOTO MAIN |
2 | ||
3 | REM $INCLUDE: 'readline.in.bas' | |
4 | REM $INCLUDE: 'types.in.bas' | |
5 | REM $INCLUDE: 'reader.in.bas' | |
6 | REM $INCLUDE: 'printer.in.bas' | |
7 | REM $INCLUDE: 'env.in.bas' | |
8 | REM $INCLUDE: 'core.in.bas' | |
9 | ||
10 | REM $INCLUDE: 'debug.in.bas' | |
11 | ||
cc9dbd92 | 12 | REM READ(A$) -> R |
9e8f5211 JM |
13 | MAL_READ: |
14 | GOSUB READ_STR | |
15 | RETURN | |
16 | ||
cc9dbd92 | 17 | REM QUASIQUOTE(A) -> R |
af621e3a JM |
18 | SUB QUASIQUOTE |
19 | REM pair? | |
20 | IF (Z%(A,0)AND31)<6 OR (Z%(A,0)AND31)>7 THEN GOTO QQ_QUOTE | |
21 | IF (Z%(A,1)=0) THEN GOTO QQ_QUOTE | |
22 | GOTO QQ_UNQUOTE | |
23 | ||
24 | QQ_QUOTE: | |
9e8f5211 | 25 | REM ['quote, ast] |
cc9dbd92 | 26 | AS$="quote":T=5:GOSUB STRING |
bbab5c5d JM |
27 | B2=R:B1=A:GOSUB LIST2 |
28 | AY=B2:GOSUB RELEASE | |
9e8f5211 | 29 | |
af621e3a | 30 | GOTO QQ_DONE |
9e8f5211 JM |
31 | |
32 | QQ_UNQUOTE: | |
cc9dbd92 | 33 | R=A+1:GOSUB DEREF_R |
bbab5c5d | 34 | IF (Z%(R,0)AND31)<>5 THEN GOTO QQ_SPLICE_UNQUOTE |
cc9dbd92 | 35 | IF S$(Z%(R,1))<>"unquote" THEN GOTO QQ_SPLICE_UNQUOTE |
9e8f5211 | 36 | REM [ast[1]] |
cc9dbd92 | 37 | R=Z%(A,1)+1:GOSUB DEREF_R |
bbab5c5d | 38 | Z%(R,0)=Z%(R,0)+32 |
9e8f5211 | 39 | |
af621e3a | 40 | GOTO QQ_DONE |
9e8f5211 JM |
41 | |
42 | QQ_SPLICE_UNQUOTE: | |
cc9dbd92 | 43 | REM push A on the stack |
bbab5c5d | 44 | X=X+1:X%(X)=A |
9e8f5211 | 45 | REM rest of cases call quasiquote on ast[1..] |
af621e3a JM |
46 | A=Z%(A,1):CALL QUASIQUOTE |
47 | T6=R | |
cc9dbd92 | 48 | REM pop A off the stack |
bbab5c5d | 49 | A=X%(X):X=X-1 |
cc9dbd92 JM |
50 | |
51 | REM set A to ast[0] for last two cases | |
52 | A=A+1:GOSUB DEREF_A | |
53 | ||
af621e3a JM |
54 | REM pair? |
55 | IF (Z%(A,0)AND31)<6 OR (Z%(A,0)AND31)>7 THEN GOTO QQ_DEFAULT | |
56 | IF (Z%(A,1)=0) THEN GOTO QQ_DEFAULT | |
57 | ||
cc9dbd92 | 58 | B=A+1:GOSUB DEREF_B |
bbab5c5d | 59 | IF (Z%(B,0)AND31)<>5 THEN GOTO QQ_DEFAULT |
cc9dbd92 | 60 | IF S$(Z%(B,1))<>"splice-unquote" THEN QQ_DEFAULT |
9e8f5211 JM |
61 | REM ['concat, ast[0][1], quasiquote(ast[1..])] |
62 | ||
bbab5c5d JM |
63 | B=Z%(A,1)+1:GOSUB DEREF_B:B2=B |
64 | AS$="concat":T=5:GOSUB STRING:B3=R | |
65 | B1=T6:GOSUB LIST3 | |
9e8f5211 | 66 | REM release inner quasiquoted since outer list takes ownership |
bbab5c5d JM |
67 | AY=B1:GOSUB RELEASE |
68 | AY=B3:GOSUB RELEASE | |
af621e3a | 69 | GOTO QQ_DONE |
9e8f5211 JM |
70 | |
71 | QQ_DEFAULT: | |
72 | REM ['cons, quasiquote(ast[0]), quasiquote(ast[1..])] | |
73 | ||
cc9dbd92 | 74 | REM push T6 on the stack |
bbab5c5d | 75 | X=X+1:X%(X)=T6 |
cc9dbd92 | 76 | REM A set above to ast[0] |
af621e3a JM |
77 | CALL QUASIQUOTE |
78 | B2=R | |
cc9dbd92 | 79 | REM pop T6 off the stack |
bbab5c5d | 80 | T6=X%(X):X=X-1 |
9e8f5211 | 81 | |
bbab5c5d JM |
82 | AS$="cons":T=5:GOSUB STRING:B3=R |
83 | B1=T6:GOSUB LIST3 | |
9e8f5211 | 84 | REM release inner quasiquoted since outer list takes ownership |
bbab5c5d JM |
85 | AY=B1:GOSUB RELEASE |
86 | AY=B2:GOSUB RELEASE | |
87 | AY=B3:GOSUB RELEASE | |
af621e3a JM |
88 | QQ_DONE: |
89 | END SUB | |
9e8f5211 JM |
90 | |
91 | ||
cc9dbd92 | 92 | REM EVAL_AST(A, E) -> R |
af621e3a | 93 | SUB EVAL_AST |
cc9dbd92 | 94 | REM push A and E on the stack |
bbab5c5d | 95 | X=X+2:X%(X-1)=E:X%(X)=A |
9e8f5211 | 96 | |
cc9dbd92 | 97 | IF ER<>-2 THEN GOTO EVAL_AST_RETURN |
9e8f5211 JM |
98 | |
99 | GOSUB DEREF_A | |
100 | ||
bbab5c5d | 101 | T=Z%(A,0)AND31 |
cc9dbd92 JM |
102 | IF T=5 THEN GOTO EVAL_AST_SYMBOL |
103 | IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ | |
9e8f5211 JM |
104 | |
105 | REM scalar: deref to actual value and inc ref cnt | |
cc9dbd92 | 106 | R=A:GOSUB DEREF_R |
bbab5c5d | 107 | Z%(R,0)=Z%(R,0)+32 |
9e8f5211 JM |
108 | GOTO EVAL_AST_RETURN |
109 | ||
110 | EVAL_AST_SYMBOL: | |
af621e3a JM |
111 | K=A:GOTO ENV_GET |
112 | ENV_GET_RETURN: | |
9e8f5211 JM |
113 | GOTO EVAL_AST_RETURN |
114 | ||
115 | EVAL_AST_SEQ: | |
a742287e JM |
116 | REM allocate the first entry (T already set above) |
117 | L=0:N=0:GOSUB ALLOC | |
9e8f5211 JM |
118 | |
119 | REM make space on the stack | |
cc9dbd92 | 120 | X=X+4 |
9e8f5211 | 121 | REM push type of sequence |
bbab5c5d | 122 | X%(X-3)=T |
9e8f5211 | 123 | REM push sequence index |
bbab5c5d | 124 | X%(X-2)=-1 |
9e8f5211 | 125 | REM push future return value (new sequence) |
bbab5c5d | 126 | X%(X-1)=R |
9e8f5211 | 127 | REM push previous new sequence entry |
bbab5c5d | 128 | X%(X)=R |
9e8f5211 JM |
129 | |
130 | EVAL_AST_SEQ_LOOP: | |
9e8f5211 | 131 | REM update index |
bbab5c5d | 132 | X%(X-2)=X%(X-2)+1 |
9e8f5211 JM |
133 | |
134 | REM check if we are done evaluating the source sequence | |
cc9dbd92 | 135 | IF Z%(A,1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE |
9e8f5211 | 136 | |
01e8850d JM |
137 | REM if we are returning to DO, then skip last element |
138 | IF X%(X-6)=2 AND Z%(Z%(A,1),1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE | |
139 | ||
9e8f5211 | 140 | REM if hashmap, skip eval of even entries (keys) |
c7330b3d | 141 | IF (X%(X-3)=8) AND ((X%(X-2)AND1)=0) THEN GOTO EVAL_AST_DO_REF |
9e8f5211 JM |
142 | GOTO EVAL_AST_DO_EVAL |
143 | ||
144 | EVAL_AST_DO_REF: | |
cc9dbd92 | 145 | R=A+1:GOSUB DEREF_R: REM deref to target of referred entry |
bbab5c5d | 146 | Z%(R,0)=Z%(R,0)+32: REM inc ref cnt of referred value |
9e8f5211 JM |
147 | GOTO EVAL_AST_ADD_VALUE |
148 | ||
149 | EVAL_AST_DO_EVAL: | |
150 | REM call EVAL for each entry | |
af621e3a | 151 | A=A+1:CALL EVAL |
cc9dbd92 | 152 | A=A-1 |
9e8f5211 JM |
153 | GOSUB DEREF_R: REM deref to target of evaluated entry |
154 | ||
155 | EVAL_AST_ADD_VALUE: | |
156 | ||
157 | REM update previous value pointer to evaluated entry | |
bbab5c5d | 158 | Z%(X%(X)+1,1)=R |
9e8f5211 | 159 | |
cc9dbd92 | 160 | IF ER<>-2 THEN GOTO EVAL_AST_SEQ_LOOP_DONE |
9e8f5211 JM |
161 | |
162 | REM allocate the next entry | |
a742287e | 163 | REM same new sequence entry type |
bbab5c5d | 164 | T=X%(X-3):L=0:N=0:GOSUB ALLOC |
9e8f5211 JM |
165 | |
166 | REM update previous sequence entry value to point to new entry | |
bbab5c5d | 167 | Z%(X%(X),1)=R |
9e8f5211 | 168 | REM update previous ptr to current entry |
bbab5c5d | 169 | X%(X)=R |
9e8f5211 JM |
170 | |
171 | REM process the next sequence entry from source list | |
cc9dbd92 | 172 | A=Z%(A,1) |
9e8f5211 JM |
173 | |
174 | GOTO EVAL_AST_SEQ_LOOP | |
175 | EVAL_AST_SEQ_LOOP_DONE: | |
176 | REM if no error, get return value (new seq) | |
bbab5c5d | 177 | IF ER=-2 THEN R=X%(X-1) |
9e8f5211 | 178 | REM otherwise, free the return value and return nil |
bbab5c5d | 179 | IF ER<>-2 THEN R=0:AY=X%(X-1):GOSUB RELEASE |
9e8f5211 JM |
180 | |
181 | REM pop previous, return, index and type | |
cc9dbd92 | 182 | X=X-4 |
9e8f5211 JM |
183 | GOTO EVAL_AST_RETURN |
184 | ||
185 | EVAL_AST_RETURN: | |
cc9dbd92 | 186 | REM pop A and E off the stack |
bbab5c5d | 187 | E=X%(X-1):A=X%(X):X=X-2 |
af621e3a | 188 | END SUB |
9e8f5211 | 189 | |
af621e3a JM |
190 | REM EVAL(A, E) -> R |
191 | SUB EVAL | |
cc9dbd92 | 192 | LV=LV+1: REM track basic return stack level |
9e8f5211 | 193 | |
cc9dbd92 | 194 | REM push A and E on the stack |
bbab5c5d | 195 | X=X+2:X%(X-1)=E:X%(X)=A |
9e8f5211 | 196 | |
01e8850d JM |
197 | REM PRINT "EVAL A:"+STR$(A)+",X:"+STR$(X)+",LV:"+STR$(LV)+",FRE:"+STR$(FRE(0)) |
198 | ||
9e8f5211 JM |
199 | EVAL_TCO_RECUR: |
200 | ||
c7330b3d JM |
201 | IF ER<>-2 THEN GOTO EVAL_RETURN |
202 | ||
cc9dbd92 JM |
203 | REM AZ=A:PR=1:GOSUB PR_STR |
204 | REM PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]" | |
9e8f5211 JM |
205 | |
206 | GOSUB DEREF_A | |
207 | ||
208 | GOSUB LIST_Q | |
cc9dbd92 | 209 | IF R THEN GOTO APPLY_LIST |
9e8f5211 | 210 | REM ELSE |
af621e3a | 211 | CALL EVAL_AST |
9e8f5211 JM |
212 | GOTO EVAL_RETURN |
213 | ||
214 | APPLY_LIST: | |
215 | GOSUB EMPTY_Q | |
bbab5c5d | 216 | IF R THEN R=A:Z%(R,0)=Z%(R,0)+32:GOTO EVAL_RETURN |
9e8f5211 | 217 | |
bbab5c5d JM |
218 | A0=A+1 |
219 | R=A0:GOSUB DEREF_R:A0=R | |
9e8f5211 JM |
220 | |
221 | REM get symbol in A$ | |
bbab5c5d JM |
222 | IF (Z%(A0,0)AND31)<>5 THEN A$="" |
223 | IF (Z%(A0,0)AND31)=5 THEN A$=S$(Z%(A0,1)) | |
9e8f5211 JM |
224 | |
225 | IF A$="def!" THEN GOTO EVAL_DEF | |
226 | IF A$="let*" THEN GOTO EVAL_LET | |
227 | IF A$="quote" THEN GOTO EVAL_QUOTE | |
228 | IF A$="quasiquote" THEN GOTO EVAL_QUASIQUOTE | |
229 | IF A$="do" THEN GOTO EVAL_DO | |
230 | IF A$="if" THEN GOTO EVAL_IF | |
231 | IF A$="fn*" THEN GOTO EVAL_FN | |
232 | GOTO EVAL_INVOKE | |
233 | ||
234 | EVAL_GET_A3: | |
bbab5c5d JM |
235 | A3=Z%(Z%(Z%(A,1),1),1)+1 |
236 | R=A3:GOSUB DEREF_R:A3=R | |
9e8f5211 | 237 | EVAL_GET_A2: |
bbab5c5d JM |
238 | A2=Z%(Z%(A,1),1)+1 |
239 | R=A2:GOSUB DEREF_R:A2=R | |
9e8f5211 | 240 | EVAL_GET_A1: |
bbab5c5d JM |
241 | A1=Z%(A,1)+1 |
242 | R=A1:GOSUB DEREF_R:A1=R | |
9e8f5211 JM |
243 | RETURN |
244 | ||
245 | EVAL_DEF: | |
246 | REM PRINT "def!" | |
bbab5c5d | 247 | GOSUB EVAL_GET_A2: REM set A1 and A2 |
9e8f5211 | 248 | |
bbab5c5d | 249 | X=X+1:X%(X)=A1: REM push A1 |
af621e3a | 250 | A=A2:CALL EVAL: REM eval a2 |
bbab5c5d | 251 | A1=X%(X):X=X-1: REM pop A1 |
9e8f5211 | 252 | |
cc9dbd92 | 253 | IF ER<>-2 THEN GOTO EVAL_RETURN |
70f29a2b | 254 | |
9e8f5211 | 255 | REM set a1 in env to a2 |
bbab5c5d | 256 | K=A1:V=R:GOSUB ENV_SET |
9e8f5211 JM |
257 | GOTO EVAL_RETURN |
258 | ||
259 | EVAL_LET: | |
260 | REM PRINT "let*" | |
bbab5c5d | 261 | GOSUB EVAL_GET_A2: REM set A1 and A2 |
9e8f5211 | 262 | |
bbab5c5d JM |
263 | X=X+1:X%(X)=A2: REM push/save A2 |
264 | X=X+1:X%(X)=E: REM push env for for later release | |
9e8f5211 JM |
265 | |
266 | REM create new environment with outer as current environment | |
cc9dbd92 JM |
267 | O=E:GOSUB ENV_NEW |
268 | E=R | |
9e8f5211 | 269 | EVAL_LET_LOOP: |
bbab5c5d | 270 | IF Z%(A1,1)=0 THEN GOTO EVAL_LET_LOOP_DONE |
9e8f5211 | 271 | |
bbab5c5d | 272 | X=X+1:X%(X)=A1: REM push A1 |
9e8f5211 | 273 | REM eval current A1 odd element |
af621e3a | 274 | A=Z%(A1,1)+1:CALL EVAL |
bbab5c5d | 275 | A1=X%(X):X=X-1: REM pop A1 |
9e8f5211 | 276 | |
c7330b3d JM |
277 | IF ER<>-2 THEN GOTO EVAL_LET_LOOP_DONE |
278 | ||
bbab5c5d JM |
279 | REM set environment: even A1 key to odd A1 eval'd above |
280 | K=A1+1:V=R:GOSUB ENV_SET | |
cc9dbd92 | 281 | AY=R:GOSUB RELEASE: REM release our use, ENV_SET took ownership |
9e8f5211 | 282 | |
bbab5c5d JM |
283 | REM skip to the next pair of A1 elements |
284 | A1=Z%(Z%(A1,1),1) | |
9e8f5211 | 285 | GOTO EVAL_LET_LOOP |
70f29a2b | 286 | |
9e8f5211 | 287 | EVAL_LET_LOOP_DONE: |
bbab5c5d | 288 | E4=X%(X):X=X-1: REM pop previous env |
70f29a2b JM |
289 | |
290 | REM release previous environment if not the current EVAL env | |
bbab5c5d | 291 | IF E4<>X%(X-2) THEN AY=E4:GOSUB RELEASE |
9e8f5211 | 292 | |
bbab5c5d JM |
293 | A2=X%(X):X=X-1: REM pop A2 |
294 | A=A2:GOTO EVAL_TCO_RECUR: REM TCO loop | |
9e8f5211 JM |
295 | |
296 | EVAL_DO: | |
cc9dbd92 | 297 | A=Z%(A,1): REM rest |
01e8850d | 298 | X=X+1:X%(X)=A: REM push/save A |
9e8f5211 | 299 | |
af621e3a | 300 | CALL EVAL_AST |
9e8f5211 | 301 | |
01e8850d JM |
302 | REM cleanup |
303 | AY=R: REM get eval'd list for release | |
304 | ||
305 | A=X%(X):X=X-1: REM pop/restore original A for LAST | |
306 | GOSUB LAST: REM get last element for return | |
307 | A=R: REM new recur AST | |
308 | ||
309 | REM cleanup | |
310 | GOSUB RELEASE: REM release eval'd list | |
311 | AY=A:GOSUB RELEASE: REM release LAST value (not sure why) | |
312 | ||
313 | GOTO EVAL_TCO_RECUR: REM TCO loop | |
9e8f5211 JM |
314 | |
315 | EVAL_QUOTE: | |
cc9dbd92 | 316 | R=Z%(A,1)+1:GOSUB DEREF_R |
bbab5c5d | 317 | Z%(R,0)=Z%(R,0)+32 |
9e8f5211 JM |
318 | GOTO EVAL_RETURN |
319 | ||
320 | EVAL_QUASIQUOTE: | |
cc9dbd92 | 321 | R=Z%(A,1)+1:GOSUB DEREF_R |
af621e3a | 322 | A=R:CALL QUASIQUOTE |
9e8f5211 | 323 | REM add quasiquote result to pending release queue to free when |
cc9dbd92 | 324 | REM next lower EVAL level returns (LV) |
bbab5c5d | 325 | Y=Y+1:Y%(Y,0)=R:Y%(Y,1)=LV |
9e8f5211 | 326 | |
cc9dbd92 | 327 | A=R:GOTO EVAL_TCO_RECUR: REM TCO loop |
9e8f5211 JM |
328 | |
329 | EVAL_IF: | |
bbab5c5d | 330 | GOSUB EVAL_GET_A1: REM set A1 |
cc9dbd92 | 331 | REM push A |
bbab5c5d | 332 | X=X+1:X%(X)=A |
af621e3a | 333 | A=A1:CALL EVAL |
cc9dbd92 | 334 | REM pop A |
bbab5c5d | 335 | A=X%(X):X=X-1 |
cc9dbd92 | 336 | IF (R=0) OR (R=1) THEN GOTO EVAL_IF_FALSE |
9e8f5211 JM |
337 | |
338 | EVAL_IF_TRUE: | |
cc9dbd92 | 339 | AY=R:GOSUB RELEASE |
bbab5c5d JM |
340 | GOSUB EVAL_GET_A2: REM set A1 and A2 after EVAL |
341 | A=A2:GOTO EVAL_TCO_RECUR: REM TCO loop | |
9e8f5211 | 342 | EVAL_IF_FALSE: |
cc9dbd92 | 343 | AY=R:GOSUB RELEASE |
bbab5c5d | 344 | REM if no false case (A3), return nil |
c7330b3d JM |
345 | B=A:GOSUB COUNT |
346 | IF R<4 THEN R=0:GOTO EVAL_RETURN | |
bbab5c5d JM |
347 | GOSUB EVAL_GET_A3: REM set A1 - A3 after EVAL |
348 | A=A3:GOTO EVAL_TCO_RECUR: REM TCO loop | |
9e8f5211 JM |
349 | |
350 | EVAL_FN: | |
bbab5c5d JM |
351 | GOSUB EVAL_GET_A2: REM set A1 and A2 |
352 | A=A2:P=A1:GOSUB MAL_FUNCTION | |
9e8f5211 JM |
353 | GOTO EVAL_RETURN |
354 | ||
355 | EVAL_INVOKE: | |
af621e3a | 356 | CALL EVAL_AST |
9e8f5211 JM |
357 | |
358 | REM if error, return f/args for release by caller | |
cc9dbd92 | 359 | IF ER<>-2 THEN GOTO EVAL_RETURN |
9e8f5211 JM |
360 | |
361 | REM push f/args for release after call | |
bbab5c5d | 362 | X=X+1:X%(X)=R |
9e8f5211 | 363 | |
cc9dbd92 | 364 | F=R+1 |
9e8f5211 | 365 | |
cc9dbd92 JM |
366 | AR=Z%(R,1): REM rest |
367 | R=F:GOSUB DEREF_R:F=R | |
9e8f5211 | 368 | |
bbab5c5d JM |
369 | REM if metadata, get the actual object |
370 | IF (Z%(F,0)AND31)>=16 THEN F=Z%(F,1) | |
371 | ||
372 | IF (Z%(F,0)AND31)=9 THEN GOTO EVAL_DO_FUNCTION | |
373 | IF (Z%(F,0)AND31)=10 THEN GOTO EVAL_DO_MAL_FUNCTION | |
9e8f5211 JM |
374 | |
375 | REM if error, pop and return f/args for release by caller | |
bbab5c5d | 376 | R=X%(X):X=X-1 |
cc9dbd92 | 377 | ER=-1:ER$="apply of non-function":GOTO EVAL_RETURN |
9e8f5211 JM |
378 | |
379 | EVAL_DO_FUNCTION: | |
01e8850d | 380 | REM regular function |
af621e3a | 381 | IF Z%(F,1)<60 THEN GOSUB DO_FUNCTION:GOTO EVAL_DO_FUNCTION_SKIP |
01e8850d | 382 | REM for recur functions (apply, map, swap!), use GOTO |
af621e3a JM |
383 | IF Z%(F,1)>60 THEN CALL DO_TCO_FUNCTION |
384 | EVAL_DO_FUNCTION_SKIP: | |
9e8f5211 JM |
385 | |
386 | REM pop and release f/args | |
bbab5c5d | 387 | AY=X%(X):X=X-1:GOSUB RELEASE |
9e8f5211 JM |
388 | GOTO EVAL_RETURN |
389 | ||
390 | EVAL_DO_MAL_FUNCTION: | |
bbab5c5d | 391 | E4=E: REM save the current environment for release |
9e8f5211 JM |
392 | |
393 | REM create new environ using env stored with function | |
bbab5c5d | 394 | O=Z%(F+1,1):BI=Z%(F+1,0):EX=AR:GOSUB ENV_NEW_BINDS |
9e8f5211 JM |
395 | |
396 | REM release previous env if it is not the top one on the | |
bbab5c5d | 397 | REM stack (X%(X-2)) because our new env refers to it and |
9e8f5211 | 398 | REM we no longer need to track it (since we are TCO recurring) |
bbab5c5d | 399 | IF E4<>X%(X-2) THEN AY=E4:GOSUB RELEASE |
9e8f5211 JM |
400 | |
401 | REM claim the AST before releasing the list containing it | |
bbab5c5d | 402 | A=Z%(F,1):Z%(A,0)=Z%(A,0)+32 |
9e8f5211 | 403 | REM add AST to pending release queue to free as soon as EVAL |
cc9dbd92 | 404 | REM actually returns (LV+1) |
bbab5c5d | 405 | Y=Y+1:Y%(Y,0)=A:Y%(Y,1)=LV+1 |
9e8f5211 JM |
406 | |
407 | REM pop and release f/args | |
bbab5c5d | 408 | AY=X%(X):X=X-1:GOSUB RELEASE |
9e8f5211 | 409 | |
cc9dbd92 JM |
410 | REM A set above |
411 | E=R:GOTO EVAL_TCO_RECUR: REM TCO loop | |
9e8f5211 JM |
412 | |
413 | EVAL_RETURN: | |
cc9dbd92 JM |
414 | REM AZ=R: PR=1: GOSUB PR_STR |
415 | REM PRINT "EVAL_RETURN R: ["+R$+"] ("+STR$(R)+"), LV:"+STR$(LV)+",ER:"+STR$(ER) | |
9e8f5211 JM |
416 | |
417 | REM release environment if not the top one on the stack | |
bbab5c5d | 418 | IF E<>X%(X-1) THEN AY=E:GOSUB RELEASE |
9e8f5211 | 419 | |
cc9dbd92 | 420 | LV=LV-1: REM track basic return stack level |
9e8f5211 JM |
421 | |
422 | REM release everything we couldn't release earlier | |
423 | GOSUB RELEASE_PEND | |
424 | ||
425 | REM trigger GC | |
bbab5c5d | 426 | TA=FRE(0) |
9e8f5211 | 427 | |
cc9dbd92 | 428 | REM pop A and E off the stack |
bbab5c5d | 429 | E=X%(X-1):A=X%(X):X=X-2 |
9e8f5211 | 430 | |
af621e3a | 431 | END SUB |
9e8f5211 | 432 | |
cc9dbd92 | 433 | REM PRINT(A) -> R$ |
9e8f5211 | 434 | MAL_PRINT: |
cc9dbd92 | 435 | AZ=A:PR=1:GOSUB PR_STR |
9e8f5211 JM |
436 | RETURN |
437 | ||
cc9dbd92 | 438 | REM RE(A$) -> R |
bbab5c5d | 439 | REM Assume D has repl_env |
9e8f5211 JM |
440 | REM caller must release result |
441 | RE: | |
cc9dbd92 | 442 | R1=0 |
9e8f5211 | 443 | GOSUB MAL_READ |
cc9dbd92 | 444 | R1=R |
0e508fa5 | 445 | IF ER<>-2 THEN GOTO RE_DONE |
9e8f5211 | 446 | |
af621e3a | 447 | A=R:E=D:CALL EVAL |
9e8f5211 | 448 | |
0e508fa5 | 449 | RE_DONE: |
9e8f5211 | 450 | REM Release memory from MAL_READ |
cc9dbd92 | 451 | IF R1<>0 THEN AY=R1:GOSUB RELEASE |
9e8f5211 JM |
452 | RETURN: REM caller must release result of EVAL |
453 | ||
454 | REM REP(A$) -> R$ | |
bbab5c5d | 455 | REM Assume D has repl_env |
af621e3a | 456 | SUB REP |
cc9dbd92 | 457 | R1=0:R2=0 |
9e8f5211 | 458 | GOSUB MAL_READ |
cc9dbd92 JM |
459 | R1=R |
460 | IF ER<>-2 THEN GOTO REP_DONE | |
9e8f5211 | 461 | |
af621e3a | 462 | A=R:E=D:CALL EVAL |
cc9dbd92 JM |
463 | R2=R |
464 | IF ER<>-2 THEN GOTO REP_DONE | |
9e8f5211 | 465 | |
cc9dbd92 | 466 | A=R:GOSUB MAL_PRINT |
9e8f5211 JM |
467 | RT$=R$ |
468 | ||
469 | REP_DONE: | |
470 | REM Release memory from MAL_READ and EVAL | |
cc9dbd92 JM |
471 | IF R2<>0 THEN AY=R2:GOSUB RELEASE |
472 | IF R1<>0 THEN AY=R1:GOSUB RELEASE | |
9e8f5211 | 473 | R$=RT$ |
af621e3a | 474 | END SUB |
9e8f5211 JM |
475 | |
476 | REM MAIN program | |
477 | MAIN: | |
478 | GOSUB INIT_MEMORY | |
479 | ||
cc9dbd92 | 480 | LV=0 |
9e8f5211 JM |
481 | |
482 | REM create repl_env | |
bbab5c5d | 483 | O=-1:GOSUB ENV_NEW:D=R |
9e8f5211 JM |
484 | |
485 | REM core.EXT: defined in Basic | |
bbab5c5d | 486 | E=D:GOSUB INIT_CORE_NS: REM set core functions in repl_env |
9e8f5211 | 487 | |
bbab5c5d | 488 | ZT=ZI: REM top of memory after base repl_env |
9e8f5211 JM |
489 | |
490 | REM core.mal: defined using the language itself | |
60ef223c | 491 | A$="(def! not (fn* (a) (if a false true)))" |
cc9dbd92 | 492 | GOSUB RE:AY=R:GOSUB RELEASE |
9e8f5211 | 493 | |
0e508fa5 | 494 | A$="(def! load-file (fn* (f) (eval (read-file f))))" |
cc9dbd92 | 495 | GOSUB RE:AY=R:GOSUB RELEASE |
9e8f5211 JM |
496 | |
497 | REM load the args file | |
498 | A$="(def! -*ARGS*- (load-file "+CHR$(34)+".args.mal"+CHR$(34)+"))" | |
cc9dbd92 | 499 | GOSUB RE:AY=R:GOSUB RELEASE |
9e8f5211 JM |
500 | |
501 | REM set the argument list | |
60ef223c | 502 | A$="(def! *ARGV* (rest -*ARGS*-))" |
cc9dbd92 | 503 | GOSUB RE:AY=R:GOSUB RELEASE |
9e8f5211 JM |
504 | |
505 | REM get the first argument | |
60ef223c JM |
506 | A$="(first -*ARGS*-)" |
507 | GOSUB RE | |
9e8f5211 JM |
508 | |
509 | REM if there is an argument, then run it as a program | |
cc9dbd92 | 510 | IF R<>0 THEN AY=R:GOSUB RELEASE:GOTO RUN_PROG |
9e8f5211 | 511 | REM no arguments, start REPL loop |
cc9dbd92 | 512 | IF R=0 THEN GOTO REPL_LOOP |
9e8f5211 JM |
513 | |
514 | RUN_PROG: | |
515 | REM run a single mal program and exit | |
60ef223c JM |
516 | A$="(load-file (first -*ARGS*-))" |
517 | GOSUB RE | |
cc9dbd92 | 518 | IF ER<>-2 THEN GOSUB PRINT_ERROR |
01e8850d | 519 | GOTO QUIT |
9e8f5211 JM |
520 | |
521 | REPL_LOOP: | |
60ef223c | 522 | A$="user> ":GOSUB READLINE: REM call input parser |
9e8f5211 JM |
523 | IF EOF=1 THEN GOTO QUIT |
524 | ||
af621e3a | 525 | A$=R$:CALL REP: REM call REP |
9e8f5211 | 526 | |
cc9dbd92 | 527 | IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP |
9e8f5211 JM |
528 | PRINT R$ |
529 | GOTO REPL_LOOP | |
530 | ||
531 | QUIT: | |
4e7d8a1b | 532 | REM GOSUB PR_MEMORY_SUMMARY |
9e8f5211 JM |
533 | END |
534 | ||
535 | PRINT_ERROR: | |
536 | PRINT "Error: "+ER$ | |
cc9dbd92 | 537 | ER=-2:ER$="" |
9e8f5211 JM |
538 | RETURN |
539 |