コード例 #1
0
def p_funcall_expr(p):
    """expr : expr LPAREN expr_list RPAREN
            | expr LPAREN RPAREN
    """
    if (len(p) == 5 and len(p[3]) == 1 and p[3][0].__class__ is node.expr
            and p[3][0].op == ":" and not p[3][0].args):
        # foo(:) => ravel(foo)
        p[0] = node.funcall(func_expr=node.ident("ravel"),
                            args=node.expr_list([p[1]]))
    else:
        args = node.expr_list() if len(p) == 4 else p[3]
        assert isinstance(args, node.expr_list)
        p[0] = node.funcall(func_expr=p[1], args=args)
コード例 #2
0
def p_funcall_expr(p):
    """expr : expr LPAREN expr_list RPAREN
            | expr LPAREN RPAREN
    """
    if (len(p) == 5 and len(p[3]) == 1 and p[3][0].__class__ is node.expr and
            p[3][0].op == ":" and not p[3][0].args):
        # foo(:) => ravel(foo)
        p[0] = node.funcall(
            func_expr=node.ident("ravel"), args=node.expr_list([p[1]]))
    else:
        args = node.expr_list() if len(p) == 4 else p[3]
        assert isinstance(args, node.expr_list)
        p[0] = node.funcall(func_expr=p[1], args=args)
コード例 #3
0
def let_statement(u):
    """
    If LHS is a plain variable, and RHS is a matrix
    enclosed in square brackets, replace the matrix
    expr with a funcall.
    """
    if u.__class__ is node.let:
        if (u.ret.__class__ is node.ident and u.args.__class__ is node.matrix):
            u.args = node.funcall(func_expr=node.ident("matlabarray"),
                                  args=node.expr_list([u.args]))
コード例 #4
0
ファイル: parse.py プロジェクト: yenchih/smop
def p_command(p):
    """
    command : ident args SEMI
    """
#    if p[1].name == "load":
#        # "load filename x" ==> "x=load(filename)"
#        # "load filename x y z" ==> "(x,y,z)=load(filename)"
#        ret=node.expr_list([node.ident(t.value) for t in p[2][1:]])
#        p[0] = node.funcall(func_expr=p[1],
#                            args=node.expr_list(p[2]),
#                            ret=ret)
#    else:
    p[0] = node.funcall(p[1],p[2])
コード例 #5
0
def p_command(p):
    """
    command : ident args SEMI
    """
    #    if p[1].name == "load":
    #        # "load filename x" ==> "x=load(filename)"
    #        # "load filename x y z" ==> "(x,y,z)=load(filename)"
    #        ret=node.expr_list([node.ident(t.value) for t in p[2][1:]])
    #        p[0] = node.funcall(func_expr=p[1],
    #                            args=node.expr_list(p[2]),
    #                            ret=ret)
    #    else:
    p[0] = node.funcall(p[1], p[2])
コード例 #6
0
ファイル: resolve.py プロジェクト: yisonghan/smop
def resolve(t, symtab=None, fp=None, func_name=None):
    if symtab is None:
        symtab = {}
    do_resolve(t, symtab)
    G = as_networkx(t)
    #import pdb;pdb.set_trace()
    for n in G.nodes():
        u = G.node[n]["ident"]
        if u.props:
            pass
        elif G.out_edges(n) and G.in_edges(n):
            u.props = "U"  # upd
            #print u.name, u.lineno, u.column
        elif G.in_edges(n):
            u.props = "D"  # def
        elif G.out_edges(n):
            u.props = "R"  # ref
        else:
            u.props = "F"  # ???
        G.node[n]["label"] = "%s\\n%s" % (n, u.props)

    for u in node.postorder(t):
        #if u.__class__ is node.func_decl:
        #    u.ident.name += "_"
        if u.__class__ is node.funcall:
            try:
                if u.func_expr.props in "UR":  # upd,ref
                    u.__class__ = node.arrayref
                #else:
                #    u.func_expr.name += "_"
            except:
                pass

    for u in node.postorder(t):
        if u.__class__ in (node.arrayref, node.cellarrayref):
            for i, v in enumerate(u.args):
                if v.__class__ is node.expr and v.op == ":":
                    v.op = "::"


#                for w in node.postorder(v):
#                    if w.__class__ is node.expr and w.op == "end":
#                        w.args[0] = u.func_expr
#                        w.args[1] = node.number(i)

    for u in node.postorder(t):
        if u.__class__ is node.let:
            if (u.ret.__class__ is node.ident
                    and u.args.__class__ is node.matrix):
                u.args = node.funcall(func_expr=node.ident("matlabarray"),
                                      args=node.expr_list([u.args]))

    H = nx.connected_components(G.to_undirected())
    for i, component in enumerate(H):
        for nodename in component:
            if G.node[nodename]["ident"].props == "R":
                has_update = 1
                break
        else:
            has_update = 0
        if has_update:
            for nodename in component:
                G.node[nodename]["ident"].props += "S"  # sparse
        #S = G.subgraph(nbunch)
        #print S.edges()
    return G
コード例 #7
0
ファイル: parse.py プロジェクト: yenchih/smop
def p_expr2(p):
    """expr2 : expr AND expr
             | expr ANDAND expr
             | expr BACKSLASH expr
             | expr COLON expr
             | expr DIV expr
             | expr DOT expr
             | expr DOTDIV expr
             | expr DOTDIVEQ expr
             | expr DOTEXP expr
             | expr DOTMUL expr
             | expr DOTMULEQ expr
             | expr EQEQ expr
             | expr EXP expr
             | expr EXPEQ expr
             | expr GE expr
             | expr GT expr 
             | expr LE expr
             | expr LT expr
             | expr MINUS expr
             | expr MUL expr
             | expr NE expr
             | expr OR expr
             | expr OROR expr
             | expr PLUS expr
             | expr EQ expr
             | expr MULEQ expr
             | expr DIVEQ expr
             | expr MINUSEQ expr
             | expr PLUSEQ expr
             | expr OREQ expr
             | expr ANDEQ expr
    """
    if p[2] == "=":
        if p[1].__class__ is node.let:
            raise NotImplementedError("assignment "
                                      "as expression")

        # The algorithm, which decides if an
        # expression F(X)
        # is arrayref or funcall, is implemented in
        # resolve.py, except the following lines up
        # to XXX. These lines handle the case where
        # an undefined array is updated:
        #    >>> clear a
        #    >>> a[1:10]=123
        # Though legal in matlab, these lines
        # confuse the algorithm, which thinks that
        # the undefined variable is a function name.
        # To prevent the confusion, we mark these
        # nodes arrayref as early as during the parse
        # phase.
        if p[1].__class__ is node.funcall:
            # A(B) = C
            p[1].__class__ = node.arrayref
        elif p[1].__class__ is node.matrix:
            # [A1(B1) A2(B2) ...] = C
            for e in p[1].args:
                if e.__class__ is node.funcall:
                    e.__class__ = node.arrayref
        # XXX   

        if isinstance(p[1],node.getfield):
            #import pdb;pdb.set_trace()
            # A.B=C  setfield(A,B,C)
            p[0] = node.setfield(p[1].args[0],
                                 p[1].args[1],
                                 p[3])
        else:
            #assert len(p[1].args) > 0
            ret = p[1].args if isinstance(p[1],node.matrix) else p[1]
            p[0] = node.let(ret=ret,
                            args=p[3],
                            lineno=p.lineno(2),
                            lexpos=p.lexpos(2))

            if isinstance(p[1],node.matrix):
                # TBD: mark idents as "P" - persistent
                if p[3].__class__ not in (node.ident,node.funcall): #, p[3].__class__
                    raise NotImplementedError("multi-assignment %d" % p[0].lineno)
                if p[3].__class__ is node.ident:
                    # [A1(B1) A2(B2) ...] = F     implied F()
                    # import pdb; pdb.set_trace()
                    p[3] = node.funcall(func_expr=p[3],
                                        args=node.expr_list())
                # [A1(B1) A2(B2) ...] = F(X)
                p[3].nargout = len(p[1].args[0])
    elif p[2] == ".*":
        p[0] = node.dot(p[1],p[3])
#    elif p[2] == "." and isinstance(p[3],node.expr) and p[3].op=="parens":
#        p[0] = node.getfield(p[1],p[3].args[0])
#        raise NotImplementedError(p[3],p.lineno(3),p.lexpos(3))
    elif p[2] == ":" and isinstance(p[1],node.expr) and p[1].op==":":
        # Colon expression means different things depending on the
        # context.  As an array subscript, it is a slice; otherwise,
        # it is a call to the "range" function, and the parser can't
        # tell which is which.  So understanding of colon expressions
        # is put off until after "resolve".
        p[0] = p[1]
        p[0].args.insert(1,p[3])
    else:
        p[0] = node.expr(op=p[2],args=node.expr_list([p[1],p[3]]))
コード例 #8
0
def p_expr2(p):
    """expr2 : expr AND expr
             | expr ANDAND expr
             | expr BACKSLASH expr
             | expr COLON expr
             | expr DIV expr
             | expr DOT expr
             | expr DOTDIV expr
             | expr DOTDIVEQ expr
             | expr DOTEXP expr
             | expr DOTMUL expr
             | expr DOTMULEQ expr
             | expr EQEQ expr
             | expr POW expr
             | expr EXP expr
             | expr EXPEQ expr
             | expr GE expr
             | expr GT expr
             | expr LE expr
             | expr LT expr
             | expr MINUS expr
             | expr MUL expr
             | expr NE expr
             | expr OR expr
             | expr OROR expr
             | expr PLUS expr
             | expr EQ expr
             | expr MULEQ expr
             | expr DIVEQ expr
             | expr MINUSEQ expr
             | expr PLUSEQ expr
             | expr OREQ expr
             | expr ANDEQ expr
    """
    if p[2] == "=":
        if p[1].__class__ is node.let:
            raise_exception(SyntaxError,
                            "Not implemented assignment as expression",
                            new_lexer)
        # The algorithm, which decides if an
        # expression F(X)
        # is arrayref or funcall, is implemented in
        # resolve.py, except the following lines up
        # to XXX. These lines handle the case where
        # an undefined array is updated:
        #    >>> clear a
        #    >>> a[1:10]=123
        # Though legal in matlab, these lines
        # confuse the algorithm, which thinks that
        # the undefined variable is a function name.
        # To prevent the confusion, we mark these
        # nodes arrayref as early as during the parse
        # phase.
        if p[1].__class__ is node.funcall:
            # A(B) = C
            p[1].__class__ = node.arrayref
        elif p[1].__class__ is node.matrix:
            # [A1(B1) A2(B2) ...] = C
            for e in p[1].args:
                if e.__class__ is node.funcall:
                    e.__class__ = node.arrayref
        # XXX

        if isinstance(p[1], node.getfield):
            # import pdb;pdb.set_trace()
            # A.B=C  setfield(A,B,C)
            p[0] = node.setfield(p[1].args[0], p[1].args[1], p[3])
        else:
            # assert len(p[1].args) > 0
            ret = p[1].args if isinstance(p[1], node.matrix) else p[1]
            p[0] = node.let(ret=ret,
                            args=p[3],
                            lineno=p.lineno(2),
                            lexpos=p.lexpos(2))

            if isinstance(p[1], node.matrix):
                # TBD: mark idents as "P" - persistent
                if p[3].__class__ not in (node.ident,
                                          node.funcall):  #, p[3].__class__
                    raise_exception(SyntaxError, "multi-assignment", new_lexer)
                if p[3].__class__ is node.ident:
                    # [A1(B1) A2(B2) ...] = F     implied F()
                    # import pdb; pdb.set_trace()
                    p[3] = node.funcall(func_expr=p[3], args=node.expr_list())
                # [A1(B1) A2(B2) ...] = F(X)
                p[3].nargout = len(p[1].args[0])
    elif p[2] == "*":
        p[0] = node.funcall(func_expr=node.ident("dot"),
                            args=node.expr_list([p[1], p[3]]))
    elif p[2] == ".*":
        p[0] = node.funcall(func_expr=node.ident("multiply"),
                            args=node.expr_list([p[1], p[3]]))


#    elif p[2] == "." and isinstance(p[3],node.expr) and p[3].op=="parens":
#        p[0] = node.getfield(p[1],p[3].args[0])
#        raise SyntaxError(p[3],p.lineno(3),p.lexpos(3))
    elif p[2] == ":" and isinstance(p[1], node.expr) and p[1].op == ":":
        # Colon expression means different things depending on the
        # context.  As an array subscript, it is a slice; otherwise,
        # it is a call to the "range" function, and the parser can't
        # tell which is which.  So understanding of colon expressions
        # is put off until after "resolve".
        p[0] = p[1]
        p[0].args.insert(1, p[3])
    else:
        p[0] = node.expr(op=p[2], args=node.expr_list([p[1], p[3]]))
コード例 #9
0
ファイル: resolve.py プロジェクト: victorlei/smop
def resolve(t, symtab=None, fp=None, func_name=None):
    if symtab is None:
        symtab = {}
    do_resolve(t, symtab)
    G = as_networkx(t)
    # import pdb;pdb.set_trace()
    for n in G.nodes():
        u = G.node[n]["ident"]
        if u.props:
            pass
        elif G.out_edges(n) and G.in_edges(n):
            u.props = "U"  # upd
            # print u.name, u.lineno, u.column
        elif G.in_edges(n):
            u.props = "D"  # def
        elif G.out_edges(n):
            u.props = "R"  # ref
        else:
            u.props = "F"  # ???
        G.node[n]["label"] = "%s\\n%s" % (n, u.props)

    for u in node.postorder(t):
        # if u.__class__ is node.func_decl:
        #    u.ident.name += "_"
        if u.__class__ is node.funcall:
            try:
                if u.func_expr.props in "UR":  # upd,ref
                    u.__class__ = node.arrayref
                # else:
                #    u.func_expr.name += "_"
            except:
                pass

    for u in node.postorder(t):
        if u.__class__ in (node.arrayref, node.cellarrayref):
            for i, v in enumerate(u.args):
                if v.__class__ is node.expr and v.op == ":":
                    v.op = "::"
    #                for w in node.postorder(v):
    #                    if w.__class__ is node.expr and w.op == "end":
    #                        w.args[0] = u.func_expr
    #                        w.args[1] = node.number(i)

    for u in node.postorder(t):
        if u.__class__ is node.let:
            if u.ret.__class__ is node.ident and u.args.__class__ is node.matrix:
                u.args = node.funcall(func_expr=node.ident("matlabarray"), args=node.expr_list([u.args]))

    H = nx.connected_components(G.to_undirected())
    for i, component in enumerate(H):
        for nodename in component:
            if G.node[nodename]["ident"].props == "R":
                has_update = 1
                break
        else:
            has_update = 0
        if has_update:
            for nodename in component:
                G.node[nodename]["ident"].props += "S"  # sparse
        # S = G.subgraph(nbunch)
        # print S.edges()
    return G