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 | |
| 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 | add(value: Value) is | |
| 42 | assert !self.is_emitted else "oops: block value already emitted" | |
| 43 | assert !self.is_closed else "oops: block is closed, can't add more values" | |
| 44 | ||
| 45 | _values.add(value) | |
| 46 | si | |
| 47 | ||
| 48 | values: Collections.MutableList[Value] => _values | |
| 49 | ||
| 50 | // The value the block leaves behind when consumed as an | |
| 51 | // expression: the earlier entries run for effect only. | |
| 52 | result_value: Value? => | |
| 53 | if _values.count > 0 then | |
| 54 | _values[_values.count - 1] | |
| 55 | else | |
| 56 | null | |
| 57 | fi | |
| 58 | ||
| 59 | gen(context: IR.CONTEXT) is | |
| 60 | assert !self.is_emitted else "oops: block value already emitted" | |
| 61 | ||
| 62 | // TODO close blocks when we've finished with them | |
| 63 | // as a sanity check | |
| 64 | // assert self.is_closed else "oops: block is not closed"; | |
| 65 | is_emitted = true | |
| 66 | ||
| 67 | ||
| 68 | for value in _values do | |
| 69 | if value.has_location then | |
| 70 | context.mark_location(value.location) | |
| 71 | fi | |
| 72 | value.gen(context) | |
| 73 | od | |
| 74 | ||
| 75 | si | |
| 76 | ||
| 77 | to_string() -> string => | |
| 78 | "block:[{type}]({_values |> join("; ")})" | |
| 79 | si | |
| 80 | ||
| 81 | class DUMMY_BLOCK: BLOCK is | |
| 82 | location: Source.LOCATION | |
| 83 | why: string | |
| 84 | ||
| 85 | init(type: Type, location: Source.LOCATION, why: string) is | |
| 86 | super.init(type) | |
| 87 | ||
| 88 | self.location = location | |
| 89 | self.why = why | |
| 90 | si | |
| 91 | ||
| 92 | gen(context: IR.CONTEXT) => throw System.InvalidOperationException("oops: dummy block generated, created at: {location} because: {why}") | |
| 93 | si | |
| 94 | si |