Skip to content

Commit

Permalink
basic: merge env_find into env_get
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Nov 11, 2024
1 parent 7b54694 commit c923200
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 44 deletions.
21 changes: 5 additions & 16 deletions impls/basic/env.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,16 @@ 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, K) -> R
REM R3 is either 0 (missing key) or 1, then R is the increfed value.
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 INC_REF_R
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
3 changes: 1 addition & 2 deletions impls/basic/step2_eval.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ SUB EVAL_AST

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

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

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

EVAL_AST_SEQ:
Expand Down
4 changes: 2 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,8 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

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

EVAL_AST_SEQ:
Expand Down
4 changes: 2 additions & 2 deletions impls/basic/step5_tco.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

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

EVAL_AST_SEQ:
Expand Down
4 changes: 2 additions & 2 deletions impls/basic/step6_file.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

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

EVAL_AST_SEQ:
Expand Down
4 changes: 2 additions & 2 deletions impls/basic/step7_quote.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

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

EVAL_AST_SEQ:
Expand Down
10 changes: 5 additions & 5 deletions impls/basic/step8_macros.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ 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
K=B:GOSUB ENV_GET
IF R3=0 THEN GOTO MACROEXPAND_DONE
B=R
REM macro?
IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE

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

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

EVAL_AST_SEQ:
Expand Down
10 changes: 5 additions & 5 deletions impls/basic/step9_try.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ 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
K=B:GOSUB ENV_GET
IF R3=0 THEN GOTO MACROEXPAND_DONE
B=R
REM macro?
IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE

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

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

EVAL_AST_SEQ:
Expand Down
10 changes: 5 additions & 5 deletions impls/basic/stepA_mal.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ 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
K=B:GOSUB ENV_GET
IF R3=0 THEN GOTO MACROEXPAND_DONE
B=R
REM macro?
IF (Z%(B)AND 31)<>11 THEN GOTO MACROEXPAND_DONE

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

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

EVAL_AST_SEQ:
Expand Down
1 change: 0 additions & 1 deletion impls/basic/variables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 c923200

Please sign in to comment.