From 9fe5f322a1b4dd9ea1253282d3cd1b39182ad816 Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 23 Jan 2025 22:24:00 +0100 Subject: [PATCH] fixed equal and not equal --- asm/amd64/amd64.go | 2 +- asm/amd64/amd64_test.go | 44 ++++++++++++++++++++++++++++++----------- asm/amd64/codegen.go | 8 ++++---- parser/parser.go | 14 ++++++++----- test.tt | 2 +- ttir/emit.go | 1 - 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/asm/amd64/amd64.go b/asm/amd64/amd64.go index 3026291..7849e2c 100644 --- a/asm/amd64/amd64.go +++ b/asm/amd64/amd64.go @@ -113,7 +113,7 @@ type SetCCInstruction struct { } 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 diff --git a/asm/amd64/amd64_test.go b/asm/amd64/amd64_test.go index f556ded..6dc9771 100644 --- a/asm/amd64/amd64_test.go +++ b/asm/amd64/amd64_test.go @@ -42,12 +42,12 @@ func TestCodegen(t *testing.T) { { Name: "main", Instructions: []Instruction{ - { + &SimpleInstruction{ Opcode: Mov, Lhs: AX, Rhs: Imm(0), }, - { + &SimpleInstruction{ Opcode: Ret, }, }, @@ -95,16 +95,38 @@ func expectFunction(t *testing.T, expected Function, actual Function) { func expectInstruction(t *testing.T, expected Instruction, actual Instruction) { t.Helper() - 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 + switch expected := expected.(type) { + case *SimpleInstruction: + actual, ok := actual.(*SimpleInstruction) + if !ok { + t.Errorf("Expected SimpleInstruction but got %T", actual) + 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) } } diff --git a/asm/amd64/codegen.go b/asm/amd64/codegen.go index a9f65de..fa8e6a9 100644 --- a/asm/amd64/codegen.go +++ b/asm/amd64/codegen.go @@ -257,17 +257,17 @@ func fixupInstruction(i Instruction) []Instruction { &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{ &SimpleInstruction{ Opcode: Mov, Lhs: Register(R11), - Rhs: Imm(rhs), + Rhs: Imm(lhs), }, &SimpleInstruction{ Opcode: Cmp, - Lhs: i.Lhs, - Rhs: Register(R11), + Lhs: Register(R11), + Rhs: i.Lhs, }, } } diff --git a/parser/parser.go b/parser/parser.go index 5f72c0e..ab1bdbd 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -15,13 +15,16 @@ const ( PrecLowest precedence = iota PrecSum PrecProduct + PrecComparison ) var precedences = map[token.TokenType]precedence{ - token.Plus: PrecSum, - token.Minus: PrecSum, - token.Asterisk: PrecProduct, - token.Slash: PrecProduct, + token.Plus: PrecSum, + token.Minus: PrecSum, + token.Asterisk: PrecProduct, + token.Slash: PrecProduct, + token.DoubleEqual: PrecComparison, + token.NotEqual: PrecComparison, } 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.Asterisk, 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.nextToken() @@ -200,6 +203,7 @@ func (p *Parser) parseExpression(precedence precedence) ast.Expression { for !p.peekTokenIs(token.Semicolon) && precedence < p.peekPrecedence() { infix := p.infixParseFns[p.peekToken.Type] + if infix == nil { return leftExpr } diff --git a/test.tt b/test.tt index c033802..2f2abbe 100644 --- a/test.tt +++ b/test.tt @@ -1 +1 @@ -fn main() = 3 * 3 / 3 + 1 - 1; +fn main() = 3 != 3; diff --git a/ttir/emit.go b/ttir/emit.go index c685f68..0d36c93 100644 --- a/ttir/emit.go +++ b/ttir/emit.go @@ -3,7 +3,6 @@ package ttir import ( "fmt" - "robaertschi.xyz/robaertschi/tt/ast" "robaertschi.xyz/robaertschi/tt/tast" )