Added basic equal / not equal versions of if/else/endif

This commit is contained in:
Mattias Hansson 2025-11-02 21:40:28 +01:00
parent 88f90fe5be
commit a0e8bf40ea
8 changed files with 2202 additions and 0 deletions

61
internal/commands/else.go Normal file
View file

@ -0,0 +1,61 @@
package commands
import (
"fmt"
"strings"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
"c65gm/internal/utils"
)
// ElseCommand handles ELSE statements in IF...ELSE...ENDIF blocks
// Syntax: ELSE
type ElseCommand struct {
skipLabel string
endLabel string
}
func (c *ElseCommand) WillHandle(line preproc.Line) bool {
params, err := utils.ParseParams(line.Text)
if err != nil || len(params) == 0 {
return false
}
return strings.ToUpper(params[0]) == "ELSE"
}
func (c *ElseCommand) 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("ELSE: wrong number of parameters (%d), expected 1", len(params))
}
// Pop the IF skip label
label, err := ctx.IfStack.Pop()
if err != nil {
return fmt.Errorf("ELSE: %w", err)
}
c.skipLabel = label
// Push new end label
c.endLabel = ctx.IfStack.Push()
return nil
}
func (c *ElseCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
var asm []string
// Jump to end (skip else block if condition was true)
asm = append(asm, fmt.Sprintf("\tjmp %s", c.endLabel))
// Place skip label (jumped here if condition was false)
asm = append(asm, c.skipLabel)
return asm, nil
}

View file

@ -0,0 +1,264 @@
package commands
import (
"fmt"
"strings"
"testing"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
)
func TestElseCommand_WillHandle(t *testing.T) {
cmd := &ElseCommand{}
tests := []struct {
name string
line string
want bool
}{
{"ELSE", "ELSE", true},
{"not ELSE", "IF a = b", false},
{"empty", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
got := cmd.WillHandle(line)
if got != tt.want {
t.Errorf("WillHandle() = %v, want %v", got, tt.want)
}
})
}
}
func TestEndIfCommand_WillHandle(t *testing.T) {
cmd := &EndIfCommand{}
tests := []struct {
name string
line string
want bool
}{
{"ENDIF", "ENDIF", true},
{"not ENDIF", "IF a = b", false},
{"empty", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
got := cmd.WillHandle(line)
if got != tt.want {
t.Errorf("WillHandle() = %v, want %v", got, tt.want)
}
})
}
}
func TestIfElseEndif_Integration(t *testing.T) {
tests := []struct {
name string
lines []string
setupVars func(*compiler.SymbolTable)
wantAsm []string
}{
{
name: "IF...ENDIF (no ELSE)",
lines: []string{
"IF a = b",
"ENDIF",
},
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 0)
},
wantAsm: []string{
"; IF a = b",
"\tlda a",
"\tcmp b",
"\tbne _I1",
"; ENDIF",
"_I1",
},
},
{
name: "IF...ELSE...ENDIF",
lines: []string{
"IF a = b",
"ELSE",
"ENDIF",
},
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 0)
},
wantAsm: []string{
"; IF a = b",
"\tlda a",
"\tcmp b",
"\tbne _I1",
"; ELSE",
"\tjmp _I2",
"_I1",
"; ENDIF",
"_I2",
},
},
{
name: "nested IF statements",
lines: []string{
"IF a = 10",
"IF b = 20",
"ENDIF",
"ENDIF",
},
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 0)
},
wantAsm: []string{
"; IF a = 10",
"\tlda a",
"\tcmp #$0a",
"\tbne _I1",
"; IF b = 20",
"\tlda b",
"\tcmp #$14",
"\tbne _I2",
"; ENDIF",
"_I2",
"; ENDIF",
"_I1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(preproc.NewPragma())
tt.setupVars(ctx.SymbolTable)
var allAsm []string
for _, lineText := range tt.lines {
line := preproc.Line{Text: lineText, Kind: preproc.Source, PragmaSetIndex: 0}
// Determine which command to use
var cmd compiler.Command
if strings.HasPrefix(strings.ToUpper(lineText), "IF") {
cmd = &IfCommand{}
} else if strings.ToUpper(lineText) == "ELSE" {
cmd = &ElseCommand{}
} else if strings.ToUpper(lineText) == "ENDIF" {
cmd = &EndIfCommand{}
} else {
t.Fatalf("unknown command: %s", lineText)
}
err := cmd.Interpret(line, ctx)
if err != nil {
t.Fatalf("Interpret(%q) error = %v", lineText, err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate(%q) error = %v", lineText, err)
}
allAsm = append(allAsm, fmt.Sprintf("; %s", lineText))
allAsm = append(allAsm, asm...)
}
if !equalAsmElse(allAsm, tt.wantAsm) {
t.Errorf("Assembly mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(allAsm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
func TestElseCommand_Errors(t *testing.T) {
tests := []struct {
name string
line string
wantErr string
}{
{
name: "ELSE without IF",
line: "ELSE",
wantErr: "stack underflow",
},
{
name: "wrong param count",
line: "ELSE extra",
wantErr: "wrong number of parameters",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(preproc.NewPragma())
cmd := &ElseCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
err := cmd.Interpret(line, ctx)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error = %q, want substring %q", err.Error(), tt.wantErr)
}
})
}
}
func TestEndIfCommand_Errors(t *testing.T) {
tests := []struct {
name string
line string
wantErr string
}{
{
name: "ENDIF without IF",
line: "ENDIF",
wantErr: "stack underflow",
},
{
name: "wrong param count",
line: "ENDIF extra",
wantErr: "wrong number of parameters",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(preproc.NewPragma())
cmd := &EndIfCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
err := cmd.Interpret(line, ctx)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error = %q, want substring %q", err.Error(), tt.wantErr)
}
})
}
}
// equalAsmElse compares two assembly slices for equality
func equalAsmElse(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

View file

@ -0,0 +1,50 @@
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
}

637
internal/commands/if.go Normal file
View file

@ -0,0 +1,637 @@
package commands
import (
"fmt"
"strings"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
"c65gm/internal/utils"
)
// IfCommand handles IF conditional statements
// Syntax:
//
// IF <param1> <op> <param2> # basic syntax
// IF <param1> <op> <param2> THEN # optional THEN keyword
//
// Supported operators (for now): =, ==, <>, !=
// More operators (>, <, >=, <=) can be added later
//
// Uses short jumps by default (inverted branch condition)
// Uses long jumps if pragma _P_USE_LONG_JUMP is set
type IfCommand struct {
operator string // =, <>, etc.
param1VarName string
param1VarKind compiler.VarKind
param1Value uint16
param1IsVar bool
param2VarName string
param2VarKind compiler.VarKind
param2Value uint16
param2IsVar bool
useLongJump bool
skipLabel string
}
func (c *IfCommand) WillHandle(line preproc.Line) bool {
params, err := utils.ParseParams(line.Text)
if err != nil || len(params) == 0 {
return false
}
return strings.ToUpper(params[0]) == "IF"
}
func (c *IfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext) error {
params, err := utils.ParseParams(line.Text)
if err != nil {
return err
}
paramCount := len(params)
// IF <param1> <op> <param2> [THEN]
if paramCount != 4 && paramCount != 5 {
return fmt.Errorf("IF: wrong number of parameters (%d), expected 4 or 5", paramCount)
}
// Check optional THEN keyword
if paramCount == 5 {
if strings.ToUpper(params[4]) != "THEN" {
return fmt.Errorf("IF: parameter #5 must be 'THEN', got %q", params[4])
}
}
// Parse operator
c.operator = params[2]
switch c.operator {
case "=", "==":
c.operator = "=" // normalize
case "<>", "!=":
c.operator = "<>" // normalize
default:
return fmt.Errorf("IF: unsupported operator %q (only =, ==, <>, != supported for now)", c.operator)
}
scope := ctx.CurrentScope()
// Create constant lookup function
constLookup := func(name string) (int64, bool) {
sym := ctx.SymbolTable.Lookup(name, scope)
if sym != nil && sym.IsConst() {
return int64(sym.Value), true
}
return 0, false
}
// Parse param1
c.param1VarName, c.param1VarKind, c.param1Value, c.param1IsVar, err = compiler.ParseOperandParam(
params[1], ctx.SymbolTable, scope, constLookup)
if err != nil {
return fmt.Errorf("IF: param1: %w", err)
}
// Parse param2
c.param2VarName, c.param2VarKind, c.param2Value, c.param2IsVar, err = compiler.ParseOperandParam(
params[3], ctx.SymbolTable, scope, constLookup)
if err != nil {
return fmt.Errorf("IF: param2: %w", err)
}
// Check pragma for long jump
ps := ctx.Pragma.GetPragmaSetByIndex(line.PragmaSetIndex)
longJumpPragma := ps.GetPragma("_P_USE_LONG_JUMP")
c.useLongJump = longJumpPragma != "" && longJumpPragma != "0"
// Push skip label onto IF stack
c.skipLabel = ctx.IfStack.Push()
return nil
}
func (c *IfCommand) Generate(ctx *compiler.CompilerContext) ([]string, error) {
switch c.operator {
case "=":
return c.generateEqual(ctx)
case "<>":
return c.generateNotEqual(ctx)
default:
return nil, fmt.Errorf("IF: internal error - unsupported operator %q", c.operator)
}
}
// generateEqual generates code for == comparison
func (c *IfCommand) generateEqual(ctx *compiler.CompilerContext) ([]string, error) {
var asm []string
// Constant folding: both literals
if !c.param1IsVar && !c.param2IsVar {
if c.param1Value != c.param2Value {
// Always false - skip entire IF block
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
}
// If equal, do nothing (condition always true)
return asm, nil
}
// Generate comparison based on types
if c.useLongJump {
return c.generateEqualLongJump(ctx)
}
return c.generateEqualShortJump(ctx)
}
// generateEqualShortJump generates optimized short jumps (inverted condition)
func (c *IfCommand) generateEqualShortJump(_ *compiler.CompilerContext) ([]string, error) {
var asm []string
// Determine effective types for comparison
kind1, kind2 := c.param1VarKind, c.param2VarKind
if !c.param1IsVar {
kind1 = inferKindFromValue(c.param1Value)
}
if !c.param2IsVar {
kind2 = inferKindFromValue(c.param2Value)
}
// byte == byte
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
}
// Inverted: if NOT equal, skip
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
return asm, nil
}
// word == word
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
// Compare low bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
}
// If low bytes differ, skip
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
// Compare high bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
}
// If high bytes differ, skip
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
return asm, nil
}
// Mixed byte/word comparisons - extend byte to word
// byte == word or word == byte
var byteVal uint16
var byteIsVar bool
var byteName string
var wordVal uint16
var wordIsVar bool
var wordName string
if kind1 == compiler.KindByte {
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
} else {
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
}
// Check word high byte must be 0
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
}
asm = append(asm, "\tcmp #0")
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
// Compare low bytes
if byteIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
}
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
}
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
return asm, nil
}
// generateEqualLongJump generates traditional long jumps (old style)
func (c *IfCommand) generateEqualLongJump(ctx *compiler.CompilerContext) ([]string, error) {
var asm []string
successLabel := ctx.GeneralStack.Push() // temporary label
// Similar logic but with inverted branches
kind1, kind2 := c.param1VarKind, c.param2VarKind
if !c.param1IsVar {
kind1 = inferKindFromValue(c.param1Value)
}
if !c.param2IsVar {
kind2 = inferKindFromValue(c.param2Value)
}
// byte == byte
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
}
asm = append(asm, fmt.Sprintf("\tbeq %s", successLabel))
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// word == word
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
// Compare low bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
}
failLabel := ctx.GeneralStack.Push()
asm = append(asm, fmt.Sprintf("\tbne %s", failLabel))
// Compare high bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
}
asm = append(asm, fmt.Sprintf("\tbeq %s", successLabel))
asm = append(asm, failLabel)
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// Mixed comparisons similar to short jump
var byteVal uint16
var byteIsVar bool
var byteName string
var wordVal uint16
var wordIsVar bool
var wordName string
if kind1 == compiler.KindByte {
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
} else {
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
}
failLabel := ctx.GeneralStack.Push()
// Check word high byte must be 0
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
}
asm = append(asm, "\tcmp #0")
asm = append(asm, fmt.Sprintf("\tbne %s", failLabel))
// Compare low bytes
if byteIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
}
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
}
asm = append(asm, fmt.Sprintf("\tbeq %s", successLabel))
asm = append(asm, failLabel)
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// generateNotEqual generates code for != comparison
func (c *IfCommand) generateNotEqual(ctx *compiler.CompilerContext) ([]string, error) {
var asm []string
// Constant folding: both literals
if !c.param1IsVar && !c.param2IsVar {
if c.param1Value == c.param2Value {
// Always false - skip entire IF block
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
}
// If not equal, do nothing (condition always true)
return asm, nil
}
// Generate comparison based on types
if c.useLongJump {
return c.generateNotEqualLongJump(ctx)
}
return c.generateNotEqualShortJump(ctx)
}
// generateNotEqualShortJump generates optimized short jumps for !=
func (c *IfCommand) generateNotEqualShortJump(ctx *compiler.CompilerContext) ([]string, error) {
var asm []string
kind1, kind2 := c.param1VarKind, c.param2VarKind
if !c.param1IsVar {
kind1 = inferKindFromValue(c.param1Value)
}
if !c.param2IsVar {
kind2 = inferKindFromValue(c.param2Value)
}
// byte != byte
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
}
// Inverted: if EQUAL, skip
asm = append(asm, fmt.Sprintf("\tbeq %s", c.skipLabel))
return asm, nil
}
// word != word - need to check if ANY byte differs
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
successLabel := ctx.GeneralStack.Push()
// Compare low bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
}
// If low bytes differ, condition is true - continue
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
// Compare high bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
}
// If high bytes differ, condition is true - continue
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
// Both bytes equal - skip
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// Mixed byte/word - similar logic
var byteVal uint16
var byteIsVar bool
var byteName string
var wordVal uint16
var wordIsVar bool
var wordName string
if kind1 == compiler.KindByte {
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
} else {
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
}
successLabel := ctx.GeneralStack.Push()
// Check word high byte != 0 means not equal
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
}
asm = append(asm, "\tcmp #0")
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
// Compare low bytes
if byteIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
}
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
}
asm = append(asm, fmt.Sprintf("\tbeq %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// generateNotEqualLongJump generates traditional long jumps for !=
func (c *IfCommand) generateNotEqualLongJump(ctx *compiler.CompilerContext) ([]string, error) {
var asm []string
successLabel := ctx.GeneralStack.Push()
kind1, kind2 := c.param1VarKind, c.param2VarKind
if !c.param1IsVar {
kind1 = inferKindFromValue(c.param1Value)
}
if !c.param2IsVar {
kind2 = inferKindFromValue(c.param2Value)
}
// byte != byte
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
}
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// word != word
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
// Compare low bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
}
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
// Compare high bytes
if c.param1IsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
}
if c.param2IsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
}
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// Mixed byte/word
var byteVal uint16
var byteIsVar bool
var byteName string
var wordVal uint16
var wordIsVar bool
var wordName string
if kind1 == compiler.KindByte {
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
} else {
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
}
// Check word high byte != 0
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
}
asm = append(asm, "\tcmp #0")
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
// Compare low bytes
if byteIsVar {
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
} else {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
}
if wordIsVar {
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
} else {
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
}
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
asm = append(asm, successLabel)
return asm, nil
}
// inferKindFromValue determines if a literal value is byte or word
func inferKindFromValue(val uint16) compiler.VarKind {
if val <= 255 {
return compiler.KindByte
}
return compiler.KindWord
}

View file

@ -0,0 +1,571 @@
package commands
import (
"strings"
"testing"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
)
func TestIfCommand_WillHandle(t *testing.T) {
cmd := &IfCommand{}
tests := []struct {
name string
line string
want bool
}{
{"basic IF", "IF a = b", true},
{"IF with THEN", "IF a = b THEN", true},
{"not IF", "LET a = b", false},
{"empty", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
got := cmd.WillHandle(line)
if got != tt.want {
t.Errorf("WillHandle() = %v, want %v", got, tt.want)
}
})
}
}
func TestIfCommand_Equal_ShortJump(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantAsm []string
}{
{
name: "byte var == byte var",
line: "IF a = b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 10)
st.AddVar("b", "", compiler.KindByte, 20)
},
wantAsm: []string{
"\tlda a",
"\tcmp b",
"\tbne _I1",
},
},
{
name: "byte var == byte literal",
line: "IF a = 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda a",
"\tcmp #$64",
"\tbne _I1",
},
},
{
name: "byte literal == byte var",
line: "IF 100 = a",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda #$64",
"\tcmp a",
"\tbne _I1",
},
},
{
name: "word var == word var",
line: "IF x = y",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0x1234)
st.AddVar("y", "", compiler.KindWord, 0x5678)
},
wantAsm: []string{
"\tlda x",
"\tcmp y",
"\tbne _I1",
"\tlda x+1",
"\tcmp y+1",
"\tbne _I1",
},
},
{
name: "word var == word literal",
line: "IF x == $1234",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda x",
"\tcmp #$34",
"\tbne _I1",
"\tlda x+1",
"\tcmp #$12",
"\tbne _I1",
},
},
{
name: "word literal == word var",
line: "IF $1234 = x",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$34",
"\tcmp x",
"\tbne _I1",
"\tlda #$12",
"\tcmp x+1",
"\tbne _I1",
},
},
{
name: "byte var == word var (mixed)",
line: "IF b = x",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("b", "", compiler.KindByte, 50)
st.AddVar("x", "", compiler.KindWord, 100)
},
wantAsm: []string{
"\tlda x+1",
"\tcmp #0",
"\tbne _I1",
"\tlda b",
"\tcmp x",
"\tbne _I1",
},
},
{
name: "word var == byte var (mixed)",
line: "IF x = b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 100)
st.AddVar("b", "", compiler.KindByte, 50)
},
wantAsm: []string{
"\tlda x+1",
"\tcmp #0",
"\tbne _I1",
"\tlda b",
"\tcmp x",
"\tbne _I1",
},
},
{
name: "constant folding - equal",
line: "IF 100 = 100",
setupVars: func(st *compiler.SymbolTable) {},
wantAsm: []string{},
},
{
name: "constant folding - not equal",
line: "IF 100 = 200",
setupVars: func(st *compiler.SymbolTable) {},
wantAsm: []string{
"\tjmp _I1",
},
},
{
name: "with THEN keyword",
line: "IF a = b THEN",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda a",
"\tcmp b",
"\tbne _I1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(preproc.NewPragma())
tt.setupVars(ctx.SymbolTable)
cmd := &IfCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source, PragmaSetIndex: 0}
err := cmd.Interpret(line, ctx)
if err != nil {
t.Fatalf("Interpret() error = %v", err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate() error = %v", err)
}
if !equalAsmIf(asm, tt.wantAsm) {
t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(asm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
func TestIfCommand_Equal_LongJump(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantAsm []string
}{
{
name: "byte var == byte var (long jump)",
line: "IF a = b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 10)
st.AddVar("b", "", compiler.KindByte, 20)
},
wantAsm: []string{
"\tlda a",
"\tcmp b",
"\tbeq _L1",
"\tjmp _I1",
"_L1",
},
},
{
name: "byte var == byte literal (long jump)",
line: "IF a = 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda a",
"\tcmp #$64",
"\tbeq _L1",
"\tjmp _I1",
"_L1",
},
},
{
name: "word var == word var (long jump)",
line: "IF x = y",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0x1234)
st.AddVar("y", "", compiler.KindWord, 0x5678)
},
wantAsm: []string{
"\tlda x",
"\tcmp y",
"\tbne _L2",
"\tlda x+1",
"\tcmp y+1",
"\tbeq _L1",
"_L2",
"\tjmp _I1",
"_L1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pragma := preproc.NewPragma()
pragma.AddPragma("_P_USE_LONG_JUMP", "1")
ctx := compiler.NewCompilerContext(pragma)
tt.setupVars(ctx.SymbolTable)
cmd := &IfCommand{}
line := preproc.Line{
Text: tt.line,
Kind: preproc.Source,
PragmaSetIndex: pragma.GetCurrentPragmaSetIndex(),
}
err := cmd.Interpret(line, ctx)
if err != nil {
t.Fatalf("Interpret() error = %v", err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate() error = %v", err)
}
if !equalAsmIf(asm, tt.wantAsm) {
t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(asm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
func TestIfCommand_NotEqual_ShortJump(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantAsm []string
}{
{
name: "byte var != byte var",
line: "IF a <> b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 10)
st.AddVar("b", "", compiler.KindByte, 20)
},
wantAsm: []string{
"\tlda a",
"\tcmp b",
"\tbeq _I1",
},
},
{
name: "byte var != byte literal",
line: "IF a != 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda a",
"\tcmp #$64",
"\tbeq _I1",
},
},
{
name: "word var != word var",
line: "IF x <> y",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0x1234)
st.AddVar("y", "", compiler.KindWord, 0x5678)
},
wantAsm: []string{
"\tlda x",
"\tcmp y",
"\tbne _L1",
"\tlda x+1",
"\tcmp y+1",
"\tbne _L1",
"\tjmp _I1",
"_L1",
},
},
{
name: "word var != word literal",
line: "IF x != $1234",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda x",
"\tcmp #$34",
"\tbne _L1",
"\tlda x+1",
"\tcmp #$12",
"\tbne _L1",
"\tjmp _I1",
"_L1",
},
},
{
name: "byte var != word var (mixed)",
line: "IF b <> x",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("b", "", compiler.KindByte, 50)
st.AddVar("x", "", compiler.KindWord, 100)
},
wantAsm: []string{
"\tlda x+1",
"\tcmp #0",
"\tbne _L1",
"\tlda b",
"\tcmp x",
"\tbeq _I1",
"_L1",
},
},
{
name: "constant folding - not equal",
line: "IF 100 <> 200",
setupVars: func(st *compiler.SymbolTable) {},
wantAsm: []string{},
},
{
name: "constant folding - equal",
line: "IF 100 != 100",
setupVars: func(st *compiler.SymbolTable) {},
wantAsm: []string{
"\tjmp _I1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(preproc.NewPragma())
tt.setupVars(ctx.SymbolTable)
cmd := &IfCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source, PragmaSetIndex: 0}
err := cmd.Interpret(line, ctx)
if err != nil {
t.Fatalf("Interpret() error = %v", err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate() error = %v", err)
}
if !equalAsmIf(asm, tt.wantAsm) {
t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(asm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
func TestIfCommand_NotEqual_LongJump(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantAsm []string
}{
{
name: "byte var != byte var (long jump)",
line: "IF a <> b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 10)
st.AddVar("b", "", compiler.KindByte, 20)
},
wantAsm: []string{
"\tlda a",
"\tcmp b",
"\tbne _L1",
"\tjmp _I1",
"_L1",
},
},
{
name: "word var != word var (long jump)",
line: "IF x <> y",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0x1234)
st.AddVar("y", "", compiler.KindWord, 0x5678)
},
wantAsm: []string{
"\tlda x",
"\tcmp y",
"\tbne _L1",
"\tlda x+1",
"\tcmp y+1",
"\tbne _L1",
"\tjmp _I1",
"_L1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pragma := preproc.NewPragma()
pragma.AddPragma("_P_USE_LONG_JUMP", "1")
ctx := compiler.NewCompilerContext(pragma)
tt.setupVars(ctx.SymbolTable)
cmd := &IfCommand{}
line := preproc.Line{
Text: tt.line,
Kind: preproc.Source,
PragmaSetIndex: pragma.GetCurrentPragmaSetIndex(),
}
err := cmd.Interpret(line, ctx)
if err != nil {
t.Fatalf("Interpret() error = %v", err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate() error = %v", err)
}
if !equalAsmIf(asm, tt.wantAsm) {
t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(asm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
func TestIfCommand_Errors(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantErr string
}{
{
name: "wrong param count",
line: "IF a = b = c",
setupVars: func(st *compiler.SymbolTable) {},
wantErr: "wrong number of parameters",
},
{
name: "unsupported operator",
line: "IF a > b",
setupVars: func(st *compiler.SymbolTable) {},
wantErr: "unsupported operator",
},
{
name: "unknown variable",
line: "IF unknown = b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("b", "", compiler.KindByte, 0)
},
wantErr: "not a valid variable or expression",
},
{
name: "invalid THEN",
line: "IF a = b NOT",
setupVars: func(st *compiler.SymbolTable) {},
wantErr: "must be 'THEN'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(preproc.NewPragma())
tt.setupVars(ctx.SymbolTable)
cmd := &IfCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source, PragmaSetIndex: 0}
err := cmd.Interpret(line, ctx)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error = %q, want substring %q", err.Error(), tt.wantErr)
}
})
}
}
// equalAsmIf compares two assembly slices for equality
func equalAsmIf(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

190
internal/commands/let.go Normal file
View file

@ -0,0 +1,190 @@
package commands
import (
"fmt"
"strings"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
"c65gm/internal/utils"
)
// LetCommand handles LET/assignment operations
// Syntax:
//
// LET <dest> GET <source> # old syntax with GET
// LET <dest> = <source> # old syntax with =
// <dest> = <source> # new syntax
//
// Note: Differs from arithmetic ops by param count (3 vs 5)
type LetCommand struct {
sourceVarName string
sourceVarKind compiler.VarKind
sourceValue uint16
sourceIsVar bool
destVarName string
destVarKind compiler.VarKind
}
func (c *LetCommand) WillHandle(line preproc.Line) bool {
params, err := utils.ParseParams(line.Text)
if err != nil || len(params) == 0 {
return false
}
// Old syntax: LET ... (must have exactly 4 params)
if strings.ToUpper(params[0]) == "LET" && len(params) == 4 {
return true
}
// New syntax: <dest> = <source> (exactly 3 params)
if len(params) == 3 && params[1] == "=" {
return true
}
return false
}
func (c *LetCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext) error {
// Clear state
c.sourceVarName = ""
c.sourceIsVar = false
c.sourceValue = 0
c.destVarName = ""
params, err := utils.ParseParams(line.Text)
if err != nil {
return err
}
paramCount := len(params)
scope := ctx.CurrentScope()
// Create constant lookup function
constLookup := func(name string) (int64, bool) {
sym := ctx.SymbolTable.Lookup(name, scope)
if sym != nil && sym.IsConst() {
return int64(sym.Value), true
}
return 0, false
}
// Determine syntax and parse accordingly
if strings.ToUpper(params[0]) == "LET" {
// Old syntax: LET <dest> GET/= <source>
if paramCount != 4 {
return fmt.Errorf("LET: wrong number of parameters (%d), expected 4", paramCount)
}
separator := strings.ToUpper(params[2])
if separator != "GET" && separator != "=" {
return fmt.Errorf("LET: parameter #3 must be 'GET' or '=', got %q", params[2])
}
// Parse destination
destName := params[1]
destSym := ctx.SymbolTable.Lookup(destName, scope)
if destSym == nil {
return fmt.Errorf("LET: unknown variable %q", destName)
}
if destSym.IsConst() {
return fmt.Errorf("LET: cannot assign to constant %q", destName)
}
c.destVarName = destSym.FullName()
c.destVarKind = destSym.GetVarKind()
// Parse source
c.sourceVarName, c.sourceVarKind, c.sourceValue, c.sourceIsVar, err = compiler.ParseOperandParam(
params[3], ctx.SymbolTable, scope, constLookup)
if err != nil {
return fmt.Errorf("LET: source: %w", err)
}
} else {
// New syntax: <dest> = <source>
if paramCount != 3 {
return fmt.Errorf("LET: wrong number of parameters (%d), expected 3", paramCount)
}
if params[1] != "=" {
return fmt.Errorf("LET: expected '=' at position 2, got %q", params[1])
}
// Parse destination
destName := params[0]
destSym := ctx.SymbolTable.Lookup(destName, scope)
if destSym == nil {
return fmt.Errorf("LET: unknown variable %q", destName)
}
if destSym.IsConst() {
return fmt.Errorf("LET: cannot assign to constant %q", destName)
}
c.destVarName = destSym.FullName()
c.destVarKind = destSym.GetVarKind()
// Parse source
c.sourceVarName, c.sourceVarKind, c.sourceValue, c.sourceIsVar, err = compiler.ParseOperandParam(
params[2], ctx.SymbolTable, scope, constLookup)
if err != nil {
return fmt.Errorf("LET: source: %w", err)
}
}
return nil
}
func (c *LetCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
var asm []string
// Variable assignment
if c.sourceIsVar {
// Destination: byte
if c.destVarKind == compiler.KindByte {
// byte → byte or word → byte (take low byte)
asm = append(asm, fmt.Sprintf("\tlda %s", c.sourceVarName))
asm = append(asm, fmt.Sprintf("\tsta %s", c.destVarName))
return asm, nil
}
// Destination: word
// byte → word (zero-extend)
if c.sourceVarKind == compiler.KindByte {
asm = append(asm, fmt.Sprintf("\tlda %s", c.sourceVarName))
asm = append(asm, fmt.Sprintf("\tsta %s", c.destVarName))
asm = append(asm, "\tlda #0")
asm = append(asm, fmt.Sprintf("\tsta %s+1", c.destVarName))
return asm, nil
}
// word → word (copy both bytes)
asm = append(asm, fmt.Sprintf("\tlda %s", c.sourceVarName))
asm = append(asm, fmt.Sprintf("\tsta %s", c.destVarName))
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.sourceVarName))
asm = append(asm, fmt.Sprintf("\tsta %s+1", c.destVarName))
return asm, nil
}
// Literal assignment
lo := uint8(c.sourceValue & 0xFF)
hi := uint8((c.sourceValue >> 8) & 0xFF)
// Destination: byte
if c.destVarKind == compiler.KindByte {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", lo))
asm = append(asm, fmt.Sprintf("\tsta %s", c.destVarName))
return asm, nil
}
// Destination: word
asm = append(asm, fmt.Sprintf("\tlda #$%02x", lo))
asm = append(asm, fmt.Sprintf("\tsta %s", c.destVarName))
// Optimization: don't reload if lo == hi (common for $0000, $FFFF)
if lo != hi {
asm = append(asm, fmt.Sprintf("\tlda #$%02x", hi))
}
asm = append(asm, fmt.Sprintf("\tsta %s+1", c.destVarName))
return asm, nil
}

View file

@ -0,0 +1,425 @@
package commands
import (
"strings"
"testing"
"c65gm/internal/compiler"
"c65gm/internal/preproc"
)
func TestLetCommand_WillHandle(t *testing.T) {
cmd := &LetCommand{}
tests := []struct {
name string
line string
want bool
}{
{"old syntax LET/GET", "LET a GET b", true},
{"old syntax LET/equals", "LET a = 10", true},
{"new syntax", "result = value", true},
{"not LET - arithmetic", "result = a + b", false}, // 5 params
{"not LET - keyword", "ADD a TO b GIVING c", false},
{"wrong param count", "LET a b", false},
{"empty", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
got := cmd.WillHandle(line)
if got != tt.want {
t.Errorf("WillHandle() = %v, want %v", got, tt.want)
}
})
}
}
func TestLetCommand_OldSyntax(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantAsm []string
wantErr bool
}{
{
name: "LET byte GET byte",
line: "LET a GET b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 10)
},
wantAsm: []string{
"\tlda b",
"\tsta a",
},
},
{
name: "LET byte = literal",
line: "LET a = 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda #$64",
"\tsta a",
},
},
{
name: "LET word GET word",
line: "LET x GET y",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
st.AddVar("y", "", compiler.KindWord, 0x1234)
},
wantAsm: []string{
"\tlda y",
"\tsta x",
"\tlda y+1",
"\tsta x+1",
},
},
{
name: "LET word = literal",
line: "LET x = $1234",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$34",
"\tsta x",
"\tlda #$12",
"\tsta x+1",
},
},
{
name: "LET word GET byte (zero-extend)",
line: "LET x GET b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
st.AddVar("b", "", compiler.KindByte, 100)
},
wantAsm: []string{
"\tlda b",
"\tsta x",
"\tlda #0",
"\tsta x+1",
},
},
{
name: "LET byte GET word (take low byte)",
line: "LET b GET x",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("b", "", compiler.KindByte, 0)
st.AddVar("x", "", compiler.KindWord, 0x1234)
},
wantAsm: []string{
"\tlda x",
"\tsta b",
},
},
{
name: "LET word = $0000 (optimization)",
line: "LET x = 0",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$00",
"\tsta x",
"\tsta x+1",
},
},
{
name: "LET word = $FFFF (optimization)",
line: "LET x = $FFFF",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$ff",
"\tsta x",
"\tsta x+1",
},
},
{
name: "LET with constant",
line: "LET a = MAXVAL",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddConst("MAXVAL", "", compiler.KindByte, 255)
},
wantAsm: []string{
"\tlda #$ff",
"\tsta a",
},
},
{
name: "error: unknown destination",
line: "LET unknown GET a",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantErr: true,
},
{
name: "error: wrong separator",
line: "LET a TO b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 0)
},
wantErr: true,
},
{
name: "error: cannot assign to constant",
line: "LET MAXVAL = 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddConst("MAXVAL", "", compiler.KindByte, 255)
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(&preproc.Pragma{})
tt.setupVars(ctx.SymbolTable)
cmd := &LetCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
err := cmd.Interpret(line, ctx)
if tt.wantErr {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("Interpret() error = %v", err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate() error = %v", err)
}
if !equalAsmLet(asm, tt.wantAsm) {
t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(asm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
func TestLetCommand_NewSyntax(t *testing.T) {
tests := []struct {
name string
line string
setupVars func(*compiler.SymbolTable)
wantAsm []string
wantErr bool
}{
{
name: "byte = byte",
line: "a = b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddVar("b", "", compiler.KindByte, 10)
},
wantAsm: []string{
"\tlda b",
"\tsta a",
},
},
{
name: "byte = literal",
line: "a = 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda #$64",
"\tsta a",
},
},
{
name: "word = word",
line: "x = y",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
st.AddVar("y", "", compiler.KindWord, 0x1234)
},
wantAsm: []string{
"\tlda y",
"\tsta x",
"\tlda y+1",
"\tsta x+1",
},
},
{
name: "word = literal",
line: "x = $1234",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$34",
"\tsta x",
"\tlda #$12",
"\tsta x+1",
},
},
{
name: "word = byte (zero-extend)",
line: "x = b",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
st.AddVar("b", "", compiler.KindByte, 100)
},
wantAsm: []string{
"\tlda b",
"\tsta x",
"\tlda #0",
"\tsta x+1",
},
},
{
name: "byte = word (take low byte)",
line: "b = x",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("b", "", compiler.KindByte, 0)
st.AddVar("x", "", compiler.KindWord, 0x1234)
},
wantAsm: []string{
"\tlda x",
"\tsta b",
},
},
{
name: "word = 0 (optimization)",
line: "x = 0",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$00",
"\tsta x",
"\tsta x+1",
},
},
{
name: "word = $FFFF (optimization)",
line: "x = 65535",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$ff",
"\tsta x",
"\tsta x+1",
},
},
{
name: "word = $0102 (different bytes, no optimization)",
line: "x = $0102",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("x", "", compiler.KindWord, 0)
},
wantAsm: []string{
"\tlda #$02",
"\tsta x",
"\tlda #$01",
"\tsta x+1",
},
},
{
name: "using constant",
line: "a = MAXVAL",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
st.AddConst("MAXVAL", "", compiler.KindByte, 255)
},
wantAsm: []string{
"\tlda #$ff",
"\tsta a",
},
},
{
name: "expression with constant",
line: "a = 10+5",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantAsm: []string{
"\tlda #$0f",
"\tsta a",
},
},
{
name: "error: unknown destination",
line: "unknown = a",
setupVars: func(st *compiler.SymbolTable) {
st.AddVar("a", "", compiler.KindByte, 0)
},
wantErr: true,
},
{
name: "error: cannot assign to constant",
line: "MAXVAL = 100",
setupVars: func(st *compiler.SymbolTable) {
st.AddConst("MAXVAL", "", compiler.KindByte, 255)
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := compiler.NewCompilerContext(&preproc.Pragma{})
tt.setupVars(ctx.SymbolTable)
cmd := &LetCommand{}
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
err := cmd.Interpret(line, ctx)
if tt.wantErr {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("Interpret() error = %v", err)
}
asm, err := cmd.Generate(ctx)
if err != nil {
t.Fatalf("Generate() error = %v", err)
}
if !equalAsmLet(asm, tt.wantAsm) {
t.Errorf("Generate() mismatch\ngot:\n%s\nwant:\n%s",
strings.Join(asm, "\n"),
strings.Join(tt.wantAsm, "\n"))
}
})
}
}
// equalAsmLet compares two assembly slices for equality
func equalAsmLet(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

View file

@ -83,6 +83,10 @@ func registerCommands(comp *compiler.Compiler) {
comp.Registry().Register(&commands.OrCommand{})
comp.Registry().Register(&commands.XorCommand{})
comp.Registry().Register(&commands.SubtractCommand{})
comp.Registry().Register(&commands.LetCommand{})
comp.Registry().Register(&commands.IfCommand{})
comp.Registry().Register(&commands.ElseCommand{})
comp.Registry().Register(&commands.EndIfCommand{})
}
func writeOutput(filename string, lines []string) error {