Skip to content

Commit

Permalink
basic: merge eval_ast and macroexpand into EVAL
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Nov 14, 2024
1 parent 2a8a4ec commit 0ec7cc3
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 358 deletions.
77 changes: 50 additions & 27 deletions impls/basic/step2_eval.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,8 @@ SUB EVAL_AST
IF ER<>-2 THEN GOTO EVAL_AST_RETURN

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ
IF T<6 OR 8<T THEN R=-1:ER=-1:E$="EVAL_AST: bad type":GOTO EVAL_AST_RETURN

REM scalar: deref to actual value and inc ref cnt
R=A
GOSUB INC_REF_R
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
H=E:B$=S$(Z%(A+1)):GOSUB HASHMAP_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+B$+"' not found":GOTO EVAL_AST_RETURN
GOSUB INC_REF_R
GOTO EVAL_AST_RETURN

EVAL_AST_SEQ:
REM setup the stack for the loop
GOSUB MAP_LOOP_START

Expand Down Expand Up @@ -94,32 +81,68 @@ SUB EVAL
REM AZ=A:B=1:GOSUB PR_STR
REM PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]"

GOSUB LIST_Q
IF R THEN GOTO APPLY_LIST
GOSUB TYPE_A
T=T-4
IF 0<T THEN ON T GOTO EVAL_SYMBOL,APPLY_LIST,EVAL_VECTOR,EVAL_MAP

REM ELSE
R=A
GOSUB INC_REF_R
GOTO EVAL_RETURN

EVAL_SYMBOL:
H=E:B$=S$(Z%(A+1)):GOSUB HASHMAP_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+B$+"' not found":GOTO EVAL_RETURN
GOSUB INC_REF_R
GOTO EVAL_RETURN

EVAL_MAP:
EVAL_VECTOR:
CALL EVAL_AST
GOTO EVAL_RETURN

APPLY_LIST:

GOSUB EMPTY_Q
IF R THEN R=A:GOSUB INC_REF_R:GOTO EVAL_RETURN

EVAL_INVOKE:
CALL EVAL_AST
W=R

REM if error, return f/args for release by caller
REM evaluate A0
GOSUB PUSH_A
A=A0:CALL EVAL
GOSUB POP_A
IF ER<>-2 THEN GOTO EVAL_RETURN

AR=Z%(R+1): REM rest
F=Z%(R+2)
F=R

GOSUB TYPE_F
IF T<>9 THEN R=-1:ER=-1:E$="apply of non-function":GOTO EVAL_INVOKE_DONE
GOSUB DO_FUNCTION
EVAL_INVOKE_DONE:
AY=W:GOSUB RELEASE
GOTO EVAL_RETURN

REM ON .. GOTO here reduces the diff with later steps.
T=T-8
IF 0<T THEN ON T GOTO EVAL_DO_FUNCTION

REM if error, pop and return f for release by caller
GOSUB POP_R
ER=-1:E$="apply of non-function":GOTO EVAL_RETURN

EVAL_DO_FUNCTION:
REM regular function

REM Evaluate the arguments
A=Z%(A+1):CALL EVAL_AST
IF ER<>-2 THEN GOSUB POP_Q:AY=Q:GOSUB RELEASE:GOTO EVAL_RETURN

REM set F and AR, push AR (after F) in the stack for release after call
GOSUB PEEK_Q:F=Q
GOSUB PUSH_R
AR=R

GOSUB DO_FUNCTION

REM pop and release f/args
GOSUB POP_Q:AY=Q:GOSUB RELEASE
GOSUB POP_Q:AY=Q
GOSUB RELEASE

EVAL_RETURN:
REM AZ=R: B=1: GOSUB PR_STR
Expand Down
77 changes: 50 additions & 27 deletions impls/basic/step3_env.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,8 @@ SUB EVAL_AST
IF ER<>-2 THEN GOTO EVAL_AST_RETURN

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ
IF T<6 OR 8<T THEN R=-1:ER=-1:E$="EVAL_AST: bad type":GOTO EVAL_AST_RETURN

REM scalar: deref to actual value and inc ref cnt
R=A
GOSUB INC_REF_R
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
B$=S$(Z%(A+1)):CALL ENV_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+B$+"' not found":GOTO EVAL_AST_RETURN
GOSUB INC_REF_R
GOTO EVAL_AST_RETURN

EVAL_AST_SEQ:
REM setup the stack for the loop
GOSUB MAP_LOOP_START

Expand Down Expand Up @@ -98,13 +85,28 @@ SUB EVAL
PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]"
DEBUG_EVAL_DONE:

GOSUB LIST_Q
IF R THEN GOTO APPLY_LIST
GOSUB TYPE_A
T=T-4
IF 0<T THEN ON T GOTO EVAL_SYMBOL,APPLY_LIST,EVAL_VECTOR,EVAL_MAP

REM ELSE
R=A
GOSUB INC_REF_R
GOTO EVAL_RETURN

EVAL_SYMBOL:
B$=S$(Z%(A+1)):CALL ENV_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+B$+"' not found":GOTO EVAL_RETURN
GOSUB INC_REF_R
GOTO EVAL_RETURN

EVAL_MAP:
EVAL_VECTOR:
CALL EVAL_AST
GOTO EVAL_RETURN

APPLY_LIST:

GOSUB EMPTY_Q
IF R THEN R=A:GOSUB INC_REF_R:GOTO EVAL_RETURN

Expand Down Expand Up @@ -171,21 +173,42 @@ SUB EVAL
A=A2:CALL EVAL: REM eval A2 using let_env
GOTO EVAL_RETURN
EVAL_INVOKE:
CALL EVAL_AST
W=R

REM if error, return f/args for release by caller
REM evaluate A0
GOSUB PUSH_A
A=A0:CALL EVAL
GOSUB POP_A
IF ER<>-2 THEN GOTO EVAL_RETURN

AR=Z%(R+1): REM rest
F=Z%(R+2)
F=R

GOSUB TYPE_F
IF T<>9 THEN R=-1:ER=-1:E$="apply of non-function":GOTO EVAL_INVOKE_DONE
GOSUB DO_FUNCTION
EVAL_INVOKE_DONE:
AY=W:GOSUB RELEASE
GOTO EVAL_RETURN

REM ON .. GOTO here reduces the diff with later steps.
T=T-8
IF 0<T THEN ON T GOTO EVAL_DO_FUNCTION

REM if error, pop and return f for release by caller
GOSUB POP_R
ER=-1:E$="apply of non-function":GOTO EVAL_RETURN

EVAL_DO_FUNCTION:
REM regular function

REM Evaluate the arguments
A=Z%(A+1):CALL EVAL_AST
IF ER<>-2 THEN GOSUB POP_Q:AY=Q:GOSUB RELEASE:GOTO EVAL_RETURN

REM set F and AR, push AR (after F) in the stack for release after call
GOSUB PEEK_Q:F=Q
GOSUB PUSH_R
AR=R

GOSUB DO_FUNCTION

REM pop and release f/args
GOSUB POP_Q:AY=Q:GOSUB RELEASE
GOSUB POP_Q:AY=Q
GOSUB RELEASE

EVAL_RETURN:
REM AZ=R: B=1: GOSUB PR_STR
Expand Down
77 changes: 51 additions & 26 deletions impls/basic/step4_if_fn_do.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,8 @@ SUB EVAL_AST
IF ER<>-2 THEN GOTO EVAL_AST_RETURN

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ
IF T<6 OR 8<T THEN R=-1:ER=-1:E$="EVAL_AST: bad type":GOTO EVAL_AST_RETURN

REM scalar: deref to actual value and inc ref cnt
R=A
GOSUB INC_REF_R
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
B$=S$(Z%(A+1)):CALL ENV_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+B$+"' not found":GOTO EVAL_AST_RETURN
GOSUB INC_REF_R
GOTO EVAL_AST_RETURN

EVAL_AST_SEQ:
REM setup the stack for the loop
GOSUB MAP_LOOP_START

Expand Down Expand Up @@ -101,13 +88,28 @@ SUB EVAL
PRINT "EVAL: "+R$+" [A:"+STR$(A)+", LV:"+STR$(LV)+"]"
DEBUG_EVAL_DONE:

GOSUB LIST_Q
IF R THEN GOTO APPLY_LIST
GOSUB TYPE_A
T=T-4
IF 0<T THEN ON T GOTO EVAL_SYMBOL,APPLY_LIST,EVAL_VECTOR,EVAL_MAP

REM ELSE
R=A
GOSUB INC_REF_R
GOTO EVAL_RETURN

EVAL_SYMBOL:
B$=S$(Z%(A+1)):CALL ENV_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+B$+"' not found":GOTO EVAL_RETURN
GOSUB INC_REF_R
GOTO EVAL_RETURN

EVAL_MAP:
EVAL_VECTOR:
CALL EVAL_AST
GOTO EVAL_RETURN

APPLY_LIST:

GOSUB EMPTY_Q
IF R THEN R=A:GOSUB INC_REF_R:GOTO EVAL_RETURN

Expand Down Expand Up @@ -212,40 +214,62 @@ SUB EVAL
GOTO EVAL_RETURN

EVAL_INVOKE:
CALL EVAL_AST

REM if error, return f/args for release by caller
REM evaluate A0
GOSUB PUSH_A
A=A0:CALL EVAL
GOSUB POP_A
IF ER<>-2 THEN GOTO EVAL_RETURN

REM push f/args for release after call
REM set F, push it in the stack for release after call
GOSUB PUSH_R
F=R

AR=Z%(R+1): REM rest
F=Z%(R+2)

REM if metadata, get the actual object
GOSUB TYPE_F
IF T=14 THEN F=Z%(F+1):GOSUB TYPE_F

ON T-8 GOTO EVAL_DO_FUNCTION,EVAL_DO_MAL_FUNCTION,EVAL_DO_MAL_FUNCTION
T=T-8
IF 0<T THEN ON T GOTO EVAL_DO_FUNCTION,EVAL_DO_MAL_FUNCTION

REM if error, pop and return f/args for release by caller
REM if error, pop and return f for release by caller
GOSUB POP_R
ER=-1:E$="apply of non-function":GOTO EVAL_RETURN

REM Duplicate evaluation of args in order to prepare step8.

EVAL_DO_FUNCTION:
REM regular function

REM Evaluate the arguments
A=Z%(A+1):CALL EVAL_AST
IF ER<>-2 THEN GOSUB POP_Q:AY=Q:GOSUB RELEASE:GOTO EVAL_RETURN

REM set F and AR, push AR (after F) in the stack for release after call
GOSUB PEEK_Q:F=Q
GOSUB PUSH_R
AR=R

IF Z%(F+1)<65 THEN GOSUB DO_FUNCTION:GOTO EVAL_DO_FUNCTION_SKIP
REM for recur functions (apply, map, swap!), use GOTO
IF Z%(F+1)>64 THEN CALL DO_TCO_FUNCTION
EVAL_DO_FUNCTION_SKIP:

REM pop and release f/args
GOSUB POP_Q:AY=Q:GOSUB RELEASE
GOSUB POP_Q:AY=Q
GOSUB RELEASE
GOTO EVAL_RETURN

EVAL_DO_MAL_FUNCTION:

REM Evaluate the arguments
A=Z%(A+1):CALL EVAL_AST
IF ER<>-2 THEN GOSUB POP_Q:AY=Q:GOSUB RELEASE:GOTO EVAL_RETURN

REM set F and AR, push AR (after F) in the stack for release after call
GOSUB PEEK_Q:F=Q
GOSUB PUSH_R
AR=R

Q=E:GOSUB PUSH_Q: REM save the current environment for release

REM create new environ using env and params stored in function
Expand All @@ -265,6 +289,7 @@ SUB EVAL
LV=LV+1:GOSUB PEND_A_LV:LV=LV-1

REM pop and release f/args
GOSUB POP_Q:AY=Q:GOSUB RELEASE
GOSUB POP_Q:AY=Q
GOSUB RELEASE

Expand Down
Loading

0 comments on commit 0ec7cc3

Please sign in to comment.