mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-18 23:13:29 +00:00
fixed equal and not equal
This commit is contained in:
parent
8e684b800c
commit
9fe5f322a1
@ -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
|
||||
|
@ -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,6 +95,15 @@ func expectFunction(t *testing.T, expected Function, actual Function) {
|
||||
|
||||
func expectInstruction(t *testing.T, expected Instruction, actual Instruction) {
|
||||
t.Helper()
|
||||
|
||||
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)
|
||||
}
|
||||
@ -106,6 +115,19 @@ func expectInstruction(t *testing.T, expected Instruction, actual Instruction) {
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ const (
|
||||
PrecLowest precedence = iota
|
||||
PrecSum
|
||||
PrecProduct
|
||||
PrecComparison
|
||||
)
|
||||
|
||||
var precedences = map[token.TokenType]precedence{
|
||||
@ -22,6 +23,8 @@ var precedences = map[token.TokenType]precedence{
|
||||
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
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package ttir
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"robaertschi.xyz/robaertschi/tt/ast"
|
||||
"robaertschi.xyz/robaertschi/tt/tast"
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user