diff --git a/internal/commands/for.go b/internal/commands/for.go index 5bb681c..79a8bcb 100644 --- a/internal/commands/for.go +++ b/internal/commands/for.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "os" "strings" "c65gm/internal/compiler" @@ -106,6 +107,26 @@ func (c *ForCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext) IsVar: endIsVar, } + if c.varKind == compiler.KindByte { + // Error on literal out of range + if !c.startOp.IsVar && c.startOp.Value > 255 { + return fmt.Errorf("FOR: BYTE variable cannot start at literal %d (max 255)", c.startOp.Value) + } + if !c.endOp.IsVar && c.endOp.Value > 255 { + return fmt.Errorf("FOR: BYTE variable cannot loop to literal %d (max 255)", c.endOp.Value) + } + + // Warn on variable type mismatch + if c.startOp.IsVar && c.startOp.VarKind == compiler.KindWord { + _, _ = fmt.Fprintf(os.Stderr, "%s:%d: warning: BYTE loop variable with WORD start value truncates to low byte\n", + line.Filename, line.LineNo) + } + if c.endOp.IsVar && c.endOp.VarKind == compiler.KindWord { + _, _ = fmt.Fprintf(os.Stderr, "%s:%d: warning: BYTE loop variable with WORD end value may cause infinite loop\n", + line.Filename, line.LineNo) + } + } + // Parse optional STEP if len(params) == 8 { if strings.ToUpper(params[6]) != "STEP" {