Skip to content

Commit

Permalink
basic: reduce diff between steps
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Nov 11, 2024
1 parent c0cf8c7 commit 7b54694
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 98 deletions.
33 changes: 17 additions & 16 deletions impls/basic/step2_eval.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ 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_AST(A, E) -> R
SUB EVAL_AST
LV=LV+1

REM push A and E on the stack
Q=E:GOSUB PUSH_Q
GOSUB PUSH_A
Expand All @@ -25,7 +20,7 @@ SUB EVAL_AST

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ

REM scalar: deref to actual value and inc ref cnt
R=A
Expand Down Expand Up @@ -82,8 +77,6 @@ SUB EVAL_AST
REM pop A and E off the stack
GOSUB POP_A
GOSUB POP_Q:E=Q

LV=LV-1
END SUB

REM EVAL(A, E) -> R
Expand All @@ -94,6 +87,8 @@ SUB EVAL
Q=E:GOSUB PUSH_Q
GOSUB PUSH_A

REM PRINT "EVAL A:"+STR$(A)+",X:"+STR$(X)+",LV:"+STR$(LV)+",FRE:"+STR$(FRE(0))

IF ER<>-2 THEN GOTO EVAL_RETURN

REM AZ=A:B=1:GOSUB PR_STR
Expand Down Expand Up @@ -127,9 +122,14 @@ SUB EVAL
GOTO EVAL_RETURN

EVAL_RETURN:
REM AZ=R: B=1: GOSUB PR_STR
REM PRINT "EVAL_RETURN R: ["+R$+"] ("+STR$(R)+"), LV:"+STR$(LV)+",ER:"+STR$(ER)

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

REM release everything we couldn't release earlier
GOSUB RELEASE_PEND

REM trigger GC
#cbm T=FRE(0)
#qbasic T=0
Expand Down Expand Up @@ -177,24 +177,21 @@ DO_FUNCTION:
DO_FUNCTION_DONE:
RETURN

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

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

A=R:E=D:CALL EVAL
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 MAL_READ and EVAL
Expand Down Expand Up @@ -242,6 +239,10 @@ MAIN:

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
34 changes: 18 additions & 16 deletions impls/basic/step3_env.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ REM $INCLUDE: 'env.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_AST(A, E) -> R
SUB EVAL_AST
LV=LV+1

REM push A and E on the stack
Q=E:GOSUB PUSH_Q
GOSUB PUSH_A
Expand All @@ -26,7 +21,7 @@ SUB EVAL_AST

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ

REM scalar: deref to actual value and inc ref cnt
R=A
Expand Down Expand Up @@ -82,8 +77,6 @@ SUB EVAL_AST
REM pop A and E off the stack
GOSUB POP_A
GOSUB POP_Q:E=Q

LV=LV-1
END SUB

REM EVAL(A, E) -> R
Expand All @@ -94,6 +87,8 @@ SUB EVAL
Q=E:GOSUB PUSH_Q
GOSUB PUSH_A

REM PRINT "EVAL A:"+STR$(A)+",X:"+STR$(X)+",LV:"+STR$(LV)+",FRE:"+STR$(FRE(0))

IF ER<>-2 THEN GOTO EVAL_RETURN

REM AZ=A:B=1:GOSUB PR_STR
Expand Down Expand Up @@ -189,12 +184,18 @@ SUB EVAL
GOTO EVAL_RETURN

EVAL_RETURN:
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
GOSUB RELEASE_PEND

REM trigger GC
#cbm T=FRE(0)
#qbasic T=0
Expand Down Expand Up @@ -237,24 +238,21 @@ DO_FUNCTION:
DO_FUNCTION_DONE:
RETURN

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

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

A=R:E=D:CALL EVAL
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 MAL_READ and EVAL
Expand Down Expand Up @@ -303,6 +301,10 @@ MAIN:

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
21 changes: 10 additions & 11 deletions impls/basic/step4_if_fn_do.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ REM $INCLUDE: 'core.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_AST(A, E) -> R
SUB EVAL_AST
Expand All @@ -25,7 +22,7 @@ SUB EVAL_AST

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ

REM scalar: deref to actual value and inc ref cnt
R=A
Expand Down Expand Up @@ -293,17 +290,15 @@ SUB EVAL

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 Assume D has repl_env
REM caller must release result
RE:
R1=-1
GOSUB MAL_READ
GOSUB READ_STR: REM inlined MAL_READ
R1=R
IF ER<>-2 THEN GOTO RE_DONE

Expand All @@ -323,7 +318,7 @@ SUB REP
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 MAL_READ and EVAL
Expand Down Expand Up @@ -361,6 +356,10 @@ MAIN:

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
21 changes: 10 additions & 11 deletions impls/basic/step5_tco.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ REM $INCLUDE: 'core.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_AST(A, E) -> R
SUB EVAL_AST
Expand All @@ -25,7 +22,7 @@ SUB EVAL_AST

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ

REM scalar: deref to actual value and inc ref cnt
R=A
Expand Down Expand Up @@ -317,17 +314,15 @@ SUB EVAL

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 Assume D has repl_env
REM caller must release result
RE:
R1=-1
GOSUB MAL_READ
GOSUB READ_STR: REM inlined MAL_READ
R1=R
IF ER<>-2 THEN GOTO RE_DONE

Expand All @@ -347,7 +342,7 @@ SUB REP
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 MAL_READ and EVAL
Expand Down Expand Up @@ -385,6 +380,10 @@ MAIN:

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
21 changes: 10 additions & 11 deletions impls/basic/step6_file.in.bas
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ REM $INCLUDE: 'core.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_AST(A, E) -> R
SUB EVAL_AST
Expand All @@ -25,7 +22,7 @@ SUB EVAL_AST

GOSUB TYPE_A
IF T=5 THEN GOTO EVAL_AST_SYMBOL
IF T>=6 AND T<=8 THEN GOTO EVAL_AST_SEQ
IF T>5 AND T<9 THEN GOTO EVAL_AST_SEQ

REM scalar: deref to actual value and inc ref cnt
R=A
Expand Down Expand Up @@ -317,17 +314,15 @@ SUB EVAL

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 Assume D has repl_env
REM caller must release result
RE:
R1=-1
GOSUB MAL_READ
GOSUB READ_STR: REM inlined MAL_READ
R1=R
IF ER<>-2 THEN GOTO RE_DONE

Expand All @@ -347,7 +342,7 @@ SUB REP
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 MAL_READ and EVAL
Expand Down Expand Up @@ -416,6 +411,10 @@ MAIN:

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
Loading

0 comments on commit 7b54694

Please sign in to comment.