Example #1
0
        def is_pointer(x):
            if x.kind == p.TOKEN:
                # see if it is a pointer declared in the current block
                # or a parent block
                for b in self.current_block:
                    for parameter in b.variables:
                        if parameter.name == x.value:
                            if helper.pointer_indirection(
                                    parameter.declaration.value, p) > 0:
                                return parameter

                # see if it is a pointer from the parameter list
                if self.current_function:
                    for parameter in self.current_function[-1].parameters:
                        if parameter.name == x.value:
                            if helper.pointer_indirection(
                                    parameter.declaration.value, p) > 0:
                                return parameter
            return None
Example #2
0
    def format_expression_as_list(self, token : parser.Node):
        """
        Given an operator node, return a list of tokens/nodes/strings to
        be passed to format_op.
        """
        p = self.p
        a = analyzer.Analyzer

        r = []

        if token.value[0].kind in [p.TOKEN, p.OPERATOR]:
            r = [token.value[0]]
            if token.value[1].kind == p.OPERATOR:
                if is_sym(token.value[1].value[0], "("):
                    r += []
                else:
                    r += [" "]
            else:
                # handles e.g. x = L'♥'
                if token.value[1].kind != p.STRING:
                    r += [" "]
            r += [token.value[1]]
            return r

        op = token.value[0].value
    
        left = None
        right = None
        extra = None

        if a.level[op] == 14: # postfix
            if token.value[1] is None:
                left = token.value[2]
            else:
                right = token.value[1]
                if len(token.value) > 2:
                    p.error_token("Operator '" + op + "' can't have two operands here.", token)
        else:
            if len(token.value) == 2:
                right = token.value[1]
            if len(token.value) == 3:
                if token.value[1] is None:
                    left = token.value[2]
                elif op in "([{":
                    right = token.value[1]
                    extra = token.value[2]
                else:
                    left = token.value[1]
                    right = token.value[2]

        if left:
            if left.kind == p.OPERATOR:
                if left.value[0].kind == p.SYMBOL:
                    leftop = left.value[0].value
                else:
                    leftop = " "
                if leftop in "([{" or a.precedence(leftop, op):
                    r += [left]
                else:
                    r += ["(", left, ")"]
            else:
                r += [left]
            if op not in self.operator_hug_left:
                r += [" "]

        operator_value = token.value[0]

        # If x is a pointer, return the type declaration, else None
        def is_pointer(x):
            if x.kind == p.TOKEN:
                # see if it is a pointer declared in the current block
                # or a parent block
                for b in self.current_block:
                    for parameter in b.variables:
                        if parameter.name == x.value:
                            if helper.pointer_indirection(
                                    parameter.declaration.value, p) > 0:
                                return parameter

                # see if it is a pointer from the parameter list
                if self.current_function:
                    for parameter in self.current_function[-1].parameters:
                        if parameter.name == x.value:
                            if helper.pointer_indirection(
                                    parameter.declaration.value, p) > 0:
                                return parameter
            return None
        
        # special recognition of . as ->
        if token.value[0].kind == p.SYMBOL:
            if token.value[0].value == ".":
                chain = helper.find_dots(p, token)
                param = is_pointer(chain[0])
                if not param:
                    chain = []
                for c in chain[1:-1]:
                    param = helper.get_pointer_member(param, c.value, p)
                    if not param:
                        break
                    if helper.pointer_indirection(param.declaration.value, p) > 0:
                        pass
                    else:
                        break
                else:
                    if param:
                        operator_value = "->"

        r += [operator_value]

        if right:
            if op not in self.operator_hug_right:
                r += " "
            if right.kind == p.OPERATOR:
                if right.value[0].kind == p.SYMBOL:
                    rightop = right.value[0].value
                else:
                    rightop = " "
                if rightop not in "([{" and a.precedence(op, rightop):
                    r += ["(", right, ")"]
                else:
                    r += [right]
            else:
                r += [right]

        if extra:
            r += [extra]

        if op == "//":
            r = ["(int)", "("] + r + [")"]
            
        return r