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'
11 REM $INCLUDE: 'debug.in.bas'
18 REM EVAL_AST(A, E) -> R
20 REM push A and E on the stack
24 IF ER
<>-2 THEN GOTO EVAL_AST_RETURN
27 IF T
=5 THEN GOTO EVAL_AST_SYMBOL
28 IF T
>=6 AND T
<=8 THEN GOTO EVAL_AST_SEQ
30 REM scalar: deref to actual value and inc ref cnt
41 REM setup the stack for the loop
45 REM check if we are done evaluating the source sequence
46 IF Z
%(A
+1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
48 REM call EVAL for each entry
50 IF T
<>8 THEN A
=Z
%(A
+2)
52 Q
=T
:GOSUB PUSH_Q
: REM push/save type
54 GOSUB POP_Q
:T
=Q
: REM pop/restore type
58 REM if error, release the unattached element
59 REM TODO: is R=0 correct?
60 IF ER
<>-2 THEN AY
=R
:GOSUB RELEASE
:R
=0:GOTO EVAL_AST_SEQ_LOOP_DONE
62 REM for hash-maps, copy the key (inc ref since we are going to
64 IF T
=8 THEN N
=M
:M
=Z
%(A
+2):Z
%(M
)=Z
%(M
)+32
67 REM update the return sequence structure
68 REM release N (and M if T=8) since seq takes full ownership
69 C
=1:GOSUB MAP_LOOP_UPDATE
71 REM process the next sequence entry from source list
74 GOTO EVAL_AST_SEQ_LOOP
75 EVAL_AST_SEQ_LOOP_DONE:
76 REM cleanup stack and get return value
81 REM pop A and E off the stack
88 LV
=LV
+1: REM track basic return stack level
90 REM push A and E on the stack
94 REM PRINT "EVAL A:"+STR$(A)+",X:"+STR$(X)+",LV:"+STR$(LV)+",FRE:"+STR$(FRE(0))
98 IF ER
<>-2 THEN GOTO EVAL_RETURN
100 REM AZ=A:B=1:GOSUB PR_STR
101 REM PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]"
104 IF R
THEN GOTO APPLY_LIST
111 IF R
THEN R
=A
:GOSUB INC_REF_R
:GOTO EVAL_RETURN
116 IF (Z
%(A0
)AND 31)<>5 THEN A
$=""
117 IF (Z
%(A0
)AND 31)=5 THEN A
$=S$(Z%(A0+1))
119 IF A
$="def!" THEN GOTO EVAL_DEF
120 IF A
$="let*" THEN GOTO EVAL_LET
121 IF A
$="do" THEN GOTO EVAL_DO
122 IF A
$="if" THEN GOTO EVAL_IF
123 IF A
$="fn*" THEN GOTO EVAL_FN
127 A3
=Z
%(Z
%(Z
%(Z
%(A
+1)+1)+1)+2)
129 A2
=Z
%(Z
%(Z
%(A
+1)+1)+2)
136 GOSUB EVAL_GET_A2
: REM set A1 and A2
139 A
=A2
:CALL EVAL
: REM eval a2
142 IF ER
<>-2 THEN GOTO EVAL_RETURN
144 REM set a1 in env to a2
145 K
=A1
:C
=R
:GOSUB ENV_SET
150 GOSUB EVAL_GET_A2
: REM set A1 and A2
152 Q
=A2
:GOSUB PUSH_Q
: REM push/save A2
153 REM create new environment with outer as current environment
157 IF Z
%(A1
+1)=0 THEN GOTO EVAL_LET_LOOP_DONE
159 Q
=A1
:GOSUB PUSH_Q
: REM push A1
160 REM eval current A1 odd element
161 A
=Z
%(Z
%(A1
+1)+2):CALL EVAL
162 GOSUB POP_Q
:A1
=Q
: REM pop A1
164 IF ER
<>-2 THEN GOTO EVAL_LET_LOOP_DONE
166 REM set key/value in the environment
167 K
=Z
%(A1
+2):C
=R
:GOSUB ENV_SET
168 AY
=R
:GOSUB RELEASE
: REM release our use, ENV_SET took ownership
170 REM skip to the next pair of A1 elements
175 GOSUB POP_Q
:A2
=Q
: REM pop A2
176 A
=A2
:CALL EVAL
: REM eval A2 using let_env
183 GOSUB PUSH_R
: REM push eval'd list
184 A
=R
:GOSUB LAST
: REM return the last element
185 GOSUB POP_Q
:AY
=Q
: REM pop eval'd list
186 GOSUB RELEASE
: REM release the eval'd list
190 GOSUB EVAL_GET_A1
: REM set A1
191 GOSUB PUSH_A
: REM push/save A
193 GOSUB POP_A
: REM pop/restore A
194 IF (R
=0) OR (R
=2) THEN GOTO EVAL_IF_FALSE
198 GOSUB EVAL_GET_A2
: REM set A1 and A2 after EVAL
199 A
=A2
:GOTO EVAL_TCO_RECUR
: REM TCO loop
202 REM if no false case (A3), return nil
204 IF R
<4 THEN R
=0:GOSUB INC_REF_R
:GOTO EVAL_RETURN
205 GOSUB EVAL_GET_A3
: REM set A1 - A3 after EVAL
206 A
=A3
:GOTO EVAL_TCO_RECUR
: REM TCO loop
209 GOSUB EVAL_GET_A2
: REM set A1 and A2
210 T
=10:L
=A2
:M
=A1
:N
=E
:GOSUB ALLOC
: REM mal function
216 REM if error, return f/args for release by caller
217 IF ER
<>-2 THEN GOTO EVAL_RETURN
219 REM push f/args for release after call
225 REM if metadata, get the actual object
227 IF T
=14 THEN F
=Z
%(F
+1):GOSUB TYPE_F
229 ON T
-8 GOTO EVAL_DO_FUNCTION
,EVAL_DO_MAL_FUNCTION
,EVAL_DO_MAL_FUNCTION
231 REM if error, pop and return f/args for release by caller
233 ER
=-1:E
$="apply of non
-function":GOTO EVAL_RETURN
237 IF Z%(F+1)<60 THEN GOSUB DO_FUNCTION:GOTO EVAL_DO_FUNCTION_SKIP
238 REM for recur functions (apply, map, swap!), use GOTO
239 IF Z%(F+1)>60 THEN CALL DO_TCO_FUNCTION
240 EVAL_DO_FUNCTION_SKIP:
242 REM pop and release f/args
247 EVAL_DO_MAL_FUNCTION:
248 Q=E:GOSUB PUSH_Q: REM save the current environment for release
250 REM create new environ using env and params stored in function
251 C=Z%(F+3):A=Z%(F+2):B=AR:GOSUB ENV_NEW_BINDS
253 REM release previous env if it is not the top one on the
254 REM stack (X%(X-2)) because our new env refers to it and
255 REM we no longer need to track it (since we are TCO recurring)
258 IF AY<>Q THEN GOSUB RELEASE
260 REM claim the AST before releasing the list containing it
261 A=Z%(F+1):Z%(A)=Z%(A)+32
262 REM add AST to pending release queue to free as soon as EVAL
263 REM actually returns (LV+1)
264 LV=LV+1:GOSUB PEND_A_LV:LV=LV-1
266 REM pop and release f/args
271 E=R:GOTO EVAL_TCO_RECUR: REM TCO loop
274 REM AZ=R: B=1: GOSUB PR_STR
275 REM PRINT "EVAL_RETURN R
: ["+R$+"] ("+STR
$(R)+"), LV
:"+STR$(LV)+",ER:"+STR$(ER)
277 REM release environment if not the top one on the stack
279 IF E<>Q THEN AY=E:GOSUB RELEASE
281 LV=LV-1: REM track basic return stack level
283 REM release everything we couldn't release earlier
290 REM pop A and E off the stack
298 AZ=A:B=1:GOSUB PR_STR
302 REM Assume D has repl_env
303 REM caller must release result
308 IF ER<>-2 THEN GOTO RE_DONE
313 REM Release memory from MAL_READ
315 RETURN: REM caller must release result of EVAL
318 REM Assume D has repl_env
324 IF ER<>-2 THEN GOTO REP_DONE
329 REM Release memory from MAL_READ and EVAL
340 C=0:GOSUB ENV_NEW:D=R
342 REM core.EXT: defined in Basic
343 E=D:GOSUB INIT_CORE_NS: REM set core functions in repl_env
345 ZT=ZI: REM top of memory after base repl_env
347 REM core.mal: defined using the language itself
348 A$="(def! not (fn* (a) (if a false true)))"
349 GOSUB RE
:AY
=R
:GOSUB RELEASE
352 A
$="user> ":GOSUB READLINE: REM call input parser
353 IF EZ=1 THEN GOTO QUIT
354 IF R$="" THEN GOTO REPL_LOOP
356 A$=R$:CALL REP: REM call REP
358 IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
363 REM GOSUB PR_MEMORY_SUMMARY_SMALL