diff --git a/internal/commands/break.go b/internal/commands/break.go index 1d89b2e..7716915 100644 --- a/internal/commands/break.go +++ b/internal/commands/break.go @@ -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") } diff --git a/internal/commands/wend.go b/internal/commands/wend.go index 6a5a3d2..d4be077 100644 --- a/internal/commands/wend.go +++ b/internal/commands/wend.go @@ -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") } diff --git a/internal/commands/while.go b/internal/commands/while.go index 94c53d5..64e14cf 100644 --- a/internal/commands/while.go +++ b/internal/commands/while.go @@ -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 { diff --git a/internal/commands/while_test.go b/internal/commands/while_test.go index 5f8391a..04fce56 100644 --- a/internal/commands/while_test.go +++ b/internal/commands/while_test.go @@ -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) } } diff --git a/internal/compiler/compiler_test.go b/internal/compiler/compiler_test.go index 21202c0..17b43aa 100644 --- a/internal/compiler/compiler_test.go +++ b/internal/compiler/compiler_test.go @@ -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") diff --git a/internal/compiler/context.go b/internal/compiler/context.go index dc6c312..c3cdcb2 100644 --- a/internal/compiler/context.go +++ b/internal/compiler/context.go @@ -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,