Smart representation of the result of a hy->AST compilation This object tries to reconcile the hy world, where everything can be used as an expression, with the Python world, where statements and expressions need to coexist. To do so, we represent a compiler result as a list o
| 127 | |
| 128 | |
| 129 | class Result: |
| 130 | """ |
| 131 | Smart representation of the result of a hy->AST compilation |
| 132 | |
| 133 | This object tries to reconcile the hy world, where everything can be used |
| 134 | as an expression, with the Python world, where statements and expressions |
| 135 | need to coexist. |
| 136 | |
| 137 | To do so, we represent a compiler result as a list of statements `stmts`, |
| 138 | terminated by an expression context `expr`. The expression context is used |
| 139 | when the compiler needs to use the result as an expression. |
| 140 | |
| 141 | Results are chained by addition: adding two results together returns a |
| 142 | Result representing the succession of the two Results' statements, with |
| 143 | the second Result's expression context. |
| 144 | |
| 145 | We make sure that a non-empty expression context does not get clobbered by |
| 146 | adding more results, by checking accesses to the expression context. We |
| 147 | assume that the context has been used, or deliberately ignored, if it has |
| 148 | been accessed. |
| 149 | |
| 150 | The Result object is interoperable with python AST objects: when an AST |
| 151 | object gets added to a Result object, it gets converted on-the-fly. |
| 152 | """ |
| 153 | |
| 154 | __slots__ = ("stmts", "temp_variables", "_expr", "__used_expr") |
| 155 | |
| 156 | def __init__(self, *, stmts=(), expr=None, temp_variables=()): |
| 157 | self.stmts = list(stmts) |
| 158 | self.temp_variables = list(temp_variables) |
| 159 | self._expr = expr |
| 160 | |
| 161 | self.__used_expr = False |
| 162 | |
| 163 | @property |
| 164 | def expr(self): |
| 165 | self.__used_expr = True |
| 166 | return self._expr |
| 167 | |
| 168 | @expr.setter |
| 169 | def expr(self, value): |
| 170 | self.__used_expr = False |
| 171 | self._expr = value |
| 172 | |
| 173 | @property |
| 174 | def lineno(self): |
| 175 | if self._expr is not None: |
| 176 | return self._expr.lineno |
| 177 | if self.stmts: |
| 178 | return self.stmts[-1].lineno |
| 179 | return None |
| 180 | |
| 181 | @property |
| 182 | def col_offset(self): |
| 183 | if self._expr is not None: |
| 184 | return self._expr.col_offset |
| 185 | if self.stmts: |
| 186 | return self.stmts[-1].col_offset |
no outgoing calls
no test coverage detected