Skip to content

Commit

Permalink
basic: aggressively reduce diff between steps
Browse files Browse the repository at this point in the history
Copy structural changes from high steps to low steps (mostly, split RE
out of REP) in order to reduce the diffs (mostly, 3 to 4 which is
already too large).

On the contrary, remove similar backports that only increase the
complexity of first steps without reducing the diffs:
 - push the environment in the stack in step2 (it does not change yet)
 - release env if not on top of the stack in step3 (not TCO yet)
 - ignoring metadata in steps4-9 (no metadata yet)

Remove obsolete debug assignments from the start of DO_FUNCTION in
step2.

Reorder the load-file logic in steps6-A in order to reduce the diff
between steps 9 and A.

Aggressively remove differences in spaces and comments.
  • Loading branch information
asarhaddon committed Nov 16, 2024
1 parent 0a467ec commit f8295e5
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 167 deletions.
32 changes: 16 additions & 16 deletions impls/basic/step0_repl.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ REM $INCLUDE: 'readline.in.bas'

REM $INCLUDE: 'debug.in.bas'

REM READ(A$) -> R$
MAL_READ:
R$=A$
RETURN
REM READ is inlined in RE

REM EVAL(A$, E) -> R$
EVAL:
REM EVAL(A$) -> R$
SUB EVAL
R$=A$
RETURN
END SUB

REM PRINT(A$) -> R$
MAL_PRINT:
R$=A$
REM PRINT is inlined in REP

REM RE(A$) -> R$
RE:
REM inlined MAL_READ
CALL EVAL
RETURN

REM REP(A$) -> R$
REP:
GOSUB MAL_READ
A=R:GOSUB EVAL
A=R:GOSUB MAL_PRINT
RETURN
SUB REP
GOSUB RE
REM MAL_PRINT
END SUB

REM MAIN program
MAIN:
REPL_LOOP:
A$="user> ":GOSUB READLINE: REM call input parser
IF EZ=1 THEN GOTO QUIT
IF R$="" THEN GOTO REPL_LOOP

A$=R$:GOSUB REP: REM call REP
A$=R$:CALL REP

PRINT R$
GOTO REPL_LOOP
Expand Down
45 changes: 29 additions & 16 deletions impls/basic/step1_read_print.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,43 @@ REM $INCLUDE: 'printer.in.bas'

REM $INCLUDE: 'debug.in.bas'

REM READ(A$) -> R
MAL_READ:
GOSUB READ_STR
RETURN
REM READ is inlined in RE

REM EVAL(A, E) -> R
REM EVAL(A) -> R
SUB EVAL
R=A
END SUB

REM PRINT(A) -> R$
MAL_PRINT:
AZ=A:B=1:GOSUB PR_STR
RETURN
REM PRINT is inlined in REP

REM RE(A$) -> R
REM caller must release result
RE:
R1=-1
GOSUB READ_STR: REM inlined MAL_READ
R1=R
IF ER<>-2 THEN GOTO RE_DONE

A=R:CALL EVAL

RE_DONE:
REM Release memory from MAL_READ
AY=R1:GOSUB RELEASE
RETURN: REM caller must release result of EVAL

REM REP(A$) -> R$
SUB REP
GOSUB MAL_READ
IF ER<>-2 THEN GOTO REP_DONE
R2=-1

A=R:CALL EVAL
GOSUB RE
R2=R
IF ER<>-2 THEN GOTO REP_DONE

A=R:GOSUB MAL_PRINT
AZ=R:B=1:GOSUB PR_STR: REM MAL_PRINT

REP_DONE:
REM Release memory from EVAL
AY=R:GOSUB RELEASE
REM Release memory from MAL_READ and EVAL
AY=R2:GOSUB RELEASE
END SUB

REM MAIN program
Expand All @@ -49,14 +58,18 @@ MAIN:
IF EZ=1 THEN GOTO QUIT
IF R$="" THEN GOTO REPL_LOOP

A$=R$:CALL REP: REM call REP
A$=R$:CALL REP

IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
PRINT R$
GOTO REPL_LOOP

QUIT:
REM GOSUB PR_MEMORY_SUMMARY_SMALL
REM GOSUB PR_MEMORY_MAP
REM P1=0:P2=ZI:GOSUB PR_MEMORY
REM P1=D:GOSUB PR_OBJECT
REM P1=ZK:GOSUB PR_OBJECT
#cbm END
#qbasic SYSTEM

Expand Down
42 changes: 21 additions & 21 deletions impls/basic/step2_eval.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ REM READ is inlined in RE

REM EVAL_AST(A, E) -> R
SUB EVAL_AST
REM push A and E on the stack
Q=E:GOSUB PUSH_Q
REM push A on the stack
GOSUB PUSH_A

IF ER<>-2 THEN GOTO EVAL_AST_RETURN
Expand Down Expand Up @@ -46,7 +45,6 @@ SUB EVAL_AST
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
Expand All @@ -61,17 +59,15 @@ SUB EVAL_AST
GOTO EVAL_AST_RETURN

EVAL_AST_RETURN:
REM pop A and E off the stack
REM pop A off the stack
GOSUB POP_A
GOSUB POP_Q:E=Q
END SUB

REM EVAL(A, E) -> R
SUB EVAL
LV=LV+1: REM track basic return stack level

REM push A and E on the stack
Q=E:GOSUB PUSH_Q
REM push A on the stack
GOSUB PUSH_A

REM PRINT "EVAL A:"+STR$(A)+",X:"+STR$(X)+",LV:"+STR$(LV)+",FRE:"+STR$(FRE(0))
Expand Down Expand Up @@ -117,8 +113,6 @@ SUB EVAL
F=Z%(R+2)

GOSUB TYPE_F

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

Expand Down Expand Up @@ -147,19 +141,13 @@ SUB EVAL
#cbm T=FRE(0)
#qbasic T=0

REM pop A and E off the stack
REM pop A off the stack
GOSUB POP_A
GOSUB POP_Q:E=Q

END SUB

REM DO_FUNCTION(F, AR)
DO_FUNCTION:
AZ=F:GOSUB PR_STR
F$=R$
AZ=AR:GOSUB PR_STR
AR$=R$

REM Get the function number
G=Z%(F+1)

Expand Down Expand Up @@ -192,15 +180,28 @@ DO_FUNCTION:

REM PRINT is inlined in REP

REM REP(A$) -> R$
REM RE(A$) -> R
REM Assume D has repl_env
SUB REP
REM caller must release result
RE:
R1=-1
GOSUB READ_STR: REM inlined MAL_READ
R1=R
IF ER<>-2 THEN GOTO REP_DONE
IF ER<>-2 THEN GOTO RE_DONE

A=R:E=D:CALL EVAL

RE_DONE:
REM Release memory from MAL_READ
AY=R1:GOSUB RELEASE
RETURN: REM caller must release result of EVAL

REM REP(A$) -> R$
REM Assume D has repl_env
SUB REP
R2=-1

GOSUB RE
R2=R
IF ER<>-2 THEN GOTO REP_DONE

Expand All @@ -209,7 +210,6 @@ SUB REP
REP_DONE:
REM Release memory from MAL_READ and EVAL
AY=R2:GOSUB RELEASE
AY=R1:GOSUB RELEASE
END SUB

REM MAIN program
Expand Down Expand Up @@ -244,7 +244,7 @@ MAIN:
IF EZ=1 THEN GOTO QUIT
IF R$="" THEN GOTO REPL_LOOP

A$=R$:CALL REP: REM call REP
A$=R$:CALL REP

IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
PRINT R$
Expand Down
31 changes: 18 additions & 13 deletions impls/basic/step3_env.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ SUB EVAL_AST
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
Expand Down Expand Up @@ -183,8 +182,6 @@ SUB EVAL
F=Z%(R+2)

GOSUB TYPE_F

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

Expand All @@ -204,10 +201,6 @@ SUB EVAL
REM AZ=R: B=1: GOSUB PR_STR
REM PRINT "EVAL_RETURN R: ["+R$+"] ("+STR$(R)+"), LV:"+STR$(LV)+",ER:"+STR$(ER)

REM release environment if not the top one on the stack
GOSUB PEEK_Q_1
IF E<>Q THEN AY=E:GOSUB RELEASE

LV=LV-1: REM track basic return stack level

REM release everything we couldn't release earlier
Expand Down Expand Up @@ -257,15 +250,28 @@ DO_FUNCTION:

REM PRINT is inlined in REP

REM REP(A$) -> R$
REM RE(A$) -> R
REM Assume D has repl_env
SUB REP
REM caller must release result
RE:
R1=-1
GOSUB READ_STR: REM inlined MAL_READ
R1=R
IF ER<>-2 THEN GOTO REP_DONE
IF ER<>-2 THEN GOTO RE_DONE

A=R:E=D:CALL EVAL

RE_DONE:
REM Release memory from MAL_READ
AY=R1:GOSUB RELEASE
RETURN: REM caller must release result of EVAL

REM REP(A$) -> R$
REM Assume D has repl_env
SUB REP
R2=-1

GOSUB RE
R2=R
IF ER<>-2 THEN GOTO REP_DONE

Expand All @@ -274,7 +280,6 @@ SUB REP
REP_DONE:
REM Release memory from MAL_READ and EVAL
AY=R2:GOSUB RELEASE
AY=R1:GOSUB RELEASE
END SUB

REM MAIN program
Expand All @@ -285,8 +290,8 @@ MAIN:

REM create repl_env
C=0:GOSUB ENV_NEW:D=R

E=D

REM + function
T=9:L=1:GOSUB ALLOC: REM native function
B$="+":C=R:GOSUB ENV_SET_S
Expand All @@ -310,7 +315,7 @@ MAIN:
IF EZ=1 THEN GOTO QUIT
IF R$="" THEN GOTO REPL_LOOP

A$=R$:CALL REP: REM call REP
A$=R$:CALL REP

IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
PRINT R$
Expand Down
7 changes: 1 addition & 6 deletions impls/basic/step4_if_fn_do.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ SUB EVAL_AST
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
Expand Down Expand Up @@ -225,10 +224,7 @@ SUB EVAL
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

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

Expand Down Expand Up @@ -299,7 +295,6 @@ END SUB

REM PRINT is inlined in REP


REM RE(A$) -> R
REM Assume D has repl_env
REM caller must release result
Expand Down Expand Up @@ -355,7 +350,7 @@ MAIN:
IF EZ=1 THEN GOTO QUIT
IF R$="" THEN GOTO REPL_LOOP

A$=R$:CALL REP: REM call REP
A$=R$:CALL REP

IF ER<>-2 THEN GOSUB PRINT_ERROR:GOTO REPL_LOOP
PRINT R$
Expand Down
Loading

0 comments on commit f8295e5

Please sign in to comment.