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