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

View File

@ -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)
}
}

View File

@ -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,
},
}
}

View File

@ -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
}

View File

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

View File

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