Cast returns an expression that casts the input to the desired type. The returned expression AST will be used directly for VM instruction generation of the desired types.
(e Expr, t Type)
| 634 | // The returned expression AST will be used directly for VM instruction |
| 635 | // generation of the desired types. |
| 636 | func Cast(e Expr, t Type) Expr { |
| 637 | // Input type is already desired. |
| 638 | if e.Type() == t { |
| 639 | return e |
| 640 | } |
| 641 | // Type casting is only required if at least one side is float. |
| 642 | // We do not cast (or check for overflow) among boolean, signed and unsigned. |
| 643 | if e.Type() != Float && t != Float { |
| 644 | return e |
| 645 | } |
| 646 | // Data type for NumberLiteral can be changed directly. |
| 647 | l, _ := e.(*NumberLiteral) |
| 648 | if l != nil { |
| 649 | l.ExprType = t |
| 650 | return l |
| 651 | } |
| 652 | // Use ParenExpr to respresent a VM type cast. |
| 653 | return &ParenExpr{Expr: e, ExprType: t} |
| 654 | } |