mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-16 05:53:30 +00:00
fixed if expressions
This commit is contained in:
parent
e9a1ee0bab
commit
b0b7ede252
@ -104,9 +104,10 @@ func cgInstruction(i ttir.Instruction) []Instruction {
|
|||||||
return []Instruction{JmpInstruction(i)}
|
return []Instruction{JmpInstruction(i)}
|
||||||
case *ttir.Copy:
|
case *ttir.Copy:
|
||||||
return []Instruction{&SimpleInstruction{Opcode: Mov, Lhs: toAsmOperand(i.Dst), Rhs: toAsmOperand(i.Src)}}
|
return []Instruction{&SimpleInstruction{Opcode: Mov, Lhs: toAsmOperand(i.Dst), Rhs: toAsmOperand(i.Src)}}
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("unexpected ttir.Instruction: %#v", i))
|
||||||
}
|
}
|
||||||
|
|
||||||
return []Instruction{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cgBinary(b *ttir.Binary) []Instruction {
|
func cgBinary(b *ttir.Binary) []Instruction {
|
||||||
|
5
test.tt
5
test.tt
@ -1,8 +1,9 @@
|
|||||||
fn main() = {
|
fn main() = {
|
||||||
hi := 4;
|
hi := 4;
|
||||||
|
|
||||||
if hi == 2 in hi = 3
|
if hi == 4 in
|
||||||
else hi = 2;
|
hi = 0
|
||||||
|
else hi = 1;
|
||||||
|
|
||||||
hi
|
hi
|
||||||
};
|
};
|
||||||
|
20
ttir/emit.go
20
ttir/emit.go
@ -105,8 +105,11 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
|||||||
instructions = append(instructions, &JumpIfZero{Value: condDst, Label: elseLabel})
|
instructions = append(instructions, &JumpIfZero{Value: condDst, Label: elseLabel})
|
||||||
thenDst, thenInstructions := emitExpression(expr.Then)
|
thenDst, thenInstructions := emitExpression(expr.Then)
|
||||||
instructions = append(instructions, thenInstructions...)
|
instructions = append(instructions, thenInstructions...)
|
||||||
if !expr.ReturnType.IsSameType(types.Unit) {
|
if expr.Else != nil {
|
||||||
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}, Jump(endOfIfLabel))
|
if !expr.ReturnType.IsSameType(types.Unit) {
|
||||||
|
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst})
|
||||||
|
}
|
||||||
|
instructions = append(instructions, Jump(endOfIfLabel))
|
||||||
} else {
|
} else {
|
||||||
dst = nil
|
dst = nil
|
||||||
}
|
}
|
||||||
@ -122,8 +125,21 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
|||||||
instructions = append(instructions, Label(endOfIfLabel))
|
instructions = append(instructions, Label(endOfIfLabel))
|
||||||
return dst, instructions
|
return dst, instructions
|
||||||
case *tast.AssignmentExpression:
|
case *tast.AssignmentExpression:
|
||||||
|
ident := expr.Lhs.(*tast.VariableReference)
|
||||||
|
|
||||||
|
rhsDst, instructions := emitExpression(expr.Rhs)
|
||||||
|
|
||||||
|
instructions = append(instructions, &Copy{Src: rhsDst, Dst: &Var{Value: ident.Identifier}})
|
||||||
|
|
||||||
|
return nil, instructions
|
||||||
case *tast.VariableDeclaration:
|
case *tast.VariableDeclaration:
|
||||||
|
rhsDst, instructions := emitExpression(expr.InitializingExpression)
|
||||||
|
|
||||||
|
instructions = append(instructions, &Copy{Src: rhsDst, Dst: &Var{Value: expr.Identifier}})
|
||||||
|
|
||||||
|
return nil, instructions
|
||||||
case *tast.VariableReference:
|
case *tast.VariableReference:
|
||||||
|
return &Var{Value: expr.Identifier}, []Instruction{}
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unexpected tast.Expression: %#v", expr))
|
panic(fmt.Sprintf("unexpected tast.Expression: %#v", expr))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user