3 REM $INCLUDE: 'readline.in.bas'
4 REM $INCLUDE: 'types.in.bas'
5 REM $INCLUDE: 'reader.in.bas'
6 REM $INCLUDE: 'printer.in.bas'
8 REM $INCLUDE: 'debug.in.bas'
15 REM EVAL_AST(A%, E%) -> R%
19 REM push A% and E% on the stack
20 ZL
%=ZL
%+2:ZZ
%(ZL
%-1)=E
%:ZZ
%(ZL
%)=A
%
22 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
36 HM
%=E
%:K
%=A
%:GOSUB HASHMAP_GET
38 IF T3
%=0 THEN ER
%=-1:ER
$="'"+ZS$(Z%(A%,1))+"' not found
":GOTO EVAL_AST_RETURN
43 REM allocate the first entry
46 REM make space on the stack
48 REM push type of sequence
50 REM push sequence index
52 REM push future return value (new sequence)
54 REM push previous new sequence entry
58 REM set new sequence entry type (with 1 ref cnt)
59 Z%(R%,0)=ZZ%(ZL%-3)+16
61 REM create value ptr placeholder
66 ZZ%(ZL%-2)=ZZ%(ZL%-2)+1
68 REM check if we are done evaluating the source sequence
69 IF Z%(A%,1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
71 REM if hashmap, skip eval of even entries (keys)
72 IF (ZZ%(ZL%-3)=8) AND ((ZZ%(ZL%-2) AND 1)=0) THEN GOTO EVAL_AST_DO_REF
76 R%=A%+1:GOSUB DEREF_R: REM deref to target of referred entry
77 Z%(R%,0)=Z%(R%,0)+16: REM inc ref cnt of referred value
78 GOTO EVAL_AST_ADD_VALUE
81 REM call EVAL for each entry
84 GOSUB DEREF_R: REM deref to target of evaluated entry
88 REM update previous value pointer to evaluated entry
91 IF ER%<>-2 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
93 REM allocate the next entry
96 REM update previous sequence entry value to point to new entry
98 REM update previous ptr to current entry
101 REM process the next sequence entry from source list
104 GOTO EVAL_AST_SEQ_LOOP
105 EVAL_AST_SEQ_LOOP_DONE:
106 REM get return value (new seq), index, and seq type
108 REM pop previous, return, index and type
113 REM pop A% and E% off the stack
114 E%=ZZ%(ZL%-1):A%=ZZ%(ZL%):ZL%=ZL%-2
119 REM EVAL(A%, E%)) -> R%
121 LV%=LV%+1: REM track basic return stack level
123 REM push A% and E% on the stack
124 ZL%=ZL%+2:ZZ%(ZL%-1)=E%:ZZ%(ZL%)=A%
126 REM AZ%=A%:PR%=1:GOSUB PR_STR
127 REM PRINT "EVAL
: "+R$+" [A%:"+STR
$(A%)+", LV
%:"+STR$(LV%)+"]"
132 IF R% THEN GOTO APPLY_LIST
139 IF R% THEN R%=A%:Z%(R%,0)=Z%(R%,0)+16:GOTO EVAL_RETURN
145 REM if error, return f/args for release by caller
146 IF ER%<>-2 THEN GOTO EVAL_RETURN
149 AR%=Z%(R%,1): REM rest
150 R%=F%:GOSUB DEREF_R:F%=R%
151 IF (Z%(F%,0)AND15)<>9 THEN ER%=-1:ER$="apply of non-function":GOTO EVAL_RETURN
153 AY
%=R3
%:GOSUB RELEASE
158 LV
%=LV
%-1: REM track basic return stack level
164 REM pop A% and E% off the stack
165 E
%=ZZ
%(ZL
%-1):A
%=ZZ
%(ZL
%):ZL
%=ZL
%-2
169 REM DO_FUNCTION(F%, AR%)
176 REM Get the function number
179 REM Get argument values
180 R
%=AR
%+1:GOSUB DEREF_R
:AA
%=Z
%(R
%,1)
181 R
%=Z
%(AR
%,1)+1:GOSUB DEREF_R
:AB
%=Z
%(R
%,1)
183 REM Allocate the return value
186 REM Switch on the function number
187 IF FF
%=1 THEN GOTO DO_ADD
188 IF FF
%=2 THEN GOTO DO_SUB
189 IF FF
%=3 THEN GOTO DO_MULT
190 IF FF
%=4 THEN GOTO DO_DIV
191 ER
%=-1:ER
$="unknown function"+STR$(FF%):RETURN
196 GOTO DO_FUNCTION_DONE
200 GOTO DO_FUNCTION_DONE
204 GOTO DO_FUNCTION_DONE
208 GOTO DO_FUNCTION_DONE
215 AZ%=A%:PR%=1:GOSUB PR_STR
219 REM Assume RE% has repl_env
224 IF ER%<>-2 THEN GOTO REP_DONE
226 A%=R%:E%=RE%:GOSUB EVAL
228 IF ER%<>-2 THEN GOTO REP_DONE
230 A%=R%:GOSUB MAL_PRINT
234 REM Release memory from MAL_READ and EVAL
235 IF R2%<>0 THEN AY%=R2%:GOSUB RELEASE
236 IF R1%<>0 THEN AY%=R1%:GOSUB RELEASE
250 A%=1:GOSUB NATIVE_FUNCTION
251 HM%=RE%:K$="+":V%=R%:GOSUB ASSOC1_S:RE%=R%
254 A%=2:GOSUB NATIVE_FUNCTION
255 HM%=RE%:K$="-":V%=R%:GOSUB ASSOC1_S:RE%=R%
258 A%=3:GOSUB NATIVE_FUNCTION
259 HM%=RE%:K$="*":V%=R%:GOSUB ASSOC1_S:RE%=R%
262 A%=4:GOSUB NATIVE_FUNCTION
263 HM%=RE%:K$="/":V%=R%:GOSUB ASSOC1_S:RE%=R%
265 ZT%=ZI%: REM top of memory after base repl_env
268 A$="user> ":GOSUB READLINE
: REM call input parser
269 IF EOF
=1 THEN GOTO QUIT
271 A
$=R$:GOSUB REP
: REM call REP
273 IF ER
%<>-2 THEN GOSUB PRINT_ERROR
:GOTO REPL_LOOP
278 REM P1%=ZT%: P2%=-1: GOSUB PR_MEMORY
279 GOSUB PR_MEMORY_SUMMARY