Represents a Fortran expression as an op-data pair. Expr instances are hashable and sortable.
| 155 | |
| 156 | |
| 157 | class Expr: |
| 158 | """Represents a Fortran expression as an op-data pair. |
| 159 | |
| 160 | Expr instances are hashable and sortable. |
| 161 | """ |
| 162 | |
| 163 | @staticmethod |
| 164 | def parse(s, language=Language.C): |
| 165 | """Parse a Fortran expression to an Expr. |
| 166 | """ |
| 167 | return fromstring(s, language=language) |
| 168 | |
| 169 | def __init__(self, op, data): |
| 170 | assert isinstance(op, Op) |
| 171 | |
| 172 | # sanity checks |
| 173 | if op is Op.INTEGER: |
| 174 | # data is a 2-tuple of numeric object and a kind value |
| 175 | # (default is 4) |
| 176 | assert isinstance(data, tuple) and len(data) == 2 |
| 177 | assert isinstance(data[0], int) |
| 178 | assert isinstance(data[1], (int, str)), data |
| 179 | elif op is Op.REAL: |
| 180 | # data is a 2-tuple of numeric object and a kind value |
| 181 | # (default is 4) |
| 182 | assert isinstance(data, tuple) and len(data) == 2 |
| 183 | assert isinstance(data[0], float) |
| 184 | assert isinstance(data[1], (int, str)), data |
| 185 | elif op is Op.COMPLEX: |
| 186 | # data is a 2-tuple of constant expressions |
| 187 | assert isinstance(data, tuple) and len(data) == 2 |
| 188 | elif op is Op.STRING: |
| 189 | # data is a 2-tuple of quoted string and a kind value |
| 190 | # (default is 1) |
| 191 | assert isinstance(data, tuple) and len(data) == 2 |
| 192 | assert (isinstance(data[0], str) |
| 193 | and data[0][::len(data[0]) - 1] in ('""', "''", '@@')) |
| 194 | assert isinstance(data[1], (int, str)), data |
| 195 | elif op is Op.SYMBOL: |
| 196 | # data is any hashable object |
| 197 | assert hash(data) is not None |
| 198 | elif op in (Op.ARRAY, Op.CONCAT): |
| 199 | # data is a tuple of expressions |
| 200 | assert isinstance(data, tuple) |
| 201 | assert all(isinstance(item, Expr) for item in data), data |
| 202 | elif op in (Op.TERMS, Op.FACTORS): |
| 203 | # data is {<term|base>:<coeff|exponent>} where dict values |
| 204 | # are nonzero Python integers |
| 205 | assert isinstance(data, dict) |
| 206 | elif op is Op.APPLY: |
| 207 | # data is (<function>, <operands>, <kwoperands>) where |
| 208 | # operands are Expr instances |
| 209 | assert isinstance(data, tuple) and len(data) == 3 |
| 210 | # function is any hashable object |
| 211 | assert hash(data[0]) is not None |
| 212 | assert isinstance(data[1], tuple) |
| 213 | assert isinstance(data[2], dict) |
| 214 | elif op is Op.INDEXING: |
no outgoing calls
searching dependent graphs…