Renamed loop stacks to prepare for FOR loop
This commit is contained in:
parent
c8c1d7e705
commit
4b5f8b30b7
6 changed files with 26 additions and 26 deletions
|
|
@ -36,7 +36,7 @@ func (c *BreakCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContex
|
|||
|
||||
// Get skip label from WHILE stack
|
||||
var err2 error
|
||||
c.skipLabel, err2 = ctx.WhileStack.Peek()
|
||||
c.skipLabel, err2 = ctx.LoopEndStack.Peek()
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("BREAK: not inside WHILE loop")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,13 +37,13 @@ func (c *WendCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext
|
|||
|
||||
// Pop loop label
|
||||
var err2 error
|
||||
c.loopLabel, err2 = ctx.LoopStack.Pop()
|
||||
c.loopLabel, err2 = ctx.LoopStartStack.Pop()
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("WEND: not inside WHILE loop")
|
||||
}
|
||||
|
||||
// Pop skip label
|
||||
c.skipLabel, err2 = ctx.WhileStack.Pop()
|
||||
c.skipLabel, err2 = ctx.LoopEndStack.Pop()
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("WEND: not inside WHILE loop")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,8 +95,8 @@ func (c *WhileCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContex
|
|||
c.useLongJump = longJumpPragma != "" && longJumpPragma != "0"
|
||||
|
||||
// Create labels
|
||||
c.loopLabel = ctx.LoopStack.Push()
|
||||
c.skipLabel = ctx.WhileStack.Push()
|
||||
c.loopLabel = ctx.LoopStartStack.Push()
|
||||
c.skipLabel = ctx.LoopEndStack.Push()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ func (c *WhileCommand) Generate(ctx *compiler.CompilerContext) ([]string, error)
|
|||
c.param1,
|
||||
c.param2,
|
||||
c.useLongJump,
|
||||
ctx.WhileStack,
|
||||
ctx.LoopEndStack,
|
||||
ctx.GeneralStack,
|
||||
)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -23,14 +23,14 @@ func TestWhileBasicEqual(t *testing.T) {
|
|||
st.AddVar("x", "", compiler.KindByte, 0)
|
||||
},
|
||||
wantWhile: []string{
|
||||
"_LOOP1",
|
||||
"_LOOPSTART1",
|
||||
"\tlda x",
|
||||
"\tcmp #$0a",
|
||||
"\tbne _WEND1",
|
||||
"\tbne _LOOPEND1",
|
||||
},
|
||||
wantWend: []string{
|
||||
"\tjmp _LOOP1",
|
||||
"_WEND1",
|
||||
"\tjmp _LOOPSTART1",
|
||||
"_LOOPEND1",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -40,17 +40,17 @@ func TestWhileBasicEqual(t *testing.T) {
|
|||
st.AddVar("x", "", compiler.KindWord, 0)
|
||||
},
|
||||
wantWhile: []string{
|
||||
"_LOOP1",
|
||||
"_LOOPSTART1",
|
||||
"\tlda x",
|
||||
"\tcmp #$e8",
|
||||
"\tbne _WEND1",
|
||||
"\tbne _LOOPEND1",
|
||||
"\tlda x+1",
|
||||
"\tcmp #$03",
|
||||
"\tbne _WEND1",
|
||||
"\tbne _LOOPEND1",
|
||||
},
|
||||
wantWend: []string{
|
||||
"\tjmp _LOOP1",
|
||||
"_WEND1",
|
||||
"\tjmp _LOOPSTART1",
|
||||
"_LOOPEND1",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -226,7 +226,7 @@ func TestWhileBreak(t *testing.T) {
|
|||
t.Fatalf("WEND Interpret() error = %v", err)
|
||||
}
|
||||
|
||||
if len(breakAsm) != 1 || !strings.Contains(breakAsm[0], "jmp _WEND") {
|
||||
if len(breakAsm) != 1 || !strings.Contains(breakAsm[0], "jmp _LOOPEND") {
|
||||
t.Errorf("BREAK should jump to WEND label, got: %v", breakAsm)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ func (c *TestBreakCommand) Interpret(line preproc.Line, ctx *CompilerContext) er
|
|||
|
||||
func (c *TestBreakCommand) Generate(ctx *CompilerContext) ([]string, error) {
|
||||
// BREAK jumps to end of WHILE loop
|
||||
label, err := ctx.WhileStack.Peek()
|
||||
label, err := ctx.LoopEndStack.Peek()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("BREAK outside of WHILE loop")
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ func TestCompilerArchitecture(t *testing.T) {
|
|||
}
|
||||
|
||||
// Manually push a WHILE label so BREAK has something to reference
|
||||
comp.Context().WhileStack.Push()
|
||||
comp.Context().LoopEndStack.Push()
|
||||
|
||||
// Compile
|
||||
output, err := comp.Compile(lines)
|
||||
|
|
@ -127,8 +127,8 @@ func TestCompilerContext(t *testing.T) {
|
|||
if ctx.ConstStrHandler == nil {
|
||||
t.Error("ConstStrHandler not initialized")
|
||||
}
|
||||
if ctx.WhileStack == nil {
|
||||
t.Error("WhileStack not initialized")
|
||||
if ctx.LoopEndStack == nil {
|
||||
t.Error("LoopEndStack not initialized")
|
||||
}
|
||||
if ctx.IfStack == nil {
|
||||
t.Error("IfStack not initialized")
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ type CompilerContext struct {
|
|||
ConstStrHandler *ConstantStringHandler
|
||||
|
||||
// Label stacks for control flow
|
||||
LoopStack *LabelStack // Start of loop (like WHILE)
|
||||
WhileStack *LabelStack // WHILE...WEND
|
||||
IfStack *LabelStack // IF...ENDIF
|
||||
GeneralStack *LabelStack // General purpose (GOSUB, etc)
|
||||
LoopStartStack *LabelStack // Start of loop (like WHILE)
|
||||
LoopEndStack *LabelStack // WHILE...WEND
|
||||
IfStack *LabelStack // IF...ENDIF
|
||||
GeneralStack *LabelStack // General purpose (GOSUB, etc)
|
||||
|
||||
// Pragma access for per-line pragma lookup
|
||||
Pragma *preproc.Pragma
|
||||
|
|
@ -34,8 +34,8 @@ func NewCompilerContext(pragma *preproc.Pragma) *CompilerContext {
|
|||
ctx := &CompilerContext{
|
||||
SymbolTable: symTable,
|
||||
ConstStrHandler: constStrHandler,
|
||||
LoopStack: NewLabelStack("_LOOP"),
|
||||
WhileStack: NewLabelStack("_WEND"),
|
||||
LoopStartStack: NewLabelStack("_LOOPSTART"),
|
||||
LoopEndStack: NewLabelStack("_LOOPEND"),
|
||||
IfStack: NewLabelStack("_I"),
|
||||
GeneralStack: generalStack,
|
||||
Pragma: pragma,
|
||||
|
|
|
|||
Loading…
Reference in a new issue