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