GOTO MAIN
+REM $INCLUDE: 'mem.in.bas'
REM $INCLUDE: 'types.in.bas'
REM $INCLUDE: 'readline.in.bas'
REM $INCLUDE: 'reader.in.bas'
IF ER<>-2 THEN GOTO EVAL_AST_RETURN
- GOSUB DEREF_A
-
- T=Z%(A,0)AND 31
+ GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
REM scalar: deref to actual value and inc ref cnt
- R=A:GOSUB DEREF_R
- Z%(R,0)=Z%(R,0)+32
+ R=A
+ GOSUB INC_REF_R
GOTO EVAL_AST_RETURN
EVAL_AST_SYMBOL:
GOTO EVAL_AST_RETURN
EVAL_AST_SEQ:
- REM allocate the first entry (T already set above)
- L=0:N=0:GOSUB ALLOC
-
- REM push type of sequence
- Q=T:GOSUB PUSH_Q
- REM push sequence index
- Q=0:GOSUB PUSH_Q
- REM push future return value (new sequence)
- GOSUB PUSH_R
- REM push previous new sequence entry
- GOSUB PUSH_R
+ REM setup the stack for the loop
+ GOSUB MAP_LOOP_START
EVAL_AST_SEQ_LOOP:
REM check if we are done evaluating the source sequence
- IF Z%(A,1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
-
- REM if hashmap, skip eval of even entries (keys)
- Q=3:GOSUB PEEK_Q_Q:T=Q
- REM get and update index
- GOSUB PEEK_Q_2
- Q=Q+1:GOSUB PUT_Q_2
- IF T=8 AND ((Q-1)AND 1)=0 THEN GOTO EVAL_AST_DO_REF
- GOTO EVAL_AST_DO_EVAL
-
- EVAL_AST_DO_REF:
- R=A+1:GOSUB DEREF_R: REM deref to target of referred entry
- Z%(R,0)=Z%(R,0)+32: REM inc ref cnt of referred value
- GOTO EVAL_AST_ADD_VALUE
-
- EVAL_AST_DO_EVAL:
- REM call EVAL for each entry
- A=A+1:CALL EVAL
- A=A-1
- GOSUB DEREF_R: REM deref to target of evaluated entry
-
- EVAL_AST_ADD_VALUE:
-
- REM update previous value pointer to evaluated entry
- GOSUB PEEK_Q
- Z%(Q+1,1)=R
-
- IF ER<>-2 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
-
- REM allocate the next entry
- REM same new sequence entry type
- Q=3:GOSUB PEEK_Q_Q:T=Q
- L=0:N=0:GOSUB ALLOC
-
- REM update previous sequence entry value to point to new entry
- GOSUB PEEK_Q
- Z%(Q,1)=R
- REM update previous ptr to current entry
- Q=R:GOSUB PUT_Q
+ IF Z%(A+1)=0 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
+
+ REM call EVAL for each entry
+ GOSUB PUSH_A
+ IF T<>8 THEN A=Z%(A+2)
+ IF T=8 THEN A=Z%(A+3)
+ Q=T:GOSUB PUSH_Q: REM push/save type
+ CALL EVAL
+ GOSUB POP_Q:T=Q: REM pop/restore type
+ GOSUB POP_A
+ M=R
+
+ REM if error, release the unattached element
+ REM TODO: is R=0 correct?
+ IF ER<>-2 THEN AY=R:GOSUB RELEASE:R=0:GOTO EVAL_AST_SEQ_LOOP_DONE
+
+ REM for hash-maps, copy the key (inc ref since we are going to
+ REM release it below)
+ IF T=8 THEN N=M:M=Z%(A+2):Z%(M)=Z%(M)+32
+
+
+ REM update the return sequence structure
+ REM release N (and M if T=8) since seq takes full ownership
+ C=1:GOSUB MAP_LOOP_UPDATE
REM process the next sequence entry from source list
- A=Z%(A,1)
+ A=Z%(A+1)
GOTO EVAL_AST_SEQ_LOOP
EVAL_AST_SEQ_LOOP_DONE:
- GOSUB PEEK_Q_1
- REM if no error, get return value (new seq)
- IF ER=-2 THEN R=Q
- REM otherwise, free the return value and return nil
- IF ER<>-2 THEN R=0:AY=Q:GOSUB RELEASE
-
- REM pop previous, return, index and type
- GOSUB POP_Q:GOSUB POP_Q:GOSUB POP_Q:GOSUB POP_Q
+ REM cleanup stack and get return value
+ GOSUB MAP_LOOP_DONE
GOTO EVAL_AST_RETURN
EVAL_AST_RETURN:
REM AZ=A:B=1:GOSUB PR_STR
REM PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]"
- GOSUB DEREF_A
-
GOSUB LIST_Q
IF R THEN GOTO APPLY_LIST
REM ELSE
APPLY_LIST:
GOSUB EMPTY_Q
- IF R THEN R=A:Z%(R,0)=Z%(R,0)+32:GOTO EVAL_RETURN
+ IF R THEN R=A:GOSUB INC_REF_R:GOTO EVAL_RETURN
- A0=A+1
- R=A0:GOSUB DEREF_R:A0=R
+ A0=Z%(A+2)
REM get symbol in A$
- IF (Z%(A0,0)AND 31)<>5 THEN A$=""
- IF (Z%(A0,0)AND 31)=5 THEN A$=S$(Z%(A0,1))
+ IF (Z%(A0)AND 31)<>5 THEN A$=""
+ IF (Z%(A0)AND 31)=5 THEN A$=S$(Z%(A0+1))
IF A$="def!" THEN GOTO EVAL_DEF
IF A$="let*" THEN GOTO EVAL_LET
GOTO EVAL_INVOKE
EVAL_GET_A3:
- A3=Z%(Z%(Z%(A,1),1),1)+1
- R=A3:GOSUB DEREF_R:A3=R
+ A3=Z%(Z%(Z%(Z%(A+1)+1)+1)+2)
EVAL_GET_A2:
- A2=Z%(Z%(A,1),1)+1
- R=A2:GOSUB DEREF_R:A2=R
+ A2=Z%(Z%(Z%(A+1)+1)+2)
EVAL_GET_A1:
- A1=Z%(A,1)+1
- R=A1:GOSUB DEREF_R:A1=R
+ A1=Z%(Z%(A+1)+2)
RETURN
EVAL_DEF:
C=E:GOSUB ENV_NEW
E=R
EVAL_LET_LOOP:
- IF Z%(A1,1)=0 THEN GOTO EVAL_LET_LOOP_DONE
+ IF Z%(A1+1)=0 THEN GOTO EVAL_LET_LOOP_DONE
Q=A1:GOSUB PUSH_Q: REM push A1
REM eval current A1 odd element
- A=Z%(A1,1)+1:CALL EVAL
+ A=Z%(Z%(A1+1)+2):CALL EVAL
GOSUB POP_Q:A1=Q: REM pop A1
IF ER<>-2 THEN GOTO EVAL_LET_LOOP_DONE
- REM set environment: even A1 key to odd A1 eval'd above
- K=A1+1:C=R:GOSUB ENV_SET
+ REM set key/value in the environment
+ K=Z%(A1+2):C=R:GOSUB ENV_SET
AY=R:GOSUB RELEASE: REM release our use, ENV_SET took ownership
REM skip to the next pair of A1 elements
- A1=Z%(Z%(A1,1),1)
+ A1=Z%(Z%(A1+1)+1)
GOTO EVAL_LET_LOOP
EVAL_LET_LOOP_DONE:
A=A2:CALL EVAL: REM eval A2 using let_env
GOTO EVAL_RETURN
EVAL_DO:
- A=Z%(A,1): REM rest
+ A=Z%(A+1): REM rest
CALL EVAL_AST
GOSUB PUSH_A: REM push/save A
A=A1:CALL EVAL
GOSUB POP_A: REM pop/restore A
- IF (R=0) OR (R=1) THEN GOTO EVAL_IF_FALSE
+ IF (R=0) OR (R=2) THEN GOTO EVAL_IF_FALSE
EVAL_IF_TRUE:
AY=R:GOSUB RELEASE
EVAL_IF_FALSE:
AY=R:GOSUB RELEASE
REM if no false case (A3), return nil
- B=A:GOSUB COUNT
- IF R<4 THEN R=0:GOTO EVAL_RETURN
+ GOSUB COUNT
+ IF R<4 THEN R=0:GOSUB INC_REF_R:GOTO EVAL_RETURN
GOSUB EVAL_GET_A3: REM set A1 - A3 after EVAL
A=A3:GOTO EVAL_TCO_RECUR: REM TCO loop
EVAL_FN:
GOSUB EVAL_GET_A2: REM set A1 and A2
- A=A2:B=A1:GOSUB MAL_FUNCTION
+ T=10:L=A2:M=A1:N=E:GOSUB ALLOC: REM mal function
GOTO EVAL_RETURN
EVAL_INVOKE:
REM push f/args for release after call
GOSUB PUSH_R
- F=R+1
-
- AR=Z%(R,1): REM rest
- R=F:GOSUB DEREF_R:F=R
+ AR=Z%(R+1): REM rest
+ F=Z%(R+2)
REM if metadata, get the actual object
- IF (Z%(F,0)AND 31)>=16 THEN F=Z%(F,1)
+ GOSUB TYPE_F
+ IF T=14 THEN F=Z%(F+1):GOSUB TYPE_F
- IF (Z%(F,0)AND 31)=9 THEN GOTO EVAL_DO_FUNCTION
- IF (Z%(F,0)AND 31)=10 THEN GOTO EVAL_DO_MAL_FUNCTION
+ ON T-8 GOTO EVAL_DO_FUNCTION,EVAL_DO_MAL_FUNCTION,EVAL_DO_MAL_FUNCTION
REM if error, pop and return f/args for release by caller
GOSUB POP_R
EVAL_DO_FUNCTION:
REM regular function
- IF Z%(F,1)<60 THEN GOSUB DO_FUNCTION:GOTO EVAL_DO_FUNCTION_SKIP
+ IF Z%(F+1)<60 THEN GOSUB DO_FUNCTION:GOTO EVAL_DO_FUNCTION_SKIP
REM for recur functions (apply, map, swap!), use GOTO
- IF Z%(F,1)>60 THEN CALL DO_TCO_FUNCTION
+ IF Z%(F+1)>60 THEN CALL DO_TCO_FUNCTION
EVAL_DO_FUNCTION_SKIP:
REM pop and release f/args
GOTO EVAL_RETURN
EVAL_DO_MAL_FUNCTION:
- E4=E: REM save the current environment for release
+ Q=E:GOSUB PUSH_Q: REM save the current environment for release
- REM create new environ using env stored with function
- C=Z%(F+1,1):A=Z%(F+1,0):B=AR:GOSUB ENV_NEW_BINDS
+ REM create new environ using env and params stored in function
+ C=Z%(F+3):A=Z%(F+2):B=AR:GOSUB ENV_NEW_BINDS
REM release previous env if it is not the top one on the
REM stack (X%(X-2)) because our new env refers to it and
REM we no longer need to track it (since we are TCO recurring)
+ GOSUB POP_Q:AY=Q
GOSUB PEEK_Q_2
- IF E4<>Q THEN AY=E4:GOSUB RELEASE
+ IF AY<>Q THEN GOSUB RELEASE
REM claim the AST before releasing the list containing it
- A=Z%(F,1):Z%(A,0)=Z%(A,0)+32
+ A=Z%(F+1):Z%(A)=Z%(A)+32
REM add AST to pending release queue to free as soon as EVAL
REM actually returns (LV+1)
LV=LV+1:GOSUB PEND_A_LV:LV=LV-1
REM Assume D has repl_env
REM caller must release result
RE:
- R1=0
+ R1=-1
GOSUB MAL_READ
R1=R
IF ER<>-2 THEN GOTO RE_DONE
RE_DONE:
REM Release memory from MAL_READ
- IF R1<>0 THEN AY=R1:GOSUB RELEASE
+ AY=R1:GOSUB RELEASE
RETURN: REM caller must release result of EVAL
REM REP(A$) -> R$
REM Assume D has repl_env
SUB REP
- R1=0:R2=0
- GOSUB MAL_READ
- R1=R
- IF ER<>-2 THEN GOTO REP_DONE
+ R2=-1
- A=R:E=D:CALL EVAL
+ GOSUB RE
R2=R
IF ER<>-2 THEN GOTO REP_DONE
A=R:GOSUB MAL_PRINT
- RT$=R$
REP_DONE:
REM Release memory from MAL_READ and EVAL
- IF R2<>0 THEN AY=R2:GOSUB RELEASE
- IF R1<>0 THEN AY=R1:GOSUB RELEASE
- R$=RT$
+ AY=R2:GOSUB RELEASE
END SUB
REM MAIN program
LV=0
REM create repl_env
- C=-1:GOSUB ENV_NEW:D=R
+ C=0:GOSUB ENV_NEW:D=R
REM core.EXT: defined in Basic
E=D:GOSUB INIT_CORE_NS: REM set core functions in repl_env
REPL_LOOP:
A$="user> ":GOSUB READLINE: REM call input parser
IF EZ=1 THEN GOTO QUIT
+ IF R$="" THEN GOTO REPL_LOOP
A$=R$:CALL REP: REM call REP
GOTO REPL_LOOP
QUIT:
- REM GOSUB PR_MEMORY_SUMMARY
- END
+ REM GOSUB PR_MEMORY_SUMMARY_SMALL
+ #cbm END
+ #qbasic SYSTEM
PRINT_ERROR:
PRINT "Error: "+E$