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 authored and kanaka committed Nov 19, 2024
1 parent 3219b75 commit 05461b7
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 212 deletions.
31 changes: 13 additions & 18 deletions impls/basic/step0_repl.in.bas
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
GOTO MAIN

REM $INCLUDE: 'mem.in.bas'
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$
RETURN
REM PRINT is inlined in REP

REM REP(A$) -> R$
REP:
GOSUB MAL_READ
A=R:GOSUB EVAL
A=R:GOSUB MAL_PRINT
RETURN
SUB REP
REM inlined READ (not affecting A$)
CALL EVAL
REM inlined PRINT (not affecting A$)
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 All @@ -41,4 +37,3 @@ MAIN:
REM GOSUB PR_MEMORY_SUMMARY_SMALL
#cbm END
#qbasic SYSTEM

42 changes: 26 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,41 @@ 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 READ
R1=R
IF ER<>-2 THEN GOTO RE_DONE

A=R:CALL EVAL

RE_DONE:
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 inlined PRINT

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

REM MAIN program
Expand All @@ -49,19 +56,22 @@ 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

PRINT_ERROR:
PRINT "Error: "+E$
ER=-2:E$=""
RETURN

49 changes: 24 additions & 25 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,24 +180,36 @@ 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
GOSUB READ_STR: REM inlined 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 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

AZ=R:B=1:GOSUB PR_STR: REM MAL_PRINT
AZ=R:B=1:GOSUB PR_STR: REM inlined PRINT

REP_DONE:
REM Release memory from MAL_READ and EVAL
REM Release memory from 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 All @@ -263,4 +263,3 @@ MAIN:
PRINT "Error: "+E$
ER=-2:E$=""
RETURN

38 changes: 21 additions & 17 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,24 +250,36 @@ 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
GOSUB READ_STR: REM inlined 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 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

AZ=R:B=1:GOSUB PR_STR: REM MAL_PRINT
AZ=R:B=1:GOSUB PR_STR: REM inlined PRINT

REP_DONE:
REM Release memory from MAL_READ and EVAL
REM Release memory from 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 All @@ -329,4 +334,3 @@ MAIN:
PRINT "Error: "+E$
ER=-2:E$=""
RETURN

Loading

0 comments on commit 05461b7

Please sign in to comment.