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)}
|
||||
case *ttir.Copy:
|
||||
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 {
|
||||
|
5
test.tt
5
test.tt
@ -1,8 +1,9 @@
|
||||
fn main() = {
|
||||
hi := 4;
|
||||
|
||||
if hi == 2 in hi = 3
|
||||
else hi = 2;
|
||||
if hi == 4 in
|
||||
hi = 0
|
||||
else hi = 1;
|
||||
|
||||
hi
|
||||
};
|
||||
|
18
ttir/emit.go
18
ttir/emit.go
@ -105,8 +105,11 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
||||
instructions = append(instructions, &JumpIfZero{Value: condDst, Label: elseLabel})
|
||||
thenDst, thenInstructions := emitExpression(expr.Then)
|
||||
instructions = append(instructions, thenInstructions...)
|
||||
if expr.Else != nil {
|
||||
if !expr.ReturnType.IsSameType(types.Unit) {
|
||||
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}, Jump(endOfIfLabel))
|
||||
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst})
|
||||
}
|
||||
instructions = append(instructions, Jump(endOfIfLabel))
|
||||
} else {
|
||||
dst = nil
|
||||
}
|
||||
@ -122,8 +125,21 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
||||
instructions = append(instructions, Label(endOfIfLabel))
|
||||
return dst, instructions
|
||||
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:
|
||||
rhsDst, instructions := emitExpression(expr.InitializingExpression)
|
||||
|
||||
instructions = append(instructions, &Copy{Src: rhsDst, Dst: &Var{Value: expr.Identifier}})
|
||||
|
||||
return nil, instructions
|
||||
case *tast.VariableReference:
|
||||
return &Var{Value: expr.Identifier}, []Instruction{}
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpected tast.Expression: %#v", expr))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user