Updated while_test.go to new code generation. Added new versions of IF, ELSE, ENDIF based on comparison.go
This commit is contained in:
parent
df7e5349ac
commit
3d6f786949
6 changed files with 645 additions and 1370 deletions
|
|
@ -9,11 +9,12 @@ import (
|
||||||
"c65gm/internal/utils"
|
"c65gm/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ElseCommand handles ELSE statements in IF...ELSE...ENDIF blocks
|
// ElseCommand handles ELSE statements
|
||||||
// Syntax: ELSE
|
// Syntax: ELSE
|
||||||
|
// Marks alternative branch in IF...ELSE...ENDIF
|
||||||
type ElseCommand struct {
|
type ElseCommand struct {
|
||||||
skipLabel string
|
skipLabel string
|
||||||
endLabel string
|
endifLabel string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ElseCommand) WillHandle(line preproc.Line) bool {
|
func (c *ElseCommand) WillHandle(line preproc.Line) bool {
|
||||||
|
|
@ -21,7 +22,6 @@ func (c *ElseCommand) WillHandle(line preproc.Line) bool {
|
||||||
if err != nil || len(params) == 0 {
|
if err != nil || len(params) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ToUpper(params[0]) == "ELSE"
|
return strings.ToUpper(params[0]) == "ELSE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,30 +32,25 @@ func (c *ElseCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(params) != 1 {
|
if len(params) != 1 {
|
||||||
return fmt.Errorf("ELSE: wrong number of parameters (%d), expected 1", len(params))
|
return fmt.Errorf("ELSE: expected 1 parameter, got %d", len(params))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop the IF skip label
|
// Pop skip label (where IF jumps on FALSE)
|
||||||
label, err := ctx.IfStack.Pop()
|
var err2 error
|
||||||
if err != nil {
|
c.skipLabel, err2 = ctx.IfStack.Pop()
|
||||||
return fmt.Errorf("ELSE: %w", err)
|
if err2 != nil {
|
||||||
|
return fmt.Errorf("ELSE: not inside IF block")
|
||||||
}
|
}
|
||||||
c.skipLabel = label
|
|
||||||
|
|
||||||
// Push new end label
|
// Push new endif label (where to jump after IF block)
|
||||||
c.endLabel = ctx.IfStack.Push()
|
c.endifLabel = ctx.IfStack.Push()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ElseCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
func (c *ElseCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
||||||
var asm []string
|
return []string{
|
||||||
|
fmt.Sprintf("\tjmp %s", c.endifLabel),
|
||||||
// Jump to end (skip else block if condition was true)
|
c.skipLabel,
|
||||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.endLabel))
|
}, nil
|
||||||
|
|
||||||
// Place skip label (jumped here if condition was false)
|
|
||||||
asm = append(asm, c.skipLabel)
|
|
||||||
|
|
||||||
return asm, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,264 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
@ -9,8 +9,9 @@ import (
|
||||||
"c65gm/internal/utils"
|
"c65gm/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EndIfCommand handles ENDIF statements to close IF...ENDIF blocks
|
// EndIfCommand handles ENDIF statements
|
||||||
// Syntax: ENDIF
|
// Syntax: ENDIF
|
||||||
|
// Ends current IF block
|
||||||
type EndIfCommand struct {
|
type EndIfCommand struct {
|
||||||
endLabel string
|
endLabel string
|
||||||
}
|
}
|
||||||
|
|
@ -20,7 +21,6 @@ func (c *EndIfCommand) WillHandle(line preproc.Line) bool {
|
||||||
if err != nil || len(params) == 0 {
|
if err != nil || len(params) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ToUpper(params[0]) == "ENDIF"
|
return strings.ToUpper(params[0]) == "ENDIF"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,20 +31,19 @@ func (c *EndIfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContex
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(params) != 1 {
|
if len(params) != 1 {
|
||||||
return fmt.Errorf("ENDIF: wrong number of parameters (%d), expected 1", len(params))
|
return fmt.Errorf("ENDIF: expected 1 parameter, got %d", len(params))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop the end label (from IF or ELSE)
|
// Pop end label
|
||||||
label, err := ctx.IfStack.Pop()
|
var err2 error
|
||||||
if err != nil {
|
c.endLabel, err2 = ctx.IfStack.Pop()
|
||||||
return fmt.Errorf("ENDIF: %w", err)
|
if err2 != nil {
|
||||||
|
return fmt.Errorf("ENDIF: not inside IF block")
|
||||||
}
|
}
|
||||||
c.endLabel = label
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EndIfCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
func (c *EndIfCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
||||||
// Just place the end label
|
|
||||||
return []string{c.endLabel}, nil
|
return []string{c.endLabel}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,29 +10,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// IfCommand handles IF conditional statements
|
// IfCommand handles IF conditional statements
|
||||||
// Syntax:
|
// Syntax: IF <param1> <op> <param2>
|
||||||
//
|
// Operators: =, ==, <>, !=, >, <, >=, <=
|
||||||
// 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 {
|
type IfCommand struct {
|
||||||
operator string // =, <>, etc.
|
operator string
|
||||||
|
param1 *operandInfo
|
||||||
param1VarName string
|
param2 *operandInfo
|
||||||
param1VarKind compiler.VarKind
|
|
||||||
param1Value uint16
|
|
||||||
param1IsVar bool
|
|
||||||
|
|
||||||
param2VarName string
|
|
||||||
param2VarKind compiler.VarKind
|
|
||||||
param2Value uint16
|
|
||||||
param2IsVar bool
|
|
||||||
|
|
||||||
useLongJump bool
|
useLongJump bool
|
||||||
skipLabel string
|
skipLabel string
|
||||||
}
|
}
|
||||||
|
|
@ -42,7 +25,6 @@ func (c *IfCommand) WillHandle(line preproc.Line) bool {
|
||||||
if err != nil || len(params) == 0 {
|
if err != nil || len(params) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ToUpper(params[0]) == "IF"
|
return strings.ToUpper(params[0]) == "IF"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,34 +34,13 @@ func (c *IfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
paramCount := len(params)
|
if len(params) != 4 {
|
||||||
|
return fmt.Errorf("IF: expected 4 parameters, got %d", 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.operator = normalizeOperator(params[2])
|
||||||
scope := ctx.CurrentScope()
|
scope := ctx.CurrentScope()
|
||||||
|
|
||||||
// Create constant lookup function
|
|
||||||
constLookup := func(name string) (int64, bool) {
|
constLookup := func(name string) (int64, bool) {
|
||||||
sym := ctx.SymbolTable.Lookup(name, scope)
|
sym := ctx.SymbolTable.Lookup(name, scope)
|
||||||
if sym != nil && sym.IsConst() {
|
if sym != nil && sym.IsConst() {
|
||||||
|
|
@ -89,541 +50,65 @@ func (c *IfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse param1
|
// Parse param1
|
||||||
c.param1VarName, c.param1VarKind, c.param1Value, c.param1IsVar, err = compiler.ParseOperandParam(
|
varName, varKind, value, isVar, err := compiler.ParseOperandParam(
|
||||||
params[1], ctx.SymbolTable, scope, constLookup)
|
params[1], ctx.SymbolTable, scope, constLookup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("IF: param1: %w", err)
|
return fmt.Errorf("IF: param1: %w", err)
|
||||||
}
|
}
|
||||||
|
c.param1 = &operandInfo{
|
||||||
|
varName: varName,
|
||||||
|
varKind: varKind,
|
||||||
|
value: value,
|
||||||
|
isVar: isVar,
|
||||||
|
}
|
||||||
|
|
||||||
// Parse param2
|
// Parse param2
|
||||||
c.param2VarName, c.param2VarKind, c.param2Value, c.param2IsVar, err = compiler.ParseOperandParam(
|
varName, varKind, value, isVar, err = compiler.ParseOperandParam(
|
||||||
params[3], ctx.SymbolTable, scope, constLookup)
|
params[3], ctx.SymbolTable, scope, constLookup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("IF: param2: %w", err)
|
return fmt.Errorf("IF: param2: %w", err)
|
||||||
}
|
}
|
||||||
|
c.param2 = &operandInfo{
|
||||||
|
varName: varName,
|
||||||
|
varKind: varKind,
|
||||||
|
value: value,
|
||||||
|
isVar: isVar,
|
||||||
|
}
|
||||||
|
|
||||||
// Check pragma for long jump
|
// Check pragma
|
||||||
ps := ctx.Pragma.GetPragmaSetByIndex(line.PragmaSetIndex)
|
ps := ctx.Pragma.GetPragmaSetByIndex(line.PragmaSetIndex)
|
||||||
longJumpPragma := ps.GetPragma("_P_USE_LONG_JUMP")
|
longJumpPragma := ps.GetPragma("_P_USE_LONG_JUMP")
|
||||||
c.useLongJump = longJumpPragma != "" && longJumpPragma != "0"
|
c.useLongJump = longJumpPragma != "" && longJumpPragma != "0"
|
||||||
|
|
||||||
// Push skip label onto IF stack
|
// Create skip label (jumps here on FALSE, or to ELSE if present)
|
||||||
c.skipLabel = ctx.IfStack.Push()
|
c.skipLabel = ctx.IfStack.Push()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *IfCommand) Generate(ctx *compiler.CompilerContext) ([]string, error) {
|
func (c *IfCommand) Generate(ctx *compiler.CompilerContext) ([]string, error) {
|
||||||
switch c.operator {
|
op, err := parseOperator(c.operator)
|
||||||
case "=":
|
if err != nil {
|
||||||
return c.generateEqual(ctx)
|
return nil, fmt.Errorf("IF: %w", err)
|
||||||
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
|
// Generate comparison (jumps to skipLabel on FALSE)
|
||||||
if byteIsVar {
|
gen, err := newComparisonGenerator(
|
||||||
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
|
op,
|
||||||
} else {
|
c.param1,
|
||||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
|
c.param2,
|
||||||
|
c.useLongJump,
|
||||||
|
ctx.IfStack,
|
||||||
|
ctx.GeneralStack,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("IF: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if wordIsVar {
|
cmpAsm, err := gen.generate()
|
||||||
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
|
if err != nil {
|
||||||
} else {
|
return nil, fmt.Errorf("IF: %w", err)
|
||||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
return cmpAsm, nil
|
||||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
|
||||||
asm = append(asm, successLabel)
|
|
||||||
return asm, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -115,8 +115,8 @@ func TestWhileAllOperators(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{"equal", "WHILE x = 10", "bne"},
|
{"equal", "WHILE x = 10", "bne"},
|
||||||
{"not equal", "WHILE x <> 10", "beq"},
|
{"not equal", "WHILE x <> 10", "beq"},
|
||||||
{"greater", "WHILE x > 10", "beq"},
|
{"greater", "WHILE x > 10", "bcs"},
|
||||||
{"less", "WHILE x < 10", "beq"},
|
{"less", "WHILE x < 10", "bcs"},
|
||||||
{"greater equal", "WHILE x >= 10", "bcc"},
|
{"greater equal", "WHILE x >= 10", "bcc"},
|
||||||
{"less equal", "WHILE x <= 10", "bcc"},
|
{"less equal", "WHILE x <= 10", "bcc"},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue