51 lines
1 KiB
Go
51 lines
1 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"c65gm/internal/compiler"
|
|
"c65gm/internal/preproc"
|
|
"c65gm/internal/utils"
|
|
)
|
|
|
|
// BreakCommand handles BREAK statements
|
|
// Syntax: BREAK
|
|
// Exits current loop
|
|
type BreakCommand struct {
|
|
skipLabel string
|
|
}
|
|
|
|
func (c *BreakCommand) WillHandle(line preproc.Line) bool {
|
|
params, err := utils.ParseParams(line.Text)
|
|
if err != nil || len(params) == 0 {
|
|
return false
|
|
}
|
|
return strings.ToUpper(params[0]) == "BREAK"
|
|
}
|
|
|
|
func (c *BreakCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext) error {
|
|
params, err := utils.ParseParams(line.Text)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(params) != 1 {
|
|
return fmt.Errorf("BREAK: expected 1 parameter, got %d", len(params))
|
|
}
|
|
|
|
// Get skip label from WHILE stack
|
|
var err2 error
|
|
c.skipLabel, err2 = ctx.LoopEndStack.Peek()
|
|
if err2 != nil {
|
|
return fmt.Errorf("BREAK: not inside WHILE loop")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *BreakCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
|
return []string{
|
|
fmt.Sprintf("\tjmp %s", c.skipLabel),
|
|
}, nil
|
|
}
|