Appearance
| 1 | namespace IR.Values is | |
| 2 | use Ghul.Pipes | |
| 3 | ||
| 4 | use TypeTyped = Semantic.Types.Typed | |
| 5 | use Semantic.Types.Type | |
| 6 | ||
| 7 | class BLOCK: Value, TypeTyped is | |
| 8 | _values: Collections.MutableList[Value] | |
| 9 | ||
| 10 | type: Type | |
| 11 | is_block: bool => true | |
| 12 | ||
| 13 | is_closed: bool | |
| 14 | is_emitted: bool public | |
| 15 | ||
| 16 | is_consumable: bool => true | |
| 17 | ||
| 18 | init(type: Type) is | |
| 19 | super.init() | |
| 20 | ||
| 21 | _values = Collections.LIST[Value]() | |
| 22 | ||
| 23 | self.type = type | |
| 24 | si | |
| 25 | ||
| 26 | init() is | |
| 27 | super.init() | |
| 28 | ||
| 29 | _values = Collections.LIST[Value]() | |
| 30 | ||
| 31 | self.type = Semantic.Types.ERROR() | |
| 32 | si | |
| 33 | ||
| 34 | close() is | |
| 35 | assert !self.is_emitted else "oops: block value already emitted" | |
| 36 | assert !self.is_closed else "oops: block is already closed" | |
| 37 | ||
| 38 | self.is_closed = true | |
| 39 | si | |
| 40 | ||
| 41 | // While the body of a structured node in this block is open, | |
| 42 | // values added here are added to that body instead. Code that | |
| 43 | // holds a block from before the node opened, such as the block | |
| 44 | // a TEMP was declared in, still appends at the current position. | |
| 45 | _open_body: BLOCK? | |
| 46 | ||
| 47 | add(value: Value) is | |
| 48 | if _open_body? then | |
| 49 | _open_body.add(value) | |
| 50 | return | |
| 51 | fi | |
| 52 | ||
| 53 | assert !self.is_emitted else "oops: block value already emitted" | |
| 54 | assert !self.is_closed else "oops: block is closed, can't add more values" | |
| 55 | ||
| 56 | _values.add(value) | |
| 57 | si | |
| 58 | ||
| 59 | open_body(body: BLOCK) is | |
| 60 | assert !_open_body? else "a structured node's body is already open in this block" | |
| 61 | ||
| 62 | _open_body = body | |
| 63 | si | |
| 64 | ||
| 65 | close_body(body: BLOCK) is | |
| 66 | assert _open_body == body else "closing a structured node's body that is not open in this block" | |
| 67 | ||
| 68 | _open_body = null | |
| 69 | si | |
| 70 | ||
| 71 | values: Collections.MutableList[Value] => _values | |
| 72 | ||
| 73 | // The value the block leaves behind when consumed as an | |
| 74 | // expression: the earlier entries run for effect only. | |
| 75 | result_value: Value? => | |
| 76 | if _values.count > 0 then | |
| 77 | _values[_values.count - 1] | |
| 78 | else | |
| 79 | null | |
| 80 | fi | |
| 81 | ||
| 82 | accept(visitor: Visitor) is | |
| 83 | visitor.visit(self) | |
| 84 | si | |
| 85 | ||
| 86 | to_string() -> string => | |
| 87 | "block:[{type}]({_values |> join("; ")})" | |
| 88 | si | |
| 89 | ||
| 90 | class DUMMY_BLOCK: BLOCK is | |
| 91 | location: Source.LOCATION | |
| 92 | why: string | |
| 93 | ||
| 94 | init(type: Type, location: Source.LOCATION, why: string) is | |
| 95 | super.init(type) | |
| 96 | ||
| 97 | self.location = location | |
| 98 | self.why = why | |
| 99 | si | |
| 100 | ||
| 101 | accept(visitor: Visitor) is | |
| 102 | visitor.visit(self) | |
| 103 | si | |
| 104 | si | |
| 105 | si |