c65gm/internal/commands/endif.go

50 lines
1 KiB
Go

package commands
import (
"fmt"
"strings"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
"c65gm/internal/utils"
)
// EndIfCommand handles ENDIF statements to close IF...ENDIF blocks
// Syntax: ENDIF
type EndIfCommand struct {
endLabel string
}
func (c *EndIfCommand) WillHandle(line preproc.Line) bool {
params, err := utils.ParseParams(line.Text)
if err != nil || len(params) == 0 {
return false
}
return strings.ToUpper(params[0]) == "ENDIF"
}
func (c *EndIfCommand) 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("ENDIF: wrong number of parameters (%d), expected 1", len(params))
}
// Pop the end label (from IF or ELSE)
label, err := ctx.IfStack.Pop()
if err != nil {
return fmt.Errorf("ENDIF: %w", err)
}
c.endLabel = label
return nil
}
func (c *EndIfCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
// Just place the end label
return []string{c.endLabel}, nil
}