fixed equal and not equal

This commit is contained in:
Robin 2025-01-23 22:24:00 +01:00
parent 8e684b800c
commit 9fe5f322a1
6 changed files with 48 additions and 23 deletions

View File

@ -113,7 +113,7 @@ type SetCCInstruction struct {
} }
func (si *SetCCInstruction) InstructionString() string { func (si *SetCCInstruction) InstructionString() string {
return fmt.Sprintf("set%s %s", si.Cond, si.Dst.OperandString(Eight)) return fmt.Sprintf("set%s %s", si.Cond, si.Dst.OperandString(One))
} }
type OperandSize int type OperandSize int

View File

@ -42,12 +42,12 @@ func TestCodegen(t *testing.T) {
{ {
Name: "main", Name: "main",
Instructions: []Instruction{ Instructions: []Instruction{
{ &SimpleInstruction{
Opcode: Mov, Opcode: Mov,
Lhs: AX, Lhs: AX,
Rhs: Imm(0), Rhs: Imm(0),
}, },
{ &SimpleInstruction{
Opcode: Ret, Opcode: Ret,
}, },
}, },
@ -95,16 +95,38 @@ func expectFunction(t *testing.T, expected Function, actual Function) {
func expectInstruction(t *testing.T, expected Instruction, actual Instruction) { func expectInstruction(t *testing.T, expected Instruction, actual Instruction) {
t.Helper() t.Helper()
if expected.Opcode != actual.Opcode {
t.Errorf("Expected opcode %q but got %q", expected.Opcode, actual.Opcode)
}
switch expected.Opcode { switch expected := expected.(type) {
case Mov: case *SimpleInstruction:
expectOperand(t, expected.Lhs, actual.Lhs) actual, ok := actual.(*SimpleInstruction)
expectOperand(t, expected.Rhs, actual.Rhs) if !ok {
case Ret: t.Errorf("Expected SimpleInstruction but got %T", actual)
// nothing to do return
}
if expected.Opcode != actual.Opcode {
t.Errorf("Expected opcode %q but got %q", expected.Opcode, actual.Opcode)
}
switch expected.Opcode {
case Mov:
expectOperand(t, expected.Lhs, actual.Lhs)
expectOperand(t, expected.Rhs, actual.Rhs)
case Ret:
// nothing to do
}
case *SetCCInstruction:
actual, ok := actual.(*SetCCInstruction)
if !ok {
t.Errorf("Expected SetCCInstruction but got %T", actual)
return
}
if expected.Cond != actual.Cond {
t.Errorf("Expected condition %q but got %q", expected.Cond, actual.Cond)
}
expectOperand(t, expected.Dst, actual.Dst)
} }
} }

View File

@ -257,17 +257,17 @@ func fixupInstruction(i Instruction) []Instruction {
&SimpleInstruction{Opcode: i.Opcode, Lhs: lhs, Rhs: Register(R10)}, &SimpleInstruction{Opcode: i.Opcode, Lhs: lhs, Rhs: Register(R10)},
} }
} }
} else if rhs, ok := i.Rhs.(Imm); ok { } else if lhs, ok := i.Lhs.(Imm); ok {
return []Instruction{ return []Instruction{
&SimpleInstruction{ &SimpleInstruction{
Opcode: Mov, Opcode: Mov,
Lhs: Register(R11), Lhs: Register(R11),
Rhs: Imm(rhs), Rhs: Imm(lhs),
}, },
&SimpleInstruction{ &SimpleInstruction{
Opcode: Cmp, Opcode: Cmp,
Lhs: i.Lhs, Lhs: Register(R11),
Rhs: Register(R11), Rhs: i.Lhs,
}, },
} }
} }

View File

@ -15,13 +15,16 @@ const (
PrecLowest precedence = iota PrecLowest precedence = iota
PrecSum PrecSum
PrecProduct PrecProduct
PrecComparison
) )
var precedences = map[token.TokenType]precedence{ var precedences = map[token.TokenType]precedence{
token.Plus: PrecSum, token.Plus: PrecSum,
token.Minus: PrecSum, token.Minus: PrecSum,
token.Asterisk: PrecProduct, token.Asterisk: PrecProduct,
token.Slash: PrecProduct, token.Slash: PrecProduct,
token.DoubleEqual: PrecComparison,
token.NotEqual: PrecComparison,
} }
type ErrorCallback func(token.Token, string, ...any) type ErrorCallback func(token.Token, string, ...any)
@ -51,7 +54,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfixFn(token.Minus, p.parseBinaryExpression) p.registerInfixFn(token.Minus, p.parseBinaryExpression)
p.registerInfixFn(token.Asterisk, p.parseBinaryExpression) p.registerInfixFn(token.Asterisk, p.parseBinaryExpression)
p.registerInfixFn(token.Slash, p.parseBinaryExpression) p.registerInfixFn(token.Slash, p.parseBinaryExpression)
p.registerInfixFn(token.Equal, p.parseBinaryExpression) p.registerInfixFn(token.DoubleEqual, p.parseBinaryExpression)
p.registerInfixFn(token.NotEqual, p.parseBinaryExpression) p.registerInfixFn(token.NotEqual, p.parseBinaryExpression)
p.nextToken() p.nextToken()
@ -200,6 +203,7 @@ func (p *Parser) parseExpression(precedence precedence) ast.Expression {
for !p.peekTokenIs(token.Semicolon) && precedence < p.peekPrecedence() { for !p.peekTokenIs(token.Semicolon) && precedence < p.peekPrecedence() {
infix := p.infixParseFns[p.peekToken.Type] infix := p.infixParseFns[p.peekToken.Type]
if infix == nil { if infix == nil {
return leftExpr return leftExpr
} }

View File

@ -1 +1 @@
fn main() = 3 * 3 / 3 + 1 - 1; fn main() = 3 != 3;

View File

@ -3,7 +3,6 @@ package ttir
import ( import (
"fmt" "fmt"
"robaertschi.xyz/robaertschi/tt/ast"
"robaertschi.xyz/robaertschi/tt/tast" "robaertschi.xyz/robaertschi/tt/tast"
) )