Appearance
| 1 | namespace IR.Values is | |
| 2 | use Semantic.Types.Type | |
| 3 | ||
| 4 | use Ghul.Pipes | |
| 5 | ||
| 6 | // Decimal is a built-in type the runtime implements rather than a | |
| 7 | // scalar a target operates on directly, so its operators are values | |
| 8 | // of their own, which a backend lowers to its runtime's decimal | |
| 9 | // support. | |
| 10 | ||
| 11 | // A built-in arithmetic operator applied to one decimal operand or | |
| 12 | // two. | |
| 13 | class DECIMAL_ARITHMETIC: Value is | |
| 14 | operation: IR.ArithmeticOperation | |
| 15 | operands: Collections.List[Value] | |
| 16 | type: Type | |
| 17 | ||
| 18 | // Marked before the operator rather than before the operands, | |
| 19 | // which mark their own positions. | |
| 20 | has_location: bool => false | |
| 21 | ||
| 22 | init(operation: IR.ArithmeticOperation, operands: Collections.List[Value], type: Type) is | |
| 23 | super.init() | |
| 24 | ||
| 25 | assert operands.count == 1 \/ operands.count == 2 else "decimal arithmetic takes one operand or two" | |
| 26 | ||
| 27 | self.operation = operation | |
| 28 | self.operands = operands | |
| 29 | self.type = type | |
| 30 | si | |
| 31 | ||
| 32 | accept(visitor: Visitor) is | |
| 33 | visitor.visit(self) | |
| 34 | si | |
| 35 | ||
| 36 | to_string() -> string => "decimal-arithmetic:{operation}({operands |> join(", ")})" | |
| 37 | si | |
| 38 | ||
| 39 | // A built-in comparison of two decimals, answering a bool. | |
| 40 | class DECIMAL_COMPARE: Value is | |
| 41 | comparison: IR.Comparison | |
| 42 | left: Value | |
| 43 | right: Value | |
| 44 | type: Type | |
| 45 | ||
| 46 | // Marked before the comparison rather than before the operands, | |
| 47 | // which mark their own positions. | |
| 48 | has_location: bool => false | |
| 49 | ||
| 50 | init(comparison: IR.Comparison, left: Value, right: Value, type: Type) is | |
| 51 | super.init() | |
| 52 | ||
| 53 | self.comparison = comparison | |
| 54 | self.left = left | |
| 55 | self.right = right | |
| 56 | self.type = type | |
| 57 | si | |
| 58 | ||
| 59 | accept(visitor: Visitor) is | |
| 60 | visitor.visit(self) | |
| 61 | si | |
| 62 | ||
| 63 | to_string() -> string => "decimal-compare:{comparison}({left}, {right})" | |
| 64 | si | |
| 65 | ||
| 66 | // A conversion between decimal and another numeric type. | |
| 67 | class DECIMAL_CONVERT: Value is | |
| 68 | type: Type | |
| 69 | value: Value | |
| 70 | ||
| 71 | // The type converted from, as the conversion was chosen for. | |
| 72 | from: Type | |
| 73 | ||
| 74 | init(type: Type, value: Value, from: Type) is | |
| 75 | super.init() | |
| 76 | ||
| 77 | self.type = type | |
| 78 | self.value = value | |
| 79 | self.from = from | |
| 80 | si | |
| 81 | ||
| 82 | accept(visitor: Visitor) is | |
| 83 | visitor.visit(self) | |
| 84 | si | |
| 85 | ||
| 86 | to_string() -> string => | |
| 87 | "decimal-convert:[{type}]({value})" | |
| 88 | si | |
| 89 | si |