Merge pull request #383 from asarhaddon/ada2tco-do
[jackhill/mal.git] / basic / step9_try.in.bas
1 GOTO MAIN
2
3 REM $INCLUDE: 'mem.in.bas'
4 REM $INCLUDE: 'types.in.bas'
5 REM $INCLUDE: 'readline.in.bas'
6 REM $INCLUDE: 'reader.in.bas'
7 REM $INCLUDE: 'printer.in.bas'
8 REM $INCLUDE: 'env.in.bas'
9 REM $INCLUDE: 'core.in.bas'
10
11 REM $INCLUDE: 'debug.in.bas'
12
13 REM READ(A$) -> R
14 MAL_READ:
15 GOSUB READ_STR
16 RETURN
17
18 REM QUASIQUOTE(A) -> R
19 SUB QUASIQUOTE
20 REM pair?
21 GOSUB TYPE_A
22 IF T<6 OR T>7 THEN GOTO QQ_QUOTE
23 IF (Z%(A+1)=0) THEN GOTO QQ_QUOTE
24 GOTO QQ_UNQUOTE
25
26 QQ_QUOTE:
27 REM ['quote, ast]
28 B$="quote":T=5:GOSUB STRING
29 B=R:A=A:GOSUB LIST2
30 AY=B:GOSUB RELEASE
31
32 GOTO QQ_DONE
33
34 QQ_UNQUOTE:
35 R=Z%(A+2)
36 IF (Z%(R)AND 31)<>5 THEN GOTO QQ_SPLICE_UNQUOTE
37 IF S$(Z%(R+1))<>"unquote" THEN GOTO QQ_SPLICE_UNQUOTE
38 REM [ast[1]]
39 R=Z%(Z%(A+1)+2)
40 GOSUB INC_REF_R
41
42 GOTO QQ_DONE
43
44 QQ_SPLICE_UNQUOTE:
45 GOSUB PUSH_A
46 REM rest of cases call quasiquote on ast[1..]
47 A=Z%(A+1):CALL QUASIQUOTE
48 W=R
49 GOSUB POP_A
50
51 REM set A to ast[0] for last two cases
52 A=Z%(A+2)
53
54 REM pair?
55 GOSUB TYPE_A
56 IF T<6 OR T>7 THEN GOTO QQ_DEFAULT
57 IF (Z%(A+1)=0) THEN GOTO QQ_DEFAULT
58
59 B=Z%(A+2)
60 IF (Z%(B)AND 31)<>5 THEN GOTO QQ_DEFAULT
61 IF S$(Z%(B+1))<>"splice-unquote" THEN QQ_DEFAULT
62 REM ['concat, ast[0][1], quasiquote(ast[1..])]
63
64 B=Z%(Z%(A+1)+2)
65 B$="concat":T=5:GOSUB STRING:C=R
66 A=W:GOSUB LIST3
67 REM release inner quasiquoted since outer list takes ownership
68 AY=A:GOSUB RELEASE
69 AY=C:GOSUB RELEASE
70 GOTO QQ_DONE
71
72 QQ_DEFAULT:
73 REM ['cons, quasiquote(ast[0]), quasiquote(ast[1..])]
74
75 Q=W:GOSUB PUSH_Q
76 REM A set above to ast[0]
77 CALL QUASIQUOTE
78 B=R
79 GOSUB POP_Q:W=Q
80
81 B$="cons":T=5:GOSUB STRING:C=R
82 A=W:GOSUB LIST3
83 REM release inner quasiquoted since outer list takes ownership
84 AY=A:GOSUB RELEASE
85 AY=B:GOSUB RELEASE
86 AY=C:GOSUB RELEASE
87 QQ_DONE:
88 END SUB
89
90 REM MACROEXPAND(A, E) -> A:
91 SUB MACROEXPAND
92 GOSUB PUSH_A
93
94 MACROEXPAND_LOOP:
95 REM list?
96 GOSUB TYPE_A
97 IF T<>6 THEN GOTO MACROEXPAND_DONE
98 REM non-empty?
99 IF Z%(A+1)=0 THEN GOTO MACROEXPAND_DONE
100 B=Z%(A+2)
101 REM symbol? in first position
102 IF (Z%(B)AND 31)<>5 THEN GOTO MACROEXPAND_DONE
103 REM defined in environment?
104 K=B:CALL ENV_FIND
105 IF R=-1 THEN GOTO MACROEXPAND_DONE
106 B=R4
107 REM macro?
108 IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE
109
110 F=B:AR=Z%(A+1):CALL APPLY
111 A=R
112
113 GOSUB PEEK_Q:AY=Q
114 REM if previous A was not the first A into macroexpand (i.e. an
115 REM intermediate form) then free it
116 IF A<>AY THEN GOSUB PEND_A_LV
117
118 IF ER<>-2 THEN GOTO MACROEXPAND_DONE
119 GOTO MACROEXPAND_LOOP
120
121 MACROEXPAND_DONE:
122 GOSUB POP_Q: REM pop original A
123 END SUB
124
125 REM EVAL_AST(A, E) -> R
126 SUB EVAL_AST
127 REM push A and E on the stack
128 Q=E:GOSUB PUSH_Q
129 GOSUB PUSH_A
130
131 IF ER<>-2 THEN GOTO EVAL_AST_RETURN
132
133 GOSUB TYPE_A
134 IF T=5 THEN GOTO EVAL_AST_SYMBOL
135 IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
136
137 REM scalar: deref to actual value and inc ref cnt
138 R=A
139 GOSUB INC_REF_R
140 GOTO EVAL_AST_RETURN
141
142 EVAL_AST_SYMBOL:
143 K=A:GOTO ENV_GET
144 ENV_GET_RETURN:
145 GOTO EVAL_AST_RETURN
146
147 EVAL_AST_SEQ:
148 REM setup the stack for the loop
149 GOSUB MAP_LOOP_START
150
151 EVAL_AST_SEQ_LOOP:
152 REM check if we are done evaluating the source sequence
153 IF Z%(A+1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
154
155 REM if we are returning to DO, then skip last element
156 REM The EVAL_DO call to EVAL_AST must be call #2 for EVAL_AST to
157 REM return early and for TCO to work
158 Q=5:GOSUB PEEK_Q_Q
159 IF Q=2 AND Z%(Z%(A+1)+1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
160
161 REM call EVAL for each entry
162 GOSUB PUSH_A
163 IF T<>8 THEN A=Z%(A+2)
164 IF T=8 THEN A=Z%(A+3)
165 Q=T:GOSUB PUSH_Q: REM push/save type
166 CALL EVAL
167 GOSUB POP_Q:T=Q: REM pop/restore type
168 GOSUB POP_A
169 M=R
170
171 REM if error, release the unattached element
172 REM TODO: is R=0 correct?
173 IF ER<>-2 THEN AY=R:GOSUB RELEASE:R=0:GOTO EVAL_AST_SEQ_LOOP_DONE
174
175 REM for hash-maps, copy the key (inc ref since we are going to
176 REM release it below)
177 IF T=8 THEN N=M:M=Z%(A+2):Z%(M)=Z%(M)+32
178
179
180 REM update the return sequence structure
181 REM release N (and M if T=8) since seq takes full ownership
182 C=1:GOSUB MAP_LOOP_UPDATE
183
184 REM process the next sequence entry from source list
185 A=Z%(A+1)
186
187 GOTO EVAL_AST_SEQ_LOOP
188 EVAL_AST_SEQ_LOOP_DONE:
189 REM cleanup stack and get return value
190 GOSUB MAP_LOOP_DONE
191 GOTO EVAL_AST_RETURN
192
193 EVAL_AST_RETURN:
194 REM pop A and E off the stack
195 GOSUB POP_A
196 GOSUB POP_Q:E=Q
197 END SUB
198
199 REM EVAL(A, E) -> R
200 SUB EVAL
201 LV=LV+1: REM track basic return stack level
202
203 REM push A and E on the stack
204 Q=E:GOSUB PUSH_Q
205 GOSUB PUSH_A
206
207 REM PRINT "EVAL A:"+STR$(A)+",X:"+STR$(X)+",LV:"+STR$(LV)+",FRE:"+STR$(FRE(0))
208
209 EVAL_TCO_RECUR:
210
211 IF ER<>-2 THEN GOTO EVAL_RETURN
212
213 REM AZ=A:B=1:GOSUB PR_STR
214 REM PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]"
215
216 GOSUB LIST_Q
217 IF R THEN GOTO APPLY_LIST
218 EVAL_NOT_LIST:
219 REM ELSE
220 CALL EVAL_AST
221 GOTO EVAL_RETURN
222
223 APPLY_LIST:
224 CALL MACROEXPAND
225
226 GOSUB LIST_Q
227 IF R<>1 THEN GOTO EVAL_NOT_LIST
228
229 GOSUB EMPTY_Q
230 IF R THEN R=A:GOSUB INC_REF_R:GOTO EVAL_RETURN
231
232 A0=Z%(A+2)
233
234 REM get symbol in A$
235 IF (Z%(A0)AND 31)<>5 THEN A$=""
236 IF (Z%(A0)AND 31)=5 THEN A$=S$(Z%(A0+1))
237
238 IF A$="def!" THEN GOTO EVAL_DEF
239 IF A$="let*" THEN GOTO EVAL_LET
240 IF A$="quote" THEN GOTO EVAL_QUOTE
241 IF A$="quasiquote" THEN GOTO EVAL_QUASIQUOTE
242 IF A$="defmacro!" THEN GOTO EVAL_DEFMACRO
243 IF A$="macroexpand" THEN GOTO EVAL_MACROEXPAND
244 IF A$="try*" THEN GOTO EVAL_TRY
245 IF A$="do" THEN GOTO EVAL_DO
246 IF A$="if" THEN GOTO EVAL_IF
247 IF A$="fn*" THEN GOTO EVAL_FN
248 GOTO EVAL_INVOKE
249
250 EVAL_GET_A3:
251 A3=Z%(Z%(Z%(Z%(A+1)+1)+1)+2)
252 EVAL_GET_A2:
253 A2=Z%(Z%(Z%(A+1)+1)+2)
254 EVAL_GET_A1:
255 A1=Z%(Z%(A+1)+2)
256 RETURN
257
258 EVAL_DEF:
259 REM PRINT "def!"
260 GOSUB EVAL_GET_A2: REM set A1 and A2
261
262 Q=A1:GOSUB PUSH_Q
263 A=A2:CALL EVAL: REM eval a2
264 GOSUB POP_Q:A1=Q
265
266 IF ER<>-2 THEN GOTO EVAL_RETURN
267
268 REM set a1 in env to a2
269 K=A1:C=R:GOSUB ENV_SET
270 GOTO EVAL_RETURN
271
272 EVAL_LET:
273 REM PRINT "let*"
274 GOSUB EVAL_GET_A2: REM set A1 and A2
275
276 Q=A2:GOSUB PUSH_Q: REM push/save A2
277 Q=E:GOSUB PUSH_Q: REM push env for for later release
278
279 REM create new environment with outer as current environment
280 C=E:GOSUB ENV_NEW
281 E=R
282 EVAL_LET_LOOP:
283 IF Z%(A1+1)=0 THEN GOTO EVAL_LET_LOOP_DONE
284
285 Q=A1:GOSUB PUSH_Q: REM push A1
286 REM eval current A1 odd element
287 A=Z%(Z%(A1+1)+2):CALL EVAL
288 GOSUB POP_Q:A1=Q: REM pop A1
289
290 IF ER<>-2 THEN GOTO EVAL_LET_LOOP_DONE
291
292 REM set key/value in the environment
293 K=Z%(A1+2):C=R:GOSUB ENV_SET
294 AY=R:GOSUB RELEASE: REM release our use, ENV_SET took ownership
295
296 REM skip to the next pair of A1 elements
297 A1=Z%(Z%(A1+1)+1)
298 GOTO EVAL_LET_LOOP
299
300 EVAL_LET_LOOP_DONE:
301 GOSUB POP_Q:AY=Q: REM pop previous env
302
303 REM release previous environment if not the current EVAL env
304 GOSUB PEEK_Q_2
305 IF AY<>Q THEN GOSUB RELEASE
306
307 GOSUB POP_Q:A2=Q: REM pop A2
308 A=A2:GOTO EVAL_TCO_RECUR: REM TCO loop
309
310 EVAL_DO:
311 A=Z%(A+1): REM rest
312 GOSUB PUSH_A: REM push/save A
313
314 REM this must be EVAL_AST call #2 for EVAL_AST to return early
315 REM and for TCO to work
316 CALL EVAL_AST
317
318 REM cleanup
319 AY=R: REM get eval'd list for release
320
321 GOSUB POP_A: REM pop/restore original A for LAST
322 GOSUB LAST: REM get last element for return
323 A=R: REM new recur AST
324
325 REM cleanup
326 GOSUB RELEASE: REM release eval'd list
327 AY=A:GOSUB RELEASE: REM release LAST value (not sure why)
328
329 GOTO EVAL_TCO_RECUR: REM TCO loop
330
331 EVAL_QUOTE:
332 R=Z%(Z%(A+1)+2)
333 GOSUB INC_REF_R
334 GOTO EVAL_RETURN
335
336 EVAL_QUASIQUOTE:
337 R=Z%(Z%(A+1)+2)
338 A=R:CALL QUASIQUOTE
339 A=R
340 REM add quasiquote result to pending release queue to free when
341 REM next lower EVAL level returns (LV)
342 GOSUB PEND_A_LV
343
344 GOTO EVAL_TCO_RECUR: REM TCO loop
345
346 EVAL_DEFMACRO:
347 REM PRINT "defmacro!"
348 GOSUB EVAL_GET_A2: REM set A1 and A2
349
350 Q=A1:GOSUB PUSH_Q: REM push A1
351 A=A2:CALL EVAL: REM eval A2
352 GOSUB POP_Q:A1=Q: REM pop A1
353
354 REM change function to macro
355 Z%(R)=Z%(R)+1
356
357 REM set A1 in env to A2
358 K=A1:C=R:GOSUB ENV_SET
359 GOTO EVAL_RETURN
360
361 EVAL_MACROEXPAND:
362 REM PRINT "macroexpand"
363 R=Z%(Z%(A+1)+2)
364 A=R:CALL MACROEXPAND
365 R=A
366
367 REM since we are returning it unevaluated, inc the ref cnt
368 GOSUB INC_REF_R
369 GOTO EVAL_RETURN
370
371 EVAL_TRY:
372 REM PRINT "try*"
373 GOSUB EVAL_GET_A1: REM set A1
374
375 GOSUB PUSH_A: REM push/save A
376 A=A1:CALL EVAL: REM eval A1
377 GOSUB POP_A: REM pop/restore A
378
379 GOSUB EVAL_GET_A2: REM set A1 and A2
380
381 REM if there is no error or catch block then return
382 IF ER=-2 OR A2=0 THEN GOTO EVAL_RETURN
383
384 REM create environment for the catch block eval
385 C=E:GOSUB ENV_NEW:E=R
386
387 A=A2:GOSUB EVAL_GET_A2: REM set A1 and A2 from catch block
388
389 REM create object for ER=-1 type raw string errors
390 IF ER=-1 THEN B$=E$:T=4:GOSUB STRING:ER=R:GOSUB INC_REF_R
391
392 REM bind the catch symbol to the error object
393 K=A1:C=ER:GOSUB ENV_SET
394 AY=R:GOSUB RELEASE: REM release our use, env took ownership
395
396 REM unset error for catch eval
397 ER=-2:E$=""
398
399 A=A2:CALL EVAL
400
401 GOTO EVAL_RETURN
402
403 EVAL_IF:
404 GOSUB EVAL_GET_A1: REM set A1
405 GOSUB PUSH_A: REM push/save A
406 A=A1:CALL EVAL
407 GOSUB POP_A: REM pop/restore A
408 IF (R=0) OR (R=2) THEN GOTO EVAL_IF_FALSE
409
410 EVAL_IF_TRUE:
411 AY=R:GOSUB RELEASE
412 GOSUB EVAL_GET_A2: REM set A1 and A2 after EVAL
413 A=A2:GOTO EVAL_TCO_RECUR: REM TCO loop
414 EVAL_IF_FALSE:
415 AY=R:GOSUB RELEASE
416 REM if no false case (A3), return nil
417 GOSUB COUNT
418 IF R<4 THEN R=0:GOSUB INC_REF_R:GOTO EVAL_RETURN
419 GOSUB EVAL_GET_A3: REM set A1 - A3 after EVAL
420 A=A3:GOTO EVAL_TCO_RECUR: REM TCO loop
421
422 EVAL_FN:
423 GOSUB EVAL_GET_A2: REM set A1 and A2
424 T=10:L=A2:M=A1:N=E:GOSUB ALLOC: REM mal function
425 GOTO EVAL_RETURN
426
427 EVAL_INVOKE:
428 CALL EVAL_AST
429
430 REM if error, return f/args for release by caller
431 IF ER<>-2 THEN GOTO EVAL_RETURN
432
433 REM push f/args for release after call
434 GOSUB PUSH_R
435
436 AR=Z%(R+1): REM rest
437 F=Z%(R+2)
438
439 REM if metadata, get the actual object
440 GOSUB TYPE_F
441 IF T=14 THEN F=Z%(F+1):GOSUB TYPE_F
442
443 ON T-8 GOTO EVAL_DO_FUNCTION,EVAL_DO_MAL_FUNCTION,EVAL_DO_MAL_FUNCTION
444
445 REM if error, pop and return f/args for release by caller
446 GOSUB POP_R
447 ER=-1:E$="apply of non-function":GOTO EVAL_RETURN
448
449 EVAL_DO_FUNCTION:
450 REM regular function
451 IF Z%(F+1)<60 THEN GOSUB DO_FUNCTION:GOTO EVAL_DO_FUNCTION_SKIP
452 REM for recur functions (apply, map, swap!), use GOTO
453 IF Z%(F+1)>60 THEN CALL DO_TCO_FUNCTION
454 EVAL_DO_FUNCTION_SKIP:
455
456 REM pop and release f/args
457 GOSUB POP_Q:AY=Q
458 GOSUB RELEASE
459 GOTO EVAL_RETURN
460
461 EVAL_DO_MAL_FUNCTION:
462 Q=E:GOSUB PUSH_Q: REM save the current environment for release
463
464 REM create new environ using env and params stored in function
465 C=Z%(F+3):A=Z%(F+2):B=AR:GOSUB ENV_NEW_BINDS
466
467 REM release previous env if it is not the top one on the
468 REM stack (X%(X-2)) because our new env refers to it and
469 REM we no longer need to track it (since we are TCO recurring)
470 GOSUB POP_Q:AY=Q
471 GOSUB PEEK_Q_2
472 IF AY<>Q THEN GOSUB RELEASE
473
474 REM claim the AST before releasing the list containing it
475 A=Z%(F+1):Z%(A)=Z%(A)+32
476 REM add AST to pending release queue to free as soon as EVAL
477 REM actually returns (LV+1)
478 LV=LV+1:GOSUB PEND_A_LV:LV=LV-1
479
480 REM pop and release f/args
481 GOSUB POP_Q:AY=Q
482 GOSUB RELEASE
483
484 REM A set above
485 E=R:GOTO EVAL_TCO_RECUR: REM TCO loop
486
487 EVAL_RETURN:
488 REM AZ=R: B=1: GOSUB PR_STR
489 REM PRINT "EVAL_RETURN R: ["+R$+"] ("+STR$(R)+"), LV:"+STR$(LV)+",ER:"+STR$(ER)
490
491 REM release environment if not the top one on the stack
492 GOSUB PEEK_Q_1
493 IF E<>Q THEN AY=E:GOSUB RELEASE
494
495 LV=LV-1: REM track basic return stack level
496
497 REM release everything we couldn't release earlier
498 GOSUB RELEASE_PEND
499
500 REM trigger GC
501 #cbm T=FRE(0)
502 #qbasic T=0
503
504 REM pop A and E off the stack
505 GOSUB POP_A
506 GOSUB POP_Q:E=Q
507
508 END SUB
509
510 REM PRINT(A) -> R$
511 MAL_PRINT:
512 AZ=A:B=1:GOSUB PR_STR
513 RETURN
514
515 REM RE(A$) -> R
516 REM Assume D has repl_env
517 REM caller must release result
518 RE:
519 R1=-1
520 GOSUB MAL_READ
521 R1=R
522 IF ER<>-2 THEN GOTO RE_DONE
523
524 A=R:E=D:CALL EVAL
525
526 RE_DONE:
527 REM Release memory from MAL_READ
528 AY=R1:GOSUB RELEASE
529 RETURN: REM caller must release result of EVAL
530
531 REM REP(A$) -> R$
532 REM Assume D has repl_env
533 SUB REP
534 R2=-1
535
536 GOSUB RE
537 R2=R
538 IF ER<>-2 THEN GOTO REP_DONE
539
540 A=R:GOSUB MAL_PRINT
541
542 REP_DONE:
543 REM Release memory from MAL_READ and EVAL
544 AY=R2:GOSUB RELEASE
545 END SUB
546
547 REM MAIN program
548 MAIN:
549 GOSUB INIT_MEMORY
550
551 LV=0
552
553 REM create repl_env
554 C=0:GOSUB ENV_NEW:D=R
555
556 REM core.EXT: defined in Basic
557 E=D:GOSUB INIT_CORE_NS: REM set core functions in repl_env
558
559 ZT=ZI: REM top of memory after base repl_env
560
561 REM core.mal: defined using the language itself
562 A$="(def! not (fn* (a) (if a false true)))"
563 GOSUB RE:AY=R:GOSUB RELEASE
564
565 A$="(def! load-file (fn* (f) (eval (read-file f))))"
566 GOSUB RE:AY=R:GOSUB RELEASE
567
568 A$="(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs)"
569 A$=A$+" (if (> (count xs) 1) (nth xs 1) (throw "+CHR$(34)+"odd number of"
570 A$=A$+" forms to cond"+CHR$(34)+")) (cons 'cond (rest (rest xs)))))))"
571 GOSUB RE:AY=R:GOSUB RELEASE
572
573 A$="(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs)"
574 A$=A$+" `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"
575 GOSUB RE:AY=R:GOSUB RELEASE
576
577 REM load the args file
578 A$="(def! -*ARGS*- (load-file "+CHR$(34)+".args.mal"+CHR$(34)+"))"
579 GOSUB RE:AY=R:GOSUB RELEASE
580
581 IF ER>-2 THEN GOSUB PRINT_ERROR:END
582
583 REM set the argument list
584 A$="(def! *ARGV* (rest -*ARGS*-))"
585 GOSUB RE:AY=R:GOSUB RELEASE
586
587 REM get the first argument
588 A$="(first -*ARGS*-)"
589 GOSUB RE
590
591 REM no arguments, start REPL loop
592 IF R<16 THEN GOTO REPL_LOOP
593
594 REM if there is an argument, then run it as a program
595
596 RUN_PROG:
597 REM free up first arg because we get it again
598 AY=R:GOSUB RELEASE
599 REM run a single mal program and exit
600 A$="(load-file (first -*ARGS*-))"
601 GOSUB RE
602 IF ER<>-2 THEN GOSUB PRINT_ERROR
603 GOTO QUIT
604
605 REPL_LOOP:
606 A$="user> ":GOSUB READLINE: REM call input parser
607 IF EZ=1 THEN GOTO QUIT
608 IF R$="" THEN GOTO REPL_LOOP
609
610 A$=R$:CALL REP: REM call REP
611
612 IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
613 PRINT R$
614 GOTO REPL_LOOP
615
616 QUIT:
617 REM GOSUB PR_MEMORY_SUMMARY_SMALL
618 #cbm END
619 #qbasic SYSTEM
620
621 PRINT_ERROR:
622 REM if the error is an object, then print and free it
623 IF ER>=0 THEN AZ=ER:B=0:GOSUB PR_STR:E$=R$:AY=ER:GOSUB RELEASE
624 PRINT "Error: "+E$
625 ER=-2:E$=""
626 RETURN
627