mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-16 05:53:30 +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 {
|
||||
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())
|
||||
|
||||
if ie.Else != nil {
|
||||
builder.WriteString(" else in ")
|
||||
builder.WriteString(ie.Else.String())
|
||||
}
|
||||
builder.WriteString(")")
|
||||
builder.WriteString(fmt.Sprintf(") :> %s", ie.Type().Name()))
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
4
test.tt
4
test.tt
@ -1,5 +1,5 @@
|
||||
fn main() = {
|
||||
if 3 == 3
|
||||
in 4
|
||||
else 3
|
||||
{ if 4 == 4 in 3 }
|
||||
else {}
|
||||
};
|
||||
|
@ -98,21 +98,27 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
||||
// } endOfIf:
|
||||
elseLabel := tempLabel()
|
||||
endOfIfLabel := tempLabel()
|
||||
dst := &Var{Value: temp()}
|
||||
var dst Operand = &Var{Value: temp()}
|
||||
|
||||
condDst, instructions := emitExpression(expr.Condition)
|
||||
|
||||
instructions = append(instructions, &JumpIfZero{Value: condDst, Label: elseLabel})
|
||||
thenDst, thenInstructions := emitExpression(expr.Then)
|
||||
instructions = append(instructions, thenInstructions...)
|
||||
if !expr.ReturnType.IsSameType(types.Unit) {
|
||||
instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}, Jump(endOfIfLabel))
|
||||
} else {
|
||||
dst = nil
|
||||
}
|
||||
|
||||
instructions = append(instructions, Label(elseLabel))
|
||||
if expr.Else != nil {
|
||||
elseDst, elseInstructions := emitExpression(expr.Else)
|
||||
instructions = append(instructions, elseInstructions...)
|
||||
if !expr.ReturnType.IsSameType(types.Unit) {
|
||||
instructions = append(instructions, &Copy{Src: elseDst, Dst: dst})
|
||||
}
|
||||
}
|
||||
instructions = append(instructions, Label(endOfIfLabel))
|
||||
return dst, instructions
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user