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
|
// Get skip label from WHILE stack
|
||||||
var err2 error
|
var err2 error
|
||||||
c.skipLabel, err2 = ctx.WhileStack.Peek()
|
c.skipLabel, err2 = ctx.LoopEndStack.Peek()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return fmt.Errorf("BREAK: not inside WHILE loop")
|
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
|
// Pop loop label
|
||||||
var err2 error
|
var err2 error
|
||||||
c.loopLabel, err2 = ctx.LoopStack.Pop()
|
c.loopLabel, err2 = ctx.LoopStartStack.Pop()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return fmt.Errorf("WEND: not inside WHILE loop")
|
return fmt.Errorf("WEND: not inside WHILE loop")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop skip label
|
// Pop skip label
|
||||||
c.skipLabel, err2 = ctx.WhileStack.Pop()
|
c.skipLabel, err2 = ctx.LoopEndStack.Pop()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return fmt.Errorf("WEND: not inside WHILE loop")
|
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"
|
c.useLongJump = longJumpPragma != "" && longJumpPragma != "0"
|
||||||
|
|
||||||
// Create labels
|
// Create labels
|
||||||
c.loopLabel = ctx.LoopStack.Push()
|
c.loopLabel = ctx.LoopStartStack.Push()
|
||||||
c.skipLabel = ctx.WhileStack.Push()
|
c.skipLabel = ctx.LoopEndStack.Push()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -116,7 +116,7 @@ func (c *WhileCommand) Generate(ctx *compiler.CompilerContext) ([]string, error)
|
||||||
c.param1,
|
c.param1,
|
||||||
c.param2,
|
c.param2,
|
||||||
c.useLongJump,
|
c.useLongJump,
|
||||||
ctx.WhileStack,
|
ctx.LoopEndStack,
|
||||||
ctx.GeneralStack,
|
ctx.GeneralStack,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,14 @@ func TestWhileBasicEqual(t *testing.T) {
|
||||||
st.AddVar("x", "", compiler.KindByte, 0)
|
st.AddVar("x", "", compiler.KindByte, 0)
|
||||||
},
|
},
|
||||||
wantWhile: []string{
|
wantWhile: []string{
|
||||||
"_LOOP1",
|
"_LOOPSTART1",
|
||||||
"\tlda x",
|
"\tlda x",
|
||||||
"\tcmp #$0a",
|
"\tcmp #$0a",
|
||||||
"\tbne _WEND1",
|
"\tbne _LOOPEND1",
|
||||||
},
|
},
|
||||||
wantWend: []string{
|
wantWend: []string{
|
||||||
"\tjmp _LOOP1",
|
"\tjmp _LOOPSTART1",
|
||||||
"_WEND1",
|
"_LOOPEND1",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -40,17 +40,17 @@ func TestWhileBasicEqual(t *testing.T) {
|
||||||
st.AddVar("x", "", compiler.KindWord, 0)
|
st.AddVar("x", "", compiler.KindWord, 0)
|
||||||
},
|
},
|
||||||
wantWhile: []string{
|
wantWhile: []string{
|
||||||
"_LOOP1",
|
"_LOOPSTART1",
|
||||||
"\tlda x",
|
"\tlda x",
|
||||||
"\tcmp #$e8",
|
"\tcmp #$e8",
|
||||||
"\tbne _WEND1",
|
"\tbne _LOOPEND1",
|
||||||
"\tlda x+1",
|
"\tlda x+1",
|
||||||
"\tcmp #$03",
|
"\tcmp #$03",
|
||||||
"\tbne _WEND1",
|
"\tbne _LOOPEND1",
|
||||||
},
|
},
|
||||||
wantWend: []string{
|
wantWend: []string{
|
||||||
"\tjmp _LOOP1",
|
"\tjmp _LOOPSTART1",
|
||||||
"_WEND1",
|
"_LOOPEND1",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -226,7 +226,7 @@ func TestWhileBreak(t *testing.T) {
|
||||||
t.Fatalf("WEND Interpret() error = %v", err)
|
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)
|
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) {
|
func (c *TestBreakCommand) Generate(ctx *CompilerContext) ([]string, error) {
|
||||||
// BREAK jumps to end of WHILE loop
|
// BREAK jumps to end of WHILE loop
|
||||||
label, err := ctx.WhileStack.Peek()
|
label, err := ctx.LoopEndStack.Peek()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("BREAK outside of WHILE loop")
|
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
|
// Manually push a WHILE label so BREAK has something to reference
|
||||||
comp.Context().WhileStack.Push()
|
comp.Context().LoopEndStack.Push()
|
||||||
|
|
||||||
// Compile
|
// Compile
|
||||||
output, err := comp.Compile(lines)
|
output, err := comp.Compile(lines)
|
||||||
|
|
@ -127,8 +127,8 @@ func TestCompilerContext(t *testing.T) {
|
||||||
if ctx.ConstStrHandler == nil {
|
if ctx.ConstStrHandler == nil {
|
||||||
t.Error("ConstStrHandler not initialized")
|
t.Error("ConstStrHandler not initialized")
|
||||||
}
|
}
|
||||||
if ctx.WhileStack == nil {
|
if ctx.LoopEndStack == nil {
|
||||||
t.Error("WhileStack not initialized")
|
t.Error("LoopEndStack not initialized")
|
||||||
}
|
}
|
||||||
if ctx.IfStack == nil {
|
if ctx.IfStack == nil {
|
||||||
t.Error("IfStack not initialized")
|
t.Error("IfStack not initialized")
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,8 @@ type CompilerContext struct {
|
||||||
ConstStrHandler *ConstantStringHandler
|
ConstStrHandler *ConstantStringHandler
|
||||||
|
|
||||||
// Label stacks for control flow
|
// Label stacks for control flow
|
||||||
LoopStack *LabelStack // Start of loop (like WHILE)
|
LoopStartStack *LabelStack // Start of loop (like WHILE)
|
||||||
WhileStack *LabelStack // WHILE...WEND
|
LoopEndStack *LabelStack // WHILE...WEND
|
||||||
IfStack *LabelStack // IF...ENDIF
|
IfStack *LabelStack // IF...ENDIF
|
||||||
GeneralStack *LabelStack // General purpose (GOSUB, etc)
|
GeneralStack *LabelStack // General purpose (GOSUB, etc)
|
||||||
|
|
||||||
|
|
@ -34,8 +34,8 @@ func NewCompilerContext(pragma *preproc.Pragma) *CompilerContext {
|
||||||
ctx := &CompilerContext{
|
ctx := &CompilerContext{
|
||||||
SymbolTable: symTable,
|
SymbolTable: symTable,
|
||||||
ConstStrHandler: constStrHandler,
|
ConstStrHandler: constStrHandler,
|
||||||
LoopStack: NewLabelStack("_LOOP"),
|
LoopStartStack: NewLabelStack("_LOOPSTART"),
|
||||||
WhileStack: NewLabelStack("_WEND"),
|
LoopEndStack: NewLabelStack("_LOOPEND"),
|
||||||
IfStack: NewLabelStack("_I"),
|
IfStack: NewLabelStack("_I"),
|
||||||
GeneralStack: generalStack,
|
GeneralStack: generalStack,
|
||||||
Pragma: pragma,
|
Pragma: pragma,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue