Skip to content
← Back

src/ir/values/arithmetic.ghul

1
namespace IR.Values is
2
use Semantic.Types.Type
3
4
use Ghul.Pipes
5
6
// A built-in arithmetic or bitwise operator applied to one operand
7
// or two.
8
class ARITHMETIC: Value is
9
operation: IR.ArithmeticOperation
10
operands: Collections.List[Value]
11
12
type: Type
13
14
// Marked before the operator rather than before the operands,
15
// which mark their own positions, unless unmarked.
16
has_location: bool => !is_marked /\ super.has_location
17
18
is_marked: bool
19
20
init(operation: IR.ArithmeticOperation, operands: Collections.List[Value], type: Type) is
21
super.init()
22
23
assert operands.count == 1 \/ operands.count == 2 else "arithmetic takes one operand or two"
24
25
self.operation = operation
26
self.operands = operands
27
self.type = type
28
29
is_marked = true
30
si
31
32
init(operation: IR.ArithmeticOperation, left: Value, right: Value, type: Type) is
33
self.init(operation, [left, right]:Value, type)
34
si
35
36
// Marks no source positions inside it: whatever holds it marks
37
// its position as a whole, if anything does.
38
unmarked() -> ARITHMETIC is
39
is_marked = false
40
41
return self
42
si
43
44
accept(visitor: Visitor) is
45
visitor.visit(self)
46
si
47
48
to_string() -> string => "arithmetic:{operation}({operands |> join(", ")})"
49
si
50
51
// A built-in comparison of two values, answering a bool. An
52
// unsigned comparison orders integers as unsigned, and floating
53
// values with unordered operands answering true.
54
class COMPARE: Value is
55
comparison: IR.Comparison
56
left: Value
57
right: Value
58
is_unsigned: bool
59
60
type: Type
61
62
// Marked before the comparison rather than before the
63
// operands, which mark their own positions.
64
has_location: bool => false
65
66
init(comparison: IR.Comparison, left: Value, right: Value, is_unsigned: bool, type: Type) is
67
super.init()
68
69
self.comparison = comparison
70
self.left = left
71
self.right = right
72
self.is_unsigned = is_unsigned
73
self.type = type
74
si
75
76
accept(visitor: Visitor) is
77
visitor.visit(self)
78
si
79
80
to_string() -> string => "compare:{comparison}{if is_unsigned then ".unsigned" else "" fi}({left}, {right})"
81
si
82
si