Пример #1
0
def reduceLoop(startNode):
    treeModified = False
    conditionNode = None

    # Can only reduce constant condition expression
    if startNode.type != "constant":
        return treeModified

    # Can only reduce a condition expression
    # of a loop context
    node = startNode
    while(node):  # find the loop's condition 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
        # startNode must be only condition
        if startNode==conditionNode or isDirectDescendant(startNode, conditionNode):
            value = startNode.get("value")
            if startNode.get("constantType") == 'string':
                value = '"' + value + '"'
            # re-parse into an internal value
            value = json.loads(value)
            condValue = bool(value)
            #print "optimizing: if"
            treeutil.inlineIfStatement(loopNode, condValue)
            treeModified = True

    return treeModified
def processEnvironmentClass(node, variantMap):
    
    def myVariantNodes(node):
        variantNodes = treeutil.findVariablePrefix(node, "this.useCheck")
        for variantNode in variantNodes:
            print variantNode.toXml()
            if not variantNode.hasParentContext("call/operand"):
                continue
            else:
                yield variantNode

    treeModified = False

    #TODO: use myVariantNodes()

    # Simple sanity checks
    params = callNode.getChild("params")
    if len(params.children) != 1:
        log("Warning", "Expecting exactly one argument for qx.core.Environment.get. Ignoring this occurrence.", params)
        return treeModified

    firstParam = params.getChildByPosition(0)
    if not isStringLiteral(firstParam):
        log("Warning", "First argument must be a string literal! Ignoring this occurrence.", firstParam)
        return treeModified

    variantKey = firstParam.get("value");
    variantValue, found = __keyLookup(variantKey, variantMap)
    #if not variantKey in variantMap.keys():
    if not found:
        return treeModified
    #else:
    #    variantValue = variantMap[variantKey]

    # Processing
    # are we in a if/loop condition expression, i.e. a "loop/expression/..." context?
    conditionNode = None
    loopType = None
    node = callNode
    while (node):
        if node.type == "expression" and node.parent and node.parent.type == "loop":
            conditionNode = node
            break
        node = node.parent

    if not conditionNode:
        return treeModified

    # handle "if" statements
    if conditionNode.parent.get("loopType") == "IF":
        loopNode = conditionNode.parent
        # get() call is only condition
        if callNode.parent == conditionNode:
            #TODO: variantValue is not interesting, only if variantKey is in variantMap (!?)
            treeutil.inlineIfStatement(loopNode, bool(variantValue)) # take the truth val of the key value
            treeModified = True

    return treeModified
Пример #3
0
 def visit_loop(self, node):
     loop_type = node.get("loopType")
     if loop_type == "IF":
         cond_node = node.children[0]
         if hasattr(cond_node, "evaluated") and cond_node.evaluated != ():
             #self._rewrite_if(node, bool(cond_node.evaluated))
             treeutil.inlineIfStatement(node, bool(cond_node.evaluated))
     for child in node.children:
         self.visit(child)
Пример #4
0
 def visit_loop(self, node):
     loop_type = node.get("loopType")
     if loop_type == "IF":
         cond_node = node.children[0]
         if hasattr(cond_node, "evaluated") and cond_node.evaluated!=():
             #self._rewrite_if(node, bool(cond_node.evaluated))
             treeutil.inlineIfStatement(node, bool(cond_node.evaluated))
     for child in node.children:
         self.visit(child)
Пример #5
0
def processEnvironmentClass(tree, usedVariantKeys):
    
    def useCheckNodes(node):
        variantNodes = treeutil.findVariablePrefix(node, "this.useCheck")
        for variantNode in variantNodes:
            #print variantNode.toXml()
            if not variantNode.hasParentContext("call/operand"):
                continue
            else:
                yield selectCallNode(variantNode)

    # -----------------------------------------------------------------
    global verbose
    global fileId
    verbose = False
    fileId = "qx.core.Environment"

    for callNode in useCheckNodes(tree):

        # Simple sanity checks
        params = callNode.getChild("params")
        if len(params.children) != 1:
            log("Warning", "Expecting exactly one argument for qx.core.Environment.get. Ignoring this occurrence.", params)
            continue

        firstParam = params.getChildByPosition(0)
        if not isStringLiteral(firstParam):
            log("Warning", "First argument must be a string literal! Ignoring this occurrence.", firstParam)
            continue

        variantKey = firstParam.get("value");
        if variantKey in usedVariantKeys:
            continue  # need these checks at run time

        # Processing
        # are we in a if/loop condition expression, i.e. a "loop/expression/..." context?
        conditionNode = None
        loopType = None
        node = callNode
        while (node):
            if node.type == "expression" and node.parent and node.parent.type == "loop":
                conditionNode = node
                break
            node = node.parent

        if not conditionNode:
            continue

        # handle "if" statements
        if conditionNode.parent.get("loopType") == "IF":
            loopNode = conditionNode.parent
            # useCheck() call is only condition
            if callNode.parent == conditionNode:
                treeutil.inlineIfStatement(loopNode, False) # use 'else' branch (if any)

    return tree
Пример #6
0
 def visit_loop(self, node):
     loop_type = node.get("loopType")
     nnode = node
     if loop_type == "IF":
         cond_node = node.children[0]
         if hasattr(cond_node, "evaluated"):
             value = bool(cond_node.evaluated)
             nnode, is_empty = treeutil.inlineIfStatement(node, value, inPlace=False)
     return nnode
Пример #7
0
 def visit_loop(self, node):
     loop_type = node.get("loopType")
     nnode = node
     if loop_type == "IF":
         cond_node = node.children[0]
         if hasattr(cond_node, "evaluated"):
             value = bool(cond_node.evaluated)
             nnode, is_empty = treeutil.inlineIfStatement(node,
                                                          value,
                                                          inPlace=False)
     return nnode
Пример #8
0
def reduceLoop(startNode):
    treeModified = False
    conditionNode = None
    loopType = None

    # Can only reduce constant condition expression
    if startNode.type != "constant":
        return treeModified

    # Can only reduce a condition expression,
    # i.e. a "loop/expression/..." context
    node = startNode
    while node:
        if node.type == "expression" and node.parent and node.parent.type == "loop":
            conditionNode = node
            break
        node = node.parent
    if not conditionNode:
        return treeModified

    # handle "if" statements
    if conditionNode.parent.get("loopType") == "IF":
        loopNode = conditionNode.parent
        # startNode must be only condition
        if isDirectDescendant(startNode, conditionNode):
            value = startNode.get("value")
            if startNode.get("constantType") == "string":
                value = '"' + value + '"'
            # re-parse into an internal value
            value = json.loads(value)
            condValue = bool(value)
            # print "optimizing: if"
            treeutil.inlineIfStatement(loopNode, condValue)
            treeModified = True

    return treeModified
def processVariantGet(callNode, variantMap):

    treeModified = False
    fresult      = [None]  # input-output parameter for functions

    # Simple sanity checks
    params = callNode.getChild("params")
    if len(params.children) != 1:
        log("Warning", "Expecting exactly one argument for qx.core.Environment.get. Ignoring this occurrence.", params)
        return treeModified

    firstParam = params.getChildByPosition(0)
    if not isStringLiteral(firstParam):
        log("Warning", "First argument must be a string literal! Ignoring this occurrence.", firstParam)
        return treeModified

    variantKey = firstParam.get("value");
    confValue, found = __keyLookup(variantKey, variantMap)
    if not found:
        return treeModified

    # Processing
    # are we in a if/loop condition expression, i.e. a "loop/expression/..." context?
    conditionNode = None
    loopType = None
    node = callNode
    while (node):
        if node.type == "expression" and node.parent and node.parent.type == "loop":
            conditionNode = node
            break
        node = node.parent

    if not conditionNode:
        return treeModified

    # handle "if" statements
    if conditionNode.parent.get("loopType") == "IF":
        loopNode = conditionNode.parent
        # get() call is only condition
        #if callNode.parent == conditionNode:
        if isDirectDescendant(callNode, conditionNode):
            # @deprecated
            if confValue in ["off", "false"]:
                varValue = False
            else:
                varValue = bool(confValue)
            treeutil.inlineIfStatement(loopNode, varValue)
            treeModified = True
        # a single comparison is the condition
        #elif (callNode.parent.parent.type == "operation"
        #      and callNode.parent.parent.parent == conditionNode):
        elif isComparisonOperand(callNode, conditionNode, fresult):
            cmpNode = fresult[0]
            # check operator
            cmpOp  = cmpNode.get("operator")
            if cmpOp in ["EQ", "SHEQ"]:
                cmpFcn = operator.eq
            elif cmpOp in ["NE", "SHNE"]:
                cmpFcn = operator.ne
            else: # unsupported compare
                cmpFcn = None
            if cmpFcn:
                # get other compare operand
                cmpOperands = cmpNode.getChildren(True)
                if cmpOperands[0] == callNode.parent: # switch between "first" and "second"
                    otherValue = cmpOperands[1].getFirstChild(ignoreComments=True)
                else:
                    otherValue = cmpOperands[0].getFirstChild(ignoreComments=True)
                if otherValue.type == "constant":
                    constType = otherValue.get("constantType")
                    if constType == "number":
                        op1 = confValue
                        op2 = int(otherValue.get("value"))
                    elif constType == "string":
                        op1 = confValue
                        op2 = otherValue.get("value")
                    elif constType == "boolean":
                        # @deprecated
                        if isinstance(confValue, types.StringTypes) and confValue in ["on","off"]:
                            op1 = {"on":True,"off":False}[confValue]
                        else:
                            op1 = confValue
                        op2 = {"true":True, "false":False}[otherValue.get("value")]
                    elif constType == "null":
                        op1 = confValue
                        op2 = None
                    # compare result
                    if constType in ("number", "string", "boolean", "null"):
                        treeutil.inlineIfStatement(loopNode, cmpFcn(op1,op2))
                        treeModified = True

    return treeModified
Пример #10
0
def processEnvironmentClass(node, variantMap):
    def myVariantNodes(node):
        variantNodes = treeutil.findVariablePrefix(node, "this.useCheck")
        for variantNode in variantNodes:
            print variantNode.toXml()
            if not variantNode.hasParentContext("call/operand"):
                continue
            else:
                yield variantNode

    treeModified = False

    #TODO: use myVariantNodes()

    # Simple sanity checks
    params = callNode.getChild("params")
    if len(params.children) != 1:
        log(
            "Warning",
            "Expecting exactly one argument for qx.core.Environment.get. Ignoring this occurrence.",
            params)
        return treeModified

    firstParam = params.getChildByPosition(0)
    if not isStringLiteral(firstParam):
        log(
            "Warning",
            "First argument must be a string literal! Ignoring this occurrence.",
            firstParam)
        return treeModified

    variantKey = firstParam.get("value")
    variantValue, found = __keyLookup(variantKey, variantMap)
    #if not variantKey in variantMap.keys():
    if not found:
        return treeModified
    #else:
    #    variantValue = variantMap[variantKey]

    # Processing
    # are we in a if/loop condition expression, i.e. a "loop/expression/..." context?
    conditionNode = None
    loopType = None
    node = callNode
    while (node):
        if node.type == "expression" and node.parent and node.parent.type == "loop":
            conditionNode = node
            break
        node = node.parent

    if not conditionNode:
        return treeModified

    # handle "if" statements
    if conditionNode.parent.get("loopType") == "IF":
        loopNode = conditionNode.parent
        # get() call is only condition
        if callNode.parent == conditionNode:
            #TODO: variantValue is not interesting, only if variantKey is in variantMap (!?)
            treeutil.inlineIfStatement(
                loopNode,
                bool(variantValue))  # take the truth val of the key value
            treeModified = True

    return treeModified
Пример #11
0
def processVariantGet(callNode, variantMap):

    treeModified = False
    fresult = [None]  # input-output parameter for functions

    # Simple sanity checks
    params = callNode.getChild("params")
    if len(params.children) != 1:
        log(
            "Warning",
            "Expecting exactly one argument for qx.core.Environment.get. Ignoring this occurrence.",
            params)
        return treeModified

    firstParam = params.getChildByPosition(0)
    if not isStringLiteral(firstParam):
        log(
            "Warning",
            "First argument must be a string literal! Ignoring this occurrence.",
            firstParam)
        return treeModified

    variantKey = firstParam.get("value")
    confValue, found = __keyLookup(variantKey, variantMap)
    if not found:
        return treeModified

    # Processing
    # are we in a if/loop condition expression, i.e. a "loop/expression/..." context?
    conditionNode = None
    loopType = None
    node = callNode
    while (node):
        if node.type == "expression" and node.parent and node.parent.type == "loop":
            conditionNode = node
            break
        node = node.parent

    if not conditionNode:
        return treeModified

    # handle "if" statements
    if conditionNode.parent.get("loopType") == "IF":
        loopNode = conditionNode.parent
        # get() call is only condition
        #if callNode.parent == conditionNode:
        if isDirectDescendant(callNode, conditionNode):
            # @deprecated
            if confValue in ["off", "false"]:
                varValue = False
            else:
                varValue = bool(confValue)
            treeutil.inlineIfStatement(loopNode, varValue)
            treeModified = True
        # a single comparison is the condition
        #elif (callNode.parent.parent.type == "operation"
        #      and callNode.parent.parent.parent == conditionNode):
        elif isComparisonOperand(callNode, conditionNode, fresult):
            cmpNode = fresult[0]
            # check operator
            cmpOp = cmpNode.get("operator")
            if cmpOp in ["EQ", "SHEQ"]:
                cmpFcn = operator.eq
            elif cmpOp in ["NE", "SHNE"]:
                cmpFcn = operator.ne
            else:  # unsupported compare
                cmpFcn = None
            if cmpFcn:
                # get other compare operand
                cmpOperands = cmpNode.getChildren(True)
                if cmpOperands[
                        0] == callNode.parent:  # switch between "first" and "second"
                    otherValue = cmpOperands[1].getFirstChild(
                        ignoreComments=True)
                else:
                    otherValue = cmpOperands[0].getFirstChild(
                        ignoreComments=True)
                if otherValue.type == "constant":
                    constType = otherValue.get("constantType")
                    if constType == "number":
                        op1 = confValue
                        op2 = int(otherValue.get("value"))
                    elif constType == "string":
                        op1 = confValue
                        op2 = otherValue.get("value")
                    elif constType == "boolean":
                        # @deprecated
                        if isinstance(confValue,
                                      types.StringTypes) and confValue in [
                                          "on", "off"
                                      ]:
                            op1 = {"on": True, "off": False}[confValue]
                        else:
                            op1 = confValue
                        op2 = {
                            "true": True,
                            "false": False
                        }[otherValue.get("value")]
                    elif constType == "null":
                        op1 = confValue
                        op2 = None
                    # compare result
                    if constType in ("number", "string", "boolean", "null"):
                        treeutil.inlineIfStatement(loopNode, cmpFcn(op1, op2))
                        treeModified = True

    return treeModified