Beispiel #1
0
def interpreter(ln, rn, surround=None, indent=0):
    '''
    If ln and rn are both collections(e.g. tuple/list), then we should take apart the collections and get
    the "ln = rn" relationship for all single elements contained in the collections.
    '''

    if not isinstance(rn, pFunc):

        # rn.add_surround(surround)
        if rn.Type == 'variable':
            rn.add_indent(indent)

        varobjects = []
        if isinstance(ln, list):
            for i in ln:
                varobjects.append(i)
        else:
            varobjects.append(ln)
        rn.add_surround(surround)
        # print(Green(str(rn)))
        dealWithStatement(param=rn, varobjects=varobjects)

    else:

        interpreterForFunction(rn, ln, indent, surround)
Beispiel #2
0
    def visit_Expr(self, Expr, surround=None, indent=0):
        if isinstance(Expr.value, ast.Call):
            # funcName = self.nest(Expr.value.func)

            param = recognizeMultiAssignment(value=Expr.value,
                                             indent=indent,
                                             surround=surround)
            if not isinstance(param, pFunc):
                raise Exception('Type error! Expect pFunc but receive ' +
                                str(type(param)))
            if not param.restricted:
                dealWithStatement(param=param)
            else:
                dealWithStatement(param=param)
Beispiel #3
0
    def visit_WithItems(self, With, surround=None, indent=0):

        items = With.items
        param = pWith()
        for item in items:

            param1 = None
            param2 = None
            if hasattr(item, 'context_expr'):
                param1 = recognizeMultiAssignment(item.context_expr,
                                                  indent=indent)
                # param1's Type is function-like class
                if isinstance(param1, pFunc):
                    randomname = varNameGenerator(varnamesRead)
                    pfunc = pFunc(funcName=param1.funcName,
                                  params=param1.params,
                                  suffix=param1.suffix,
                                  Type='function',
                                  restricted=param1.restricted,
                                  indent=indent)
                    if pfunc.restricted:
                        pfunc.add_Type('restrictedFunc')
                    pfunc.add_surround(surround)
                    pfunc.add_child(param)
                    vparam = pVar(randomname)
                    param1 = copy.deepcopy(vparam)
                    dealWithStatement(param=pfunc, varobjects=[vparam])
                    param.add_parent(pfunc)
                    param1.update_varTofunc_ele(pfunc)

                elif isinstance(param1, pVar):
                    pass

                else:
                    raise Exception(Cyan('with context_expr\'s type is not handled: ' \
                            + str(param1)))
            if item.optional_vars:
                param2 = recognizeMultiAssignment(item.optional_vars,
                                                  indent=indent)
            param.add_withitem((param1, param2))
            param.add_surround(surround)
            param.add_indent(indent)

        return param
Beispiel #4
0
def buildCallString(ele, fullstr, indent, surround, rv=False):

    params = fillInParams(ele.args, ele.keywords, indent, surround)

    pfunc = pFunc(funcName=fullstr,
                  Type='function',
                  indent=indent,
                  surround=surround,
                  params=params)

    if rv:
        return pfunc

    name_func = varNameGenerator(varnamesRead)
    varobject_func = pVar(name_func)

    dealWithStatement(param=pfunc, varobjects=[varobject_func])

    fullstr = name_func

    return fullstr
Beispiel #5
0
def recognizeCall(value, indent, outsideFunction, outsideList, outsideDict,
                  insideTuple, outCalculation, outsideSet, surround):

    pfunc = nestplus(value, indent, surround)
    pfunc.add_indent(indent)

    if outsideFunction or \
        outsideList or \
            outsideDict or \
                insideTuple or \
                    outCalculation or \
                        outsideSet:

        pfunc.add_surround(surround)
        name = varNameGenerator(varnamesRead)
        varobject = pVar(name)

        dealWithStatement(param=pfunc, varobjects=[varobject])

        return copy.deepcopy(varobject)

    return pfunc
Beispiel #6
0
def interpreterForFunction(rn, ln, indent, surround):
    '''
    May contain bugs!!!
    
    Now we cannot handle the situation where variables and functions both appear 
    in the right-hand side, such as "a, b, c = c, fun()". Because we do not know
    the number of fun()'s returned value. In this case, We can do nothing but 
    hope the above form will never shows up in the testing programs. In other
    words, if the right-hand side includes a function, then we tacitly approve
    that the function shows at the end.
    '''

    varobjects = []
    if isinstance(ln, list):
        for i in ln:
            varobjects.append(i)
    else:
        varobjects.append(ln)
    if rn.restricted:
        rn.add_Type('restrictedFunc')
    rn.add_indent(indent)
    rn.add_surround(surround)
    dealWithStatement(param=rn, varobjects=varobjects)
Beispiel #7
0
def buildFullString(rcur, indent, surround):

    fullstr = ''
    len_rcur = len(rcur)
    cnt = 0
    for ele in rcur[::-1]:
        cnt += 1
        if isinstance(ele, ast.Name):
            fullstr = buildNameString(ele, fullstr)

        elif isinstance(ele, ast.Attribute):
            fullstr = buildAttributeString(ele, fullstr)

        elif isinstance(ele, ast.BinOp) or isinstance(ele, ast.UnaryOp):
            param = recognizeMultiAssignment(ele, indent=indent)
            rn = varNameGenerator(varnamesRead)
            varobject = pVar(rn)
            dealWithStatement(param=param, varobjects=[varobject])
            fullstr += rn

        elif isinstance(ele, ast.Subscript):
            if cnt < len_rcur:
                fullstr = buildSubscriptString(ele, fullstr, indent)
            else:
                return buildSubscriptString(ele, fullstr, indent, True)

        elif isinstance(ele, ast.Call):
            if cnt < len_rcur:
                fullstr = buildCallString(ele, fullstr, indent, surround)
            else:
                return buildCallString(ele, fullstr, indent, surround, True)

        elif isinstance(ele, ast.Constant):
            fullstr += '\'\'\'' + ele.value + '\'\'\''

    return fullstr
Beispiel #8
0
 def visit_With(self, With, surround=None, indent=0, func=None):
     # pass
     param = self.visit_WithItems(With, surround=surround, indent=indent)
     dealWithStatement(param=param)
     self.visit_WithBody(With, param, indent, func)
Beispiel #9
0
def buildSubscriptString(ele, fullstr, indent, judge=False):

    name = varNameGenerator(varnamesRead)
    varobject = pVar(name)
    psubs = pSubs()
    psubs.add_prefix(pVar(fullstr))
    slice = ele.slice
    rv = recognizeMultiAssignment(slice, outsideSlice=True)

    if isinstance(rv, pVar):
        fullstr += '[' + rv.name + ']'
        varnamesRead.add(rv.name)
        psubs.add_content(rv)

    elif isinstance(rv, pNumber):
        fullstr += '[' + str(rv.num) + ']'
        psubs.add_content(rv)

    elif isinstance(rv, pConst):
        fullstr += '[\'' + str(rv.const) + '\']'
        psubs.add_content(rv)

    elif isinstance(rv, pBinop) or isinstance(rv, pUop):
        rn = varNameGenerator(varnamesRead)
        varobject = pVar(rn)
        dealWithStatement(param=rv, varobjects=[varobject])
        psubs.add_content(varobject)
        fullstr += '[' + rn + ']'

    elif isinstance(rv, pTuple):
        '''
            a[:, 2] = ...
        '''
        psubs.add_content(rv)
        fullstr += '['
        for ele in rv.content:
            if isinstance(ele, pConst):
                fullstr += ele.const + ','
            elif isinstance(ele, pNumber):
                fullstr += str(ele.num) + ','
            elif isinstance(ele, pVar):
                fullstr += ele.name + ','
            elif isinstance(ele, pSlice):
                fullstr += ':,'

        if rv.content:
            fullstr = fullstr[:-1]
        fullstr += ']'

    else:
        rn = varNameGenerator(varnamesRead)
        pvar = pVar(rn)
        dealWithStatement(param=rv, varobjects=[pvar])
        fullstr += '[' + rn + ']'
        psubs.add_content(pvar)

    psubs.add_fullstr(fullstr)
    psubs.add_indent(indent)
    if judge:
        return psubs

    dealWithStatement(param=psubs, varobjects=[varobject])
    fullstr = name
    return fullstr