2016-09-20 05:23:21 +03:00
|
|
|
GOTO MAIN
|
|
|
|
|
|
|
|
REM $INCLUDE: 'readline.in.bas'
|
|
|
|
REM $INCLUDE: 'types.in.bas'
|
|
|
|
REM $INCLUDE: 'reader.in.bas'
|
|
|
|
REM $INCLUDE: 'printer.in.bas'
|
|
|
|
REM $INCLUDE: 'env.in.bas'
|
|
|
|
REM $INCLUDE: 'core.in.bas'
|
|
|
|
|
2016-09-22 07:27:12 +03:00
|
|
|
REM $INCLUDE: 'debug.in.bas'
|
|
|
|
|
2016-09-20 05:23:21 +03:00
|
|
|
REM READ(A$) -> R%
|
|
|
|
MAL_READ:
|
|
|
|
GOSUB READ_STR
|
|
|
|
RETURN
|
|
|
|
|
|
|
|
REM EVAL_AST(A%, E%) -> R%
|
|
|
|
REM called using GOTO to avoid basic return address stack usage
|
|
|
|
REM top of stack should have return label index
|
|
|
|
EVAL_AST:
|
|
|
|
REM push A% and E% on the stack
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+2:ZZ%(ZL%-1)=E%:ZZ%(ZL%)=A%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO EVAL_AST_RETURN
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
GOSUB DEREF_A
|
|
|
|
|
|
|
|
T%=Z%(A%,0)AND15
|
2016-09-24 06:36:17 +03:00
|
|
|
IF T%=5 THEN GOTO EVAL_AST_SYMBOL
|
|
|
|
IF T%>=6 AND T%<=8 THEN GOTO EVAL_AST_SEQ
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM scalar: deref to actual value and inc ref cnt
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=A%:GOSUB DEREF_R
|
2016-09-20 05:23:21 +03:00
|
|
|
Z%(R%,0)=Z%(R%,0)+16
|
|
|
|
GOTO EVAL_AST_RETURN
|
|
|
|
|
|
|
|
EVAL_AST_SYMBOL:
|
2016-09-24 06:36:17 +03:00
|
|
|
K%=A%:GOSUB ENV_GET
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_AST_RETURN
|
|
|
|
|
|
|
|
EVAL_AST_SEQ:
|
|
|
|
REM allocate the first entry
|
2016-09-24 06:36:17 +03:00
|
|
|
SZ%=2:GOSUB ALLOC
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM make space on the stack
|
|
|
|
ZL%=ZL%+4
|
|
|
|
REM push type of sequence
|
|
|
|
ZZ%(ZL%-3)=T%
|
|
|
|
REM push sequence index
|
|
|
|
ZZ%(ZL%-2)=-1
|
|
|
|
REM push future return value (new sequence)
|
|
|
|
ZZ%(ZL%-1)=R%
|
|
|
|
REM push previous new sequence entry
|
|
|
|
ZZ%(ZL%)=R%
|
|
|
|
|
|
|
|
EVAL_AST_SEQ_LOOP:
|
|
|
|
REM set new sequence entry type (with 1 ref cnt)
|
|
|
|
Z%(R%,0)=ZZ%(ZL%-3)+16
|
|
|
|
Z%(R%,1)=0
|
|
|
|
REM create value ptr placeholder
|
|
|
|
Z%(R%+1,0)=14
|
|
|
|
Z%(R%+1,1)=0
|
|
|
|
|
|
|
|
REM update index
|
|
|
|
ZZ%(ZL%-2)=ZZ%(ZL%-2)+1
|
|
|
|
|
|
|
|
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)
|
|
|
|
IF (ZZ%(ZL%-3)=8) AND ((ZZ%(ZL%-2) AND 1)=0) THEN GOTO EVAL_AST_DO_REF
|
|
|
|
GOTO EVAL_AST_DO_EVAL
|
|
|
|
|
|
|
|
EVAL_AST_DO_REF:
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=A%+1:GOSUB DEREF_R: REM deref to target of referred entry
|
2016-09-20 05:23:21 +03:00
|
|
|
Z%(R%,0)=Z%(R%,0)+16: REM inc ref cnt of referred value
|
|
|
|
GOTO EVAL_AST_ADD_VALUE
|
|
|
|
|
|
|
|
EVAL_AST_DO_EVAL:
|
|
|
|
REM call EVAL for each entry
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=A%+1:GOSUB EVAL
|
2016-09-20 05:23:21 +03:00
|
|
|
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
|
|
|
|
Z%(ZZ%(ZL%)+1,1)=R%
|
|
|
|
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO EVAL_AST_SEQ_LOOP_DONE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM allocate the next entry
|
2016-09-24 06:36:17 +03:00
|
|
|
SZ%=2:GOSUB ALLOC
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM update previous sequence entry value to point to new entry
|
|
|
|
Z%(ZZ%(ZL%),1)=R%
|
|
|
|
REM update previous ptr to current entry
|
|
|
|
ZZ%(ZL%)=R%
|
|
|
|
|
|
|
|
REM process the next sequence entry from source list
|
|
|
|
A%=Z%(A%,1)
|
|
|
|
|
|
|
|
GOTO EVAL_AST_SEQ_LOOP
|
|
|
|
EVAL_AST_SEQ_LOOP_DONE:
|
2016-09-22 07:27:12 +03:00
|
|
|
REM if no error, get return value (new seq)
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%=-2 THEN R%=ZZ%(ZL%-1)
|
2016-09-22 07:27:12 +03:00
|
|
|
REM otherwise, free the return value and return nil
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN R%=0:AY%=ZZ%(ZL%-1):GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM pop previous, return, index and type
|
|
|
|
ZL%=ZL%-4
|
|
|
|
GOTO EVAL_AST_RETURN
|
|
|
|
|
|
|
|
EVAL_AST_RETURN:
|
|
|
|
REM pop A% and E% off the stack
|
2016-09-24 06:36:17 +03:00
|
|
|
E%=ZZ%(ZL%-1):A%=ZZ%(ZL%):ZL%=ZL%-2
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM pop EVAL AST return label/address
|
2016-09-24 06:36:17 +03:00
|
|
|
RN%=ZZ%(ZL%):ZL%=ZL%-1
|
|
|
|
ON RN% GOTO EVAL_AST_RETURN_1,EVAL_AST_RETURN_2,EVAL_AST_RETURN_3
|
2016-09-20 05:23:21 +03:00
|
|
|
RETURN
|
|
|
|
|
|
|
|
REM EVAL(A%, E%)) -> R%
|
|
|
|
EVAL:
|
|
|
|
LV%=LV%+1: REM track basic return stack level
|
|
|
|
|
|
|
|
REM push A% and E% on the stack
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+2:ZZ%(ZL%-1)=E%:ZZ%(ZL%)=A%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
EVAL_TCO_RECUR:
|
|
|
|
|
2016-10-07 06:22:57 +03:00
|
|
|
REM AZ%=A%:PR%=1:GOSUB PR_STR
|
|
|
|
REM PRINT "EVAL: "+R$+" [A%:"+STR$(A%)+", LV%:"+STR$(LV%)+"]"
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
GOSUB DEREF_A
|
|
|
|
|
|
|
|
GOSUB LIST_Q
|
|
|
|
IF R% THEN GOTO APPLY_LIST
|
|
|
|
REM ELSE
|
|
|
|
REM push EVAL_AST return label/address
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=1
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_AST
|
|
|
|
EVAL_AST_RETURN_1:
|
|
|
|
|
|
|
|
GOTO EVAL_RETURN
|
|
|
|
|
|
|
|
APPLY_LIST:
|
|
|
|
GOSUB EMPTY_Q
|
2016-09-24 06:36:17 +03:00
|
|
|
IF R% THEN R%=A%:Z%(R%,0)=Z%(R%,0)+16:GOTO EVAL_RETURN
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
A0%=A%+1
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=A0%:GOSUB DEREF_R:A0%=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM get symbol in A$
|
|
|
|
IF (Z%(A0%,0)AND15)<>5 THEN A$=""
|
|
|
|
IF (Z%(A0%,0)AND15)=5 THEN A$=ZS$(Z%(A0%,1))
|
|
|
|
|
|
|
|
IF A$="def!" THEN GOTO EVAL_DEF
|
|
|
|
IF A$="let*" THEN GOTO EVAL_LET
|
|
|
|
IF A$="do" THEN GOTO EVAL_DO
|
|
|
|
IF A$="if" THEN GOTO EVAL_IF
|
|
|
|
IF A$="fn*" THEN GOTO EVAL_FN
|
|
|
|
GOTO EVAL_INVOKE
|
|
|
|
|
|
|
|
EVAL_GET_A3:
|
2016-09-21 05:11:46 +03:00
|
|
|
A3%=Z%(Z%(Z%(A%,1),1),1)+1
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=A3%:GOSUB DEREF_R:A3%=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
EVAL_GET_A2:
|
2016-09-21 05:11:46 +03:00
|
|
|
A2%=Z%(Z%(A%,1),1)+1
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=A2%:GOSUB DEREF_R:A2%=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
EVAL_GET_A1:
|
2016-09-21 05:11:46 +03:00
|
|
|
A1%=Z%(A%,1)+1
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=A1%:GOSUB DEREF_R:A1%=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
RETURN
|
|
|
|
|
|
|
|
EVAL_DEF:
|
|
|
|
REM PRINT "def!"
|
|
|
|
GOSUB EVAL_GET_A2: REM set a1% and a2%
|
|
|
|
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=A1%: REM push A1%
|
|
|
|
A%=A2%:GOSUB EVAL: REM eval a2
|
|
|
|
A1%=ZZ%(ZL%):ZL%=ZL%-1: REM pop A1%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO EVAL_RETURN
|
2016-10-07 06:22:57 +03:00
|
|
|
|
2016-09-20 05:23:21 +03:00
|
|
|
REM set a1 in env to a2
|
2016-09-24 06:36:17 +03:00
|
|
|
K%=A1%:V%=R%:GOSUB ENV_SET
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_RETURN
|
|
|
|
|
|
|
|
EVAL_LET:
|
|
|
|
REM PRINT "let*"
|
|
|
|
GOSUB EVAL_GET_A2: REM set a1% and a2%
|
|
|
|
|
2016-10-07 06:22:57 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=A2%: REM push/save A2%
|
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=E%: REM push env for for later release
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM create new environment with outer as current environment
|
2016-09-24 06:36:17 +03:00
|
|
|
EO%=E%:GOSUB ENV_NEW
|
2016-09-20 05:23:21 +03:00
|
|
|
E%=R%
|
|
|
|
EVAL_LET_LOOP:
|
|
|
|
IF Z%(A1%,1)=0 THEN GOTO EVAL_LET_LOOP_DONE
|
|
|
|
|
2016-10-07 06:22:57 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=A1%: REM push A1%
|
2016-09-20 05:23:21 +03:00
|
|
|
REM eval current A1 odd element
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=Z%(A1%,1)+1:GOSUB EVAL
|
2016-10-07 06:22:57 +03:00
|
|
|
A1%=ZZ%(ZL%):ZL%=ZL%-1: REM pop A1%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM set environment: even A1% key to odd A1% eval'd above
|
2016-09-24 06:36:17 +03:00
|
|
|
K%=A1%+1:V%=R%:GOSUB ENV_SET
|
|
|
|
AY%=R%:GOSUB RELEASE: REM release our use, ENV_SET took ownership
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM skip to the next pair of A1% elements
|
|
|
|
A1%=Z%(Z%(A1%,1),1)
|
|
|
|
GOTO EVAL_LET_LOOP
|
2016-10-07 06:22:57 +03:00
|
|
|
|
2016-09-20 05:23:21 +03:00
|
|
|
EVAL_LET_LOOP_DONE:
|
2016-10-07 06:22:57 +03:00
|
|
|
E4%=ZZ%(ZL%):ZL%=ZL%-1: REM pop previous env
|
|
|
|
|
|
|
|
REM release previous environment if not the current EVAL env
|
|
|
|
IF E4%<>ZZ%(ZL%-2) THEN AY%=E4%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-10-07 06:22:57 +03:00
|
|
|
A2%=ZZ%(ZL%):ZL%=ZL%-1: REM pop A2%
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=A2%:GOTO EVAL_TCO_RECUR: REM TCO loop
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
EVAL_DO:
|
|
|
|
A%=Z%(A%,1): REM rest
|
|
|
|
|
|
|
|
REM TODO: TCO
|
|
|
|
|
|
|
|
REM push EVAL_AST return label/address
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=2
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_AST
|
|
|
|
EVAL_AST_RETURN_2:
|
|
|
|
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=R%: REM push eval'd list
|
|
|
|
A%=R%:GOSUB LAST: REM return the last element
|
|
|
|
AY%=ZZ%(ZL%):ZL%=ZL%-1: REM pop eval'd list
|
2016-09-20 05:23:21 +03:00
|
|
|
GOSUB RELEASE: REM release the eval'd list
|
|
|
|
GOTO EVAL_RETURN
|
|
|
|
|
|
|
|
EVAL_IF:
|
|
|
|
GOSUB EVAL_GET_A1: REM set a1%
|
|
|
|
REM push A%
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=A%
|
|
|
|
A%=A1%:GOSUB EVAL
|
2016-09-20 05:23:21 +03:00
|
|
|
REM pop A%
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=ZZ%(ZL%):ZL%=ZL%-1
|
2016-09-20 05:23:21 +03:00
|
|
|
IF (R%=0) OR (R%=1) THEN GOTO EVAL_IF_FALSE
|
|
|
|
|
|
|
|
EVAL_IF_TRUE:
|
2016-09-24 06:36:17 +03:00
|
|
|
AY%=R%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
GOSUB EVAL_GET_A2: REM set a1% and a2% after EVAL
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=A2%:GOTO EVAL_TCO_RECUR: REM TCO loop
|
2016-09-20 05:23:21 +03:00
|
|
|
EVAL_IF_FALSE:
|
2016-09-24 06:36:17 +03:00
|
|
|
AY%=R%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
REM if no false case (A3%), return nil
|
2016-09-24 06:36:17 +03:00
|
|
|
IF Z%(Z%(Z%(A%,1),1),1)=0 THEN R%=0:GOTO EVAL_RETURN
|
2016-09-20 05:23:21 +03:00
|
|
|
GOSUB EVAL_GET_A3: REM set a1% - a3% after EVAL
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=A3%:GOTO EVAL_TCO_RECUR: REM TCO loop
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
EVAL_FN:
|
|
|
|
GOSUB EVAL_GET_A2: REM set a1% and a2%
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=A2%:P%=A1%:GOSUB MAL_FUNCTION
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_RETURN
|
|
|
|
|
|
|
|
EVAL_INVOKE:
|
|
|
|
REM push EVAL_AST return label/address
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=3
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_AST
|
|
|
|
EVAL_AST_RETURN_3:
|
|
|
|
|
|
|
|
REM if error, return f/args for release by caller
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO EVAL_RETURN
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM push f/args for release after call
|
2016-09-24 06:36:17 +03:00
|
|
|
ZL%=ZL%+1:ZZ%(ZL%)=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
F%=R%+1
|
|
|
|
|
|
|
|
AR%=Z%(R%,1): REM rest
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=F%:GOSUB DEREF_R:F%=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
IF (Z%(F%,0)AND15)=9 THEN GOTO EVAL_DO_FUNCTION
|
|
|
|
IF (Z%(F%,0)AND15)=10 THEN GOTO EVAL_DO_MAL_FUNCTION
|
|
|
|
|
|
|
|
REM if error, pop and return f/args for release by caller
|
2016-09-24 06:36:17 +03:00
|
|
|
R%=ZZ%(ZL%):ZL%=ZL%-1
|
2016-10-09 07:41:33 +03:00
|
|
|
ER%=-1:ER$="apply of non-function":GOTO EVAL_RETURN
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
EVAL_DO_FUNCTION:
|
|
|
|
GOSUB DO_FUNCTION
|
|
|
|
|
|
|
|
REM pop and release f/args
|
2016-09-24 06:36:17 +03:00
|
|
|
AY%=ZZ%(ZL%):ZL%=ZL%-1:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
GOTO EVAL_RETURN
|
|
|
|
|
|
|
|
EVAL_DO_MAL_FUNCTION:
|
|
|
|
E4%=E%: REM save the current environment for release
|
|
|
|
|
|
|
|
REM create new environ using env stored with function
|
2016-09-24 06:36:17 +03:00
|
|
|
EO%=Z%(F%+1,1):BI%=Z%(F%+1,0):EX%=AR%:GOSUB ENV_NEW_BINDS
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM release previous env if it is not the top one on the
|
|
|
|
REM stack (ZZ%(ZL%-2)) because our new env refers to it and
|
|
|
|
REM we no longer need to track it (since we are TCO recurring)
|
2016-09-24 06:36:17 +03:00
|
|
|
IF E4%<>ZZ%(ZL%-2) THEN AY%=E4%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM claim the AST before releasing the list containing it
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=Z%(F%,1):Z%(A%,0)=Z%(A%,0)+16
|
2016-09-22 07:27:12 +03:00
|
|
|
REM add AST to pending release queue to free as soon as EVAL
|
|
|
|
REM actually returns (LV%+1)
|
2016-09-24 06:36:17 +03:00
|
|
|
ZM%=ZM%+1:ZR%(ZM%,0)=A%:ZR%(ZM%,1)=LV%+1
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM pop and release f/args
|
2016-09-24 06:36:17 +03:00
|
|
|
AY%=ZZ%(ZL%):ZL%=ZL%-1:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM A% set above
|
2016-09-24 06:36:17 +03:00
|
|
|
E%=R%:GOTO EVAL_TCO_RECUR: REM TCO loop
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
EVAL_RETURN:
|
2016-09-22 07:27:12 +03:00
|
|
|
REM AZ%=R%: PR%=1: GOSUB PR_STR
|
|
|
|
REM PRINT "EVAL_RETURN R%: ["+R$+"] ("+STR$(R%)+"), LV%:"+STR$(LV%)+",ER%:"+STR$(ER%)
|
|
|
|
|
2016-09-20 05:23:21 +03:00
|
|
|
REM release environment if not the top one on the stack
|
2016-09-24 06:36:17 +03:00
|
|
|
IF E%<>ZZ%(ZL%-1) THEN AY%=E%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-09-22 07:27:12 +03:00
|
|
|
LV%=LV%-1: REM track basic return stack level
|
|
|
|
|
2016-09-20 05:23:21 +03:00
|
|
|
REM release everything we couldn't release earlier
|
|
|
|
GOSUB RELEASE_PEND
|
|
|
|
|
|
|
|
REM trigger GC
|
|
|
|
TA%=FRE(0)
|
|
|
|
|
|
|
|
REM pop A% and E% off the stack
|
2016-09-24 06:36:17 +03:00
|
|
|
E%=ZZ%(ZL%-1):A%=ZZ%(ZL%):ZL%=ZL%-2
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
RETURN
|
|
|
|
|
|
|
|
REM PRINT(A%) -> R$
|
|
|
|
MAL_PRINT:
|
2016-09-24 06:36:17 +03:00
|
|
|
AZ%=A%:PR%=1:GOSUB PR_STR
|
2016-09-20 05:23:21 +03:00
|
|
|
RETURN
|
|
|
|
|
|
|
|
REM RE(A$) -> R%
|
|
|
|
REM Assume RE% has repl_env
|
|
|
|
REM caller must release result
|
|
|
|
RE:
|
|
|
|
R1%=0
|
|
|
|
GOSUB MAL_READ
|
|
|
|
R1%=R%
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO REP_DONE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=R%:E%=RE%:GOSUB EVAL
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REP_DONE:
|
|
|
|
REM Release memory from MAL_READ
|
2016-09-24 06:36:17 +03:00
|
|
|
IF R1%<>0 THEN AY%=R1%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
RETURN: REM caller must release result of EVAL
|
|
|
|
|
|
|
|
REM REP(A$) -> R$
|
|
|
|
REM Assume RE% has repl_env
|
|
|
|
REP:
|
2016-09-24 06:36:17 +03:00
|
|
|
R1%=0:R2%=0
|
2016-09-20 05:23:21 +03:00
|
|
|
GOSUB MAL_READ
|
|
|
|
R1%=R%
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO REP_DONE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=R%:E%=RE%:GOSUB EVAL
|
2016-09-20 05:23:21 +03:00
|
|
|
R2%=R%
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOTO REP_DONE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-09-24 06:36:17 +03:00
|
|
|
A%=R%:GOSUB MAL_PRINT
|
2016-09-20 05:23:21 +03:00
|
|
|
RT$=R$
|
|
|
|
|
|
|
|
REP_DONE:
|
|
|
|
REM Release memory from MAL_READ and EVAL
|
2016-09-24 06:36:17 +03:00
|
|
|
IF R2%<>0 THEN AY%=R2%:GOSUB RELEASE
|
|
|
|
IF R1%<>0 THEN AY%=R1%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
R$=RT$
|
|
|
|
RETURN
|
|
|
|
|
|
|
|
REM MAIN program
|
|
|
|
MAIN:
|
|
|
|
GOSUB INIT_MEMORY
|
|
|
|
|
|
|
|
LV%=0
|
|
|
|
|
|
|
|
REM create repl_env
|
2016-09-24 06:36:17 +03:00
|
|
|
EO%=-1:GOSUB ENV_NEW:RE%=R%
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM core.EXT: defined in Basic
|
2016-09-24 06:36:17 +03:00
|
|
|
E%=RE%:GOSUB INIT_CORE_NS: REM set core functions in repl_env
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
ZT%=ZI%: REM top of memory after base repl_env
|
|
|
|
|
|
|
|
REM core.mal: defined using the language itself
|
2016-09-24 06:36:17 +03:00
|
|
|
A$="(def! not (fn* (a) (if a false true)))"
|
|
|
|
GOSUB RE:AY%=R%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
A$="(def! load-file (fn* (f) (eval (read-string (str "
|
2016-09-24 06:36:17 +03:00
|
|
|
A$=A$+CHR$(34)+"(do "+CHR$(34)+" (slurp f) "+CHR$(34)+")"+CHR$(34)+")))))"
|
|
|
|
GOSUB RE:AY%=R%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM load the args file
|
|
|
|
A$="(def! -*ARGS*- (load-file "+CHR$(34)+".args.mal"+CHR$(34)+"))"
|
2016-09-24 06:36:17 +03:00
|
|
|
GOSUB RE:AY%=R%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM set the argument list
|
2016-09-24 06:36:17 +03:00
|
|
|
A$="(def! *ARGV* (rest -*ARGS*-))"
|
|
|
|
GOSUB RE:AY%=R%:GOSUB RELEASE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM get the first argument
|
2016-09-24 06:36:17 +03:00
|
|
|
A$="(first -*ARGS*-)"
|
|
|
|
GOSUB RE
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REM if there is an argument, then run it as a program
|
2016-09-24 06:36:17 +03:00
|
|
|
IF R%<>0 THEN AY%=R%:GOSUB RELEASE:GOTO RUN_PROG
|
2016-09-20 05:23:21 +03:00
|
|
|
REM no arguments, start REPL loop
|
|
|
|
IF R%=0 THEN GOTO REPL_LOOP
|
|
|
|
|
|
|
|
RUN_PROG:
|
|
|
|
REM run a single mal program and exit
|
2016-09-24 06:36:17 +03:00
|
|
|
A$="(load-file (first -*ARGS*-))"
|
|
|
|
GOSUB RE
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOSUB PRINT_ERROR
|
2016-09-22 07:27:12 +03:00
|
|
|
END
|
2016-09-20 05:23:21 +03:00
|
|
|
|
|
|
|
REPL_LOOP:
|
2016-09-24 06:36:17 +03:00
|
|
|
A$="user> ":GOSUB READLINE: REM call input parser
|
2016-09-20 05:23:21 +03:00
|
|
|
IF EOF=1 THEN GOTO QUIT
|
|
|
|
|
2016-09-24 06:36:17 +03:00
|
|
|
A$=R$:GOSUB REP: REM call REP
|
2016-09-20 05:23:21 +03:00
|
|
|
|
2016-10-09 07:41:33 +03:00
|
|
|
IF ER%<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
|
2016-09-20 05:23:21 +03:00
|
|
|
PRINT R$
|
|
|
|
GOTO REPL_LOOP
|
|
|
|
|
|
|
|
QUIT:
|
|
|
|
REM P1%=ZT%: P2%=-1: GOSUB PR_MEMORY
|
|
|
|
GOSUB PR_MEMORY_SUMMARY
|
|
|
|
END
|
|
|
|
|
|
|
|
PRINT_ERROR:
|
2016-09-21 05:11:46 +03:00
|
|
|
PRINT "Error: "+ER$
|
2016-10-09 07:41:33 +03:00
|
|
|
ER%=-2:ER$=""
|
2016-09-20 05:23:21 +03:00
|
|
|
RETURN
|
|
|
|
|