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