Appearance
| 1 | namespace Syntax.Trees.Expressions is | |
| 2 | use Source | |
| 3 | ||
| 4 | // `[element for p in source if condition ...]`, an array, or the same | |
| 5 | // clauses in braces, `{element for ...}`, a pipe whose elements are | |
| 6 | // produced as it is read. The clauses form a | |
| 7 | // chain rather than a list: each clause holds the one after it, and | |
| 8 | // the last holds the element, so a `for` clause's scope covers every | |
| 9 | // later clause and the element, and a walk reaches the element inside | |
| 10 | // every clause scope. | |
| 11 | class COMPREHENSION: Expression, ScopeCarrier is | |
| 12 | element: Expression | |
| 13 | first: COMPREHENSION_FOR | |
| 14 | type_expression: TypeExpressions.TypeExpression | |
| 15 | ||
| 16 | // Written in braces: the result is a Pipe[T] produced by a | |
| 17 | // generator literal the comprehension owns, whose scope this is. | |
| 18 | is_lazy: bool | |
| 19 | scope: Semantic.Scope? public | |
| 20 | ||
| 21 | description: string => | |
| 22 | if is_lazy then | |
| 23 | "comprehension" | |
| 24 | else | |
| 25 | "list comprehension" | |
| 26 | fi | |
| 27 | ||
| 28 | // Compile-expressions output: the element type of the result, and | |
| 29 | // the list the elements are gathered into before the array is | |
| 30 | // made from it. | |
| 31 | element_type: Semantic.Types.Type? public | |
| 32 | list_type: Semantic.Types.Type? public | |
| 33 | list_constructor: Semantic.Symbols.Function? public | |
| 34 | list_add: Semantic.Symbols.Function? public | |
| 35 | list_to_array: Semantic.Symbols.Function? public | |
| 36 | ||
| 37 | // Set where the result can be allocated at its final length up | |
| 38 | // front: a single `for` clause, no `if`, and a source whose count | |
| 39 | // is known before the loop. The source is evaluated once into the | |
| 40 | // local here, and the count is read from that local. | |
| 41 | presize_count: IR.Values.Value? public | |
| 42 | presize_source: IR.LOCAL? public | |
| 43 | ||
| 44 | clear() is | |
| 45 | super.clear() | |
| 46 | _clear_comprehension_outputs() | |
| 47 | si | |
| 48 | ||
| 49 | clear_outputs() is | |
| 50 | super.clear_outputs() | |
| 51 | _clear_comprehension_outputs() | |
| 52 | si | |
| 53 | ||
| 54 | _clear_comprehension_outputs() is | |
| 55 | element_type = null | |
| 56 | list_type = null | |
| 57 | list_constructor = null | |
| 58 | list_add = null | |
| 59 | list_to_array = null | |
| 60 | presize_count = null | |
| 61 | presize_source = null | |
| 62 | si | |
| 63 | ||
| 64 | init(location: LOCATION, element: Expression, first: COMPREHENSION_FOR, type_expression: TypeExpressions.TypeExpression, is_lazy: bool) is | |
| 65 | super.init(location) | |
| 66 | ||
| 67 | self.is_lazy = is_lazy | |
| 68 | ||
| 69 | self.element = element | |
| 70 | self.first = first | |
| 71 | self.type_expression = type_expression | |
| 72 | ||
| 73 | let last: COMPREHENSION_CLAUSE mut = first | |
| 74 | ||
| 75 | while let next = last.inner do | |
| 76 | last = next | |
| 77 | od | |
| 78 | ||
| 79 | last.element = element | |
| 80 | si | |
| 81 | ||
| 82 | set_expected_type(expected_type: Semantic.Types.Type?, error_message: string?) is | |
| 83 | compile_expressions_state.set_expected_type(expected_type, error_message) | |
| 84 | si | |
| 85 | ||
| 86 | accept(visitor: Visitor) is | |
| 87 | visitor.visit(self) | |
| 88 | si | |
| 89 | ||
| 90 | walk(visitor: Visitor) is | |
| 91 | if !visitor.pre(self) then | |
| 92 | first.walk(visitor) | |
| 93 | type_expression.walk(visitor) | |
| 94 | fi | |
| 95 | ||
| 96 | accept(visitor) | |
| 97 | si | |
| 98 | si | |
| 99 | ||
| 100 | // A clause of a comprehension. `inner` is the clause after this one; | |
| 101 | // the last clause has none, and holds the comprehension's element | |
| 102 | // instead. | |
| 103 | class COMPREHENSION_CLAUSE: Trees.Node abstract is | |
| 104 | inner: COMPREHENSION_CLAUSE? public | |
| 105 | element: Expression? public | |
| 106 | ||
| 107 | init(location: LOCATION) is | |
| 108 | super.init(location) | |
| 109 | si | |
| 110 | ||
| 111 | walk_inner(visitor: Visitor) is | |
| 112 | if let inner = self.inner then | |
| 113 | inner.walk(visitor) | |
| 114 | elif let element = self.element then | |
| 115 | element.walk(visitor) | |
| 116 | fi | |
| 117 | si | |
| 118 | si | |
| 119 | ||
| 120 | // `for pattern in source`. Iterates the same way a `for` statement | |
| 121 | // does, so it carries the same iterator state. | |
| 122 | class COMPREHENSION_FOR: COMPREHENSION_CLAUSE, ScopeCarrier, IteratorCarrier is | |
| 123 | scope: Semantic.Scope? public | |
| 124 | ||
| 125 | variable: Variables.VARIABLE | |
| 126 | source: Expression | |
| 127 | ||
| 128 | iterated: Expression? => source | |
| 129 | ||
| 130 | read_iterator: Semantic.Symbols.Function? public => iterator_state.read_iterator, = value is iterator_state.read_iterator = value si | |
| 131 | read_current: Semantic.Symbols.Function? public => iterator_state.read_current, = value is iterator_state.read_current = value si | |
| 132 | move_next: Semantic.Symbols.Function? public => iterator_state.move_next, = value is iterator_state.move_next = value si | |
| 133 | ||
| 134 | iterator_field: Semantic.Symbols.Field? public => iterator_state.iterator_field, = value is iterator_state.iterator_field = value si | |
| 135 | ||
| 136 | fusion: Syntax.Process.PIPE_FUSION? public => iterator_state.fusion, = value is iterator_state.fusion = value si | |
| 137 | ||
| 138 | iterator_state: Syntax.Process.ITERATOR_STATE field | |
| 139 | ||
| 140 | init(location: LOCATION, variable: Variables.VARIABLE, source: Expression) is | |
| 141 | super.init(location) | |
| 142 | ||
| 143 | self.variable = variable | |
| 144 | self.source = source | |
| 145 | si | |
| 146 | ||
| 147 | clear() is | |
| 148 | iterator_state.clear() | |
| 149 | si | |
| 150 | ||
| 151 | clear_outputs() is | |
| 152 | iterator_state.clear_outputs() | |
| 153 | si | |
| 154 | ||
| 155 | accept(visitor: Visitor) is | |
| 156 | visitor.visit(self) | |
| 157 | si | |
| 158 | ||
| 159 | walk(visitor: Visitor) is | |
| 160 | if !visitor.pre(self) then | |
| 161 | variable.walk(visitor) | |
| 162 | source.walk(visitor) | |
| 163 | walk_inner(visitor) | |
| 164 | fi | |
| 165 | ||
| 166 | accept(visitor) | |
| 167 | si | |
| 168 | si | |
| 169 | ||
| 170 | // `if condition`: later clauses and the element see only the | |
| 171 | // elements for which it holds. | |
| 172 | class COMPREHENSION_IF: COMPREHENSION_CLAUSE is | |
| 173 | condition: Expression | |
| 174 | ||
| 175 | init(location: LOCATION, condition: Expression) is | |
| 176 | super.init(location) | |
| 177 | ||
| 178 | self.condition = condition | |
| 179 | si | |
| 180 | ||
| 181 | accept(visitor: Visitor) is | |
| 182 | visitor.visit(self) | |
| 183 | si | |
| 184 | ||
| 185 | walk(visitor: Visitor) is | |
| 186 | if !visitor.pre(self) then | |
| 187 | condition.walk(visitor) | |
| 188 | walk_inner(visitor) | |
| 189 | fi | |
| 190 | ||
| 191 | accept(visitor) | |
| 192 | si | |
| 193 | si | |
| 194 | si |