mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-18 23:13:29 +00:00
fix unit return for if expression
This commit is contained in:
parent
9e6e3bd1e4
commit
90bb478537
@ -158,14 +158,14 @@ func (ie *IfExpression) TokenLiteral() string { return ie.Token.Literal }
|
|||||||
func (ie *IfExpression) String() string {
|
func (ie *IfExpression) String() string {
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
|
|
||||||
builder.WriteString("(if\n\t")
|
builder.WriteString(fmt.Sprintf("(if %s\n\t", ie.Condition.String()))
|
||||||
builder.WriteString(ie.Then.String())
|
builder.WriteString(ie.Then.String())
|
||||||
|
|
||||||
if ie.Else != nil {
|
if ie.Else != nil {
|
||||||
builder.WriteString(" else in ")
|
builder.WriteString(" else in ")
|
||||||
builder.WriteString(ie.Else.String())
|
builder.WriteString(ie.Else.String())
|
||||||
}
|
}
|
||||||
builder.WriteString(")")
|
builder.WriteString(fmt.Sprintf(") :> %s", ie.Type().Name()))
|
||||||
|
|
||||||
return builder.String()
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
4
test.tt
4
test.tt
@ -1,5 +1,5 @@
|
|||||||
fn main() = {
|
fn main() = {
|
||||||
if 3 == 3
|
if 3 == 3
|
||||||
in 4
|
{ if 4 == 4 in 3 }
|
||||||
else 3
|
else {}
|
||||||
};
|
};
|
||||||
|
@ -98,21 +98,27 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
|||||||
// } endOfIf:
|
// } endOfIf:
|
||||||
elseLabel := tempLabel()
|
elseLabel := tempLabel()
|
||||||
endOfIfLabel := tempLabel()
|
endOfIfLabel := tempLabel()
|
||||||
dst := &Var{Value: temp()}
|
var dst Operand = &Var{Value: temp()}
|
||||||
|
|
||||||
condDst, instructions := emitExpression(expr.Condition)
|
condDst, instructions := emitExpression(expr.Condition)
|
||||||
|
|
||||||
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) {
|
||||||
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}, Jump(endOfIfLabel))
|
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}, Jump(endOfIfLabel))
|
||||||
|
} else {
|
||||||
|
dst = nil
|
||||||
|
}
|
||||||
|
|
||||||
instructions = append(instructions, Label(elseLabel))
|
instructions = append(instructions, Label(elseLabel))
|
||||||
if expr.Else != nil {
|
if expr.Else != nil {
|
||||||
elseDst, elseInstructions := emitExpression(expr.Else)
|
elseDst, elseInstructions := emitExpression(expr.Else)
|
||||||
instructions = append(instructions, elseInstructions...)
|
instructions = append(instructions, elseInstructions...)
|
||||||
|
if !expr.ReturnType.IsSameType(types.Unit) {
|
||||||
instructions = append(instructions, &Copy{Src: elseDst, Dst: dst})
|
instructions = append(instructions, &Copy{Src: elseDst, Dst: dst})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
instructions = append(instructions, Label(endOfIfLabel))
|
instructions = append(instructions, Label(endOfIfLabel))
|
||||||
return dst, instructions
|
return dst, instructions
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user