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