Transformation for performing some simple binary ops E.g.: + - / * etc... when left and right operands are supported constants
(self, context)
| 29 | return |
| 30 | |
| 31 | def binop(self, context): |
| 32 | """ |
| 33 | Transformation for performing some simple binary ops |
| 34 | E.g.: + - / * etc... when left and right operands are supported constants |
| 35 | """ |
| 36 | node = context.node |
| 37 | |
| 38 | if not isinstance(node, BinOp): |
| 39 | return |
| 40 | |
| 41 | # Lookup variables in a stack |
| 42 | if type(node.left) == str: |
| 43 | try: |
| 44 | node.left = context.stack[node.left] |
| 45 | context.visitor.modified = True |
| 46 | except (TypeError, KeyError): |
| 47 | pass |
| 48 | |
| 49 | if type(node.right) == str: |
| 50 | try: |
| 51 | node.right = context.stack[node.right] |
| 52 | context.visitor.modified = True |
| 53 | except (TypeError, KeyError): |
| 54 | pass |
| 55 | |
| 56 | # Rewrite `Var` pointers to their values they point to |
| 57 | if type(node.right) == Var and isinstance(node.right.value, ASTNode): |
| 58 | node.right = node.right.value |
| 59 | context.visitor.modified = True |
| 60 | |
| 61 | if type(node.left) == Var and isinstance(node.left.value, ASTNode): |
| 62 | node.left = node.left.value |
| 63 | context.visitor.modified = True |
| 64 | |
| 65 | if node.op == "add": |
| 66 | if type(node.left) == String and type(node.right) == String: |
| 67 | new_str = str(node.right) + str(node.left) |
| 68 | new_node = String(value=new_str) |
| 69 | new_node.enrich_from_previous(node) |
| 70 | context.replace(new_node) |
| 71 | return True |
| 72 | elif type(node.left) == Number and type(node.right) == Number: |
| 73 | new_node = Number(node.left.value + node.right.value) |
| 74 | new_node.enrich_from_previous(node) |
| 75 | context.replace(new_node) |
| 76 | return True |
| 77 | elif node.op == "mod": |
| 78 | try: |
| 79 | if type(node.left) == String and type(node.right) == String: |
| 80 | new_str = str(node.right) % str(node.left) |
| 81 | new_node = String(value=new_str) |
| 82 | new_node.enrich_from_previous(node) |
| 83 | context.replace(new_node) |
| 84 | return True |
| 85 | except TypeError: |
| 86 | pass |
| 87 | # TODO cover other cases |
| 88 |
nothing calls this directly
no test coverage detected