Added sensible handling for byte iterators

This commit is contained in:
Mattias Hansson 2025-11-18 23:23:05 +01:00
parent 4f51572477
commit 315be292bf

View file

@ -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" {