def _concatOperation(self, node): result = "" assert node.type=="operation" and node.get("operator")=="ADD", "Can only process concatenation of string literals" evaluate.evaluate(node) if node.evaluated != (): result = node.evaluated else: console.warn("Unknown expression as argument to translation method (%s:%s)" % (treeutil.getFileFromSyntaxItem(node), node.get("line"),)) return result
def reduceOperation(literalNode): resultNode = literalNode treeModified = False # can only reduce with constants if literalNode.type != "constant": return literalNode, False # check if we're in an operation ngParent = nextNongroupParent(literalNode) # could be operand in ops if not ngParent or ngParent.type != "operation": return literalNode, False else: operationNode = ngParent # try to evaluate expr operationNode = evaluate.evaluate(operationNode) if operationNode.evaluated != (): # we have a value # create replacement resultNode = symbol("constant")( operationNode.get("line"), operationNode.get("column")) resultNode = set_node_type_from_value(resultNode, operationNode.evaluated) # modify tree operationNode.parent.replaceChild(operationNode, resultNode) treeModified = True return resultNode, treeModified
def reduceOperation(literalNode): resultNode = literalNode treeModified = False # can only reduce with constants if literalNode.type != "constant": return literalNode, False # check if we're in an operation ngParent = nextNongroupParent(literalNode) # could be operand in ops if not ngParent or ngParent.type != "operation": return literalNode, False else: operationNode = ngParent # try to evaluate expr operationNode = evaluate.evaluate(operationNode) if operationNode.evaluated != (): # we have a value # create replacement resultNode = symbol("constant")(operationNode.get("line"), operationNode.get("column")) resultNode = set_node_type_from_value(resultNode, operationNode.evaluated) # modify tree operationNode.parent.replaceChild(operationNode, resultNode) treeModified = True return resultNode, treeModified
def _concatOperation(self, node): result = "" assert node.type == "operation" and node.get( "operator" ) == "ADD", "Can only process concatenation of string literals" evaluate.evaluate(node) if node.evaluated != (): result = node.evaluated else: console.warn( "Unknown expression as argument to translation method (%s:%s)" % ( treeutil.getFileFromSyntaxItem(node), node.get("line"), )) return result
def reduceLoop(startNode): treeModified = False conditionNode = None # find the loop's condition node node = startNode while(node): if node.parent and node.parent.type == "loop" and node.parent.getFirstChild(ignoreComments=True)==node: conditionNode = node break node = node.parent if not conditionNode: return treeModified # handle "if" statements if conditionNode.parent.get("loopType") == "IF": loopNode = conditionNode.parent evaluate.evaluate(conditionNode) if conditionNode.evaluated!=(): reducer.reduce(loopNode) treeModified = True return treeModified
def reduceLoop(startNode): treeModified = False conditionNode = None # find the loop's condition node node = startNode while (node): if node.parent and node.parent.type == "loop" and node.parent.getFirstChild( ignoreComments=True) == node: conditionNode = node break node = node.parent if not conditionNode: return treeModified # handle "if" statements if conditionNode.parent.get("loopType") == "IF": loopNode = conditionNode.parent evaluate.evaluate(conditionNode) if conditionNode.evaluated != (): reducer.reduce(loopNode) treeModified = True return treeModified