Skip to content

Commit

Permalink
basic: prepare ENV_GET for DEBUG-EVAL
Browse files Browse the repository at this point in the history
Pass a BASIC string to ENV_GET and HASHMAP_GET (for DEBUG-EVAL).
Merge eval_find into eval_env.
Let HASHMAP_GET return its result into R for consistency.
Raise no exception from ENV_GET (for DEBUG-EVAL).

Move INC_REF_R outside of ENV_GET for clarity (the macroexpand part
will disappear soon anyway).
  • Loading branch information
asarhaddon committed Nov 13, 2024
1 parent 7b54694 commit e824ceb
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 48 deletions.
4 changes: 2 additions & 2 deletions impls/basic/core.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ DO_FUNCTION:
GOTO DO_ASSOC_LOOP
DO_GET:
IF A=0 THEN R=0:GOTO INC_REF_R
H=A:K=B:GOSUB HASHMAP_GET
H=A:B$=S$(Z%(B+1)):GOSUB HASHMAP_GET
GOTO INC_REF_R
DO_CONTAINS:
H=A:K=B:GOSUB HASHMAP_CONTAINS
H=A:B$=S$(Z%(B+1)):GOSUB HASHMAP_CONTAINS
GOTO RETURN_TRUE_FALSE
DO_KEYS:
T1=0
Expand Down
20 changes: 5 additions & 15 deletions impls/basic/env.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,17 @@ ENV_SET_S:
R=C
RETURN

REM ENV_FIND(E, K) -> R
REM Returns environment (R) containing K. If found, value found is
REM in R4
SUB ENV_FIND
REM ENV_GET(E, B$) -> R
REM - R3=1 if the key was found, else 0
SUB ENV_GET
T=E
ENV_FIND_LOOP:
H=Z%(T+1)
REM More efficient to use GET for value (R) and contains? (R3)
GOSUB HASHMAP_GET
REM if we found it, save value in R4 for ENV_GET
IF R3=1 THEN R4=R:R=T:GOTO ENV_FIND_DONE
REM if we found it, return it
IF R3=1 THEN GOTO ENV_FIND_DONE
T=Z%(T+2): REM get outer environment
IF T>0 THEN GOTO ENV_FIND_LOOP
R=-1
ENV_FIND_DONE:
END SUB

REM ENV_GET(E, K) -> R
ENV_GET:
CALL ENV_FIND
IF R=-1 THEN ER=-1:E$="'"+S$(Z%(K+1))+"' not found":GOTO ENV_GET_RETURN
R=R4
GOSUB INC_REF_R
GOTO ENV_GET_RETURN
4 changes: 2 additions & 2 deletions impls/basic/step2_eval.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
H=E:K=A:GOSUB HASHMAP_GET
IF R3=0 THEN R=-1:ER=-1:E$="'"+S$(Z%(A+1))+"' not found":GOTO EVAL_AST_RETURN
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

Expand Down
5 changes: 3 additions & 2 deletions impls/basic/step3_env.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
5 changes: 3 additions & 2 deletions impls/basic/step4_if_fn_do.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
5 changes: 3 additions & 2 deletions impls/basic/step5_tco.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
5 changes: 3 additions & 2 deletions impls/basic/step6_file.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
5 changes: 3 additions & 2 deletions impls/basic/step7_quote.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
12 changes: 7 additions & 5 deletions impls/basic/step8_macros.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ SUB MACROEXPAND
REM symbol? in first position
IF (Z%(B)AND 31)<>5 THEN GOTO MACROEXPAND_DONE
REM defined in environment?
K=B:CALL ENV_FIND
IF R=-1 THEN GOTO MACROEXPAND_DONE
B=R4
B$=S$(Z%(B+1)):CALL ENV_GET
IF R3=0 THEN GOTO MACROEXPAND_DONE
B=R
REM macro?
IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE

GOSUB INC_REF_R
F=B:AR=Z%(A+1):CALL APPLY
A=R

Expand Down Expand Up @@ -177,8 +178,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
12 changes: 7 additions & 5 deletions impls/basic/step9_try.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ SUB MACROEXPAND
REM symbol? in first position
IF (Z%(B)AND 31)<>5 THEN GOTO MACROEXPAND_DONE
REM defined in environment?
K=B:CALL ENV_FIND
IF R=-1 THEN GOTO MACROEXPAND_DONE
B=R4
B$=S$(Z%(B+1)):CALL ENV_GET
IF R3=0 THEN GOTO MACROEXPAND_DONE
B=R
REM macro?
IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE

GOSUB INC_REF_R
F=B:AR=Z%(A+1):CALL APPLY
A=R

Expand Down Expand Up @@ -177,8 +178,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
12 changes: 7 additions & 5 deletions impls/basic/stepA_mal.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ SUB MACROEXPAND
REM symbol? in first position
IF (Z%(B)AND 31)<>5 THEN GOTO MACROEXPAND_DONE
REM defined in environment?
K=B:CALL ENV_FIND
IF R=-1 THEN GOTO MACROEXPAND_DONE
B=R4
B$=S$(Z%(B+1)):CALL ENV_GET
IF R3=0 THEN GOTO MACROEXPAND_DONE
B=R
REM macro?
IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE

GOSUB INC_REF_R
F=B:AR=Z%(A+1):CALL APPLY
A=R

Expand Down Expand Up @@ -177,8 +178,9 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_SYMBOL:
K=A:GOTO ENV_GET
ENV_GET_RETURN:
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:
Expand Down
3 changes: 1 addition & 2 deletions impls/basic/types.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,9 @@ ASSOC1_S:
AY=K:GOSUB RELEASE: REM map took ownership of key
RETURN

REM HASHMAP_GET(H, K) -> R
REM HASHMAP_GET(H, B$) -> R
REM - returns R3 with whether we found it or not
HASHMAP_GET:
B$=S$(Z%(K+1)): REM search key string
R3=0: REM whether found or not (for HASHMAP_CONTAINS)
R=0
HASHMAP_GET_LOOP:
Expand Down
3 changes: 1 addition & 2 deletions impls/basic/variables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Calling arguments/temporaries:
A : common call argument (especially EVAL, EVAL_AST)
A$ : common call argument (READLINE, reader, string temp, key value)
B : common call argument
B$ : STRING arg (HASHMAP_GET temp), PR_STR_SEQ separator
B$ : STRING arg for HASHMAP_GET, PR_STR_SEQ separator
: INIT_CORE_SET_FUNCTION, ENV_SET_S, ASSOC1_S
C : common call argument, DO_TCO_FUNCTION temp in DO_APPLY
E : environment (EVAL, EVAL_AST)
Expand All @@ -60,7 +60,6 @@ P3 : PR_OBJECT, PR_MEMORY_VALUE
R1 : REP, RE - MAL_READ result temp
R2 : REP, RE - EVAL result temp
R3 : HASHMAP_GET, DO_HASH_MAP, DO_KEYS_VALS temp and return value
R4 : ENV_FIND temp and return value
R6 : SLICE return value (last element)
SZ : size argument to ALLOC
S1$ : REPLACE needle
Expand Down

0 comments on commit e824ceb

Please sign in to comment.