示例#1
0
文件: parse.py 项目: RobBW/smop
def p_try_catch(p):
    """
    try_catch : TRY stmt_list CATCH stmt_list END_STMT
              | TRY stmt_list END_STMT
    """
    assert isinstance(p[2],node.stmt_list)
    #assert isinstance(p[4],node.stmt_list)
    p[0] = node.try_catch(try_stmt=p[2],
                          catch_stmt=node.stmt_list(), # FIXME
                          finally_stmt=node.stmt_list())
示例#2
0
def p_try_catch(p):
    """
    try_catch : TRY stmt_list CATCH stmt_list END_STMT
              | TRY stmt_list END_STMT
    """
    assert isinstance(p[2], node.stmt_list)
    #assert isinstance(p[4],node.stmt_list)
    p[0] = node.try_catch(
        try_stmt=p[2],
        catch_stmt=node.stmt_list(),  # FIXME
        finally_stmt=node.stmt_list())
示例#3
0
def p_function(p):
    """
    function : func_decl
             | func_decl END_STMT
             | func_decl stmt_list
             | func_decl stmt_list END_STMT
    """
    # we backpatch the func_decl node
    func_decl = p[1]
    end_stmt = None
    if len(p) > 2 and isinstance(p[2], node.stmt_list):
        stmt_list = p[2]
        if len(p) == 4:
            end_stmt = p[3]
    else:
        stmt_list = node.stmt_list()
        if len(p) == 3:
            end_stmt = p[2]

    assert stmt_list.__class__ is node.stmt_list
    assert func_decl.__class__ is node.func_decl
    assert end_stmt is None or end_stmt == 'end', repr(end_stmt)
    func_decl.use_nargin = use_nargin
    func_decl.use_varargin = use_varargin

    # if not end_stmt:
    #    print "%s has no end" % func_decl.ident.name

    # for fns in [fn for fn in stmt_list if fn.__class__ is node.function and not fn.end]:
    #    print fn.head.ident.name

    if len(stmt_list) == 0 or stmt_list[-1].__class__ is not node.return_stmt:
        stmt_list.append(node.return_stmt(ret_expr))

    p[0] = node.function(head=func_decl, body=stmt_list, end=end_stmt)
示例#4
0
文件: parse.py 项目: RobBW/smop
def p_function(p):
    """
    function : func_decl
             | func_decl END_STMT
             | func_decl stmt_list
             | func_decl stmt_list END_STMT
    """
    # we backpatch the func_decl node
    func_decl = p[1]
    end_stmt = None
    if len(p) > 2 and isinstance(p[2], node.stmt_list):
        stmt_list = p[2]
        if len(p) == 4:
            end_stmt = p[3]
    else:
        stmt_list = node.stmt_list()
        if len(p) == 3:
            end_stmt = p[2]

    assert stmt_list.__class__ is node.stmt_list
    assert func_decl.__class__ is node.func_decl
    assert end_stmt is None or end_stmt == 'end', repr(end_stmt)
    func_decl.use_nargin = use_nargin
    func_decl.use_varargin = use_varargin

    # if not end_stmt:
    #    print "%s has no end" % func_decl.ident.name

    # for fns in [fn for fn in stmt_list if fn.__class__ is node.function and not fn.end]:
    #    print fn.head.ident.name

    if len(stmt_list) == 0 or stmt_list[-1].__class__ is not node.return_stmt:
        stmt_list.append(node.return_stmt(ret_expr))

    p[0] = node.function(head=func_decl, body=stmt_list, end=end_stmt)
示例#5
0
文件: parse.py 项目: RobBW/smop
def p_stmt_list_opt(p):
    """
    stmt_list_opt :
                  | stmt_list
    """
    if len(p) == 1:
        p[0] = node.stmt_list()
    else:
        p[0] = p[1]
示例#6
0
def p_stmt_list_opt(p):
    """
    stmt_list_opt :
                  | stmt_list
    """
    if len(p) == 1:
        p[0] = node.stmt_list()
    else:
        p[0] = p[1]
示例#7
0
文件: backend.py 项目: cephdon/smop
def _backend(self, level=0):
    s = "if %s:%s" % (self.cond_expr._backend(),
                      self.then_stmt._backend(level + 1))
    if self.else_stmt:
        # Eech. This should have been handled in the parser.
        if self.else_stmt.__class__ == node.if_stmt:
            self.else_stmt = node.stmt_list([self.else_stmt])
        s += "\n" + indent * level
        s += "else:%s" % self.else_stmt._backend(level + 1)
    return s
示例#8
0
def _backend(self,level=0):
    s = "if %s:%s" % (self.cond_expr._backend(),
                      self.then_stmt._backend(level+1))
    if self.else_stmt:
        # Eech. This should have been handled in the parser.
        if self.else_stmt.__class__ == node.if_stmt:
            self.else_stmt = node.stmt_list([self.else_stmt])
        s += "\n"+indent*level
        s += "else:%s" % self.else_stmt._backend(level+1)
    return s
示例#9
0
文件: parse.py 项目: RobBW/smop
def p_stmt_list(p):
    """
    stmt_list : stmt
              | stmt_list stmt
    """
    if len(p) == 2:
        p[0] = node.stmt_list([p[1]] if p[1] else [])
    elif len(p) == 3:
        p[0] = p[1]
        if p[2]:
            p[0].append(p[2])
    else:
        assert 0
示例#10
0
def p_stmt_list(p):
    """
    stmt_list : stmt
              | stmt_list stmt
    """
    if len(p) == 2:
        p[0] = node.stmt_list([p[1]] if p[1] else [])
    elif len(p) == 3:
        p[0] = p[1]
        if p[2]:
            p[0].append(p[2])
    else:
        assert 0
示例#11
0
文件: parse.py 项目: RobBW/smop
def p_top(p):
    """
    top :
        | stmt_list
    """
    if len(p) == 2:
        stmt_list = p[1]
    else:
        stmt_list = node.stmt_list()

    if len([fn for fn in stmt_list if fn.__class__ is node.function and not fn.end]):
        _pull_functions_toplevel(stmt_list, stmt_list)

    p[0] = stmt_list
示例#12
0
def p_elseif_stmt(p):
    """
    elseif_stmt :
                | ELSE stmt_list_opt
                | ELSEIF expr sep stmt_list_opt elseif_stmt
    """
    if len(p) == 1:
        p[0] = node.stmt_list()
    elif len(p) == 3:
        p[0] = p[2]
    elif len(p) == 6:
        p[0] = node.if_stmt(cond_expr=p[2], then_stmt=p[4], else_stmt=p[5])
    else:
        assert 0
示例#13
0
def p_top(p):
    """
    top : comments
        | comments stmt_list
    """
    if len(p) == 3:
        stmt_list = p[2]
    else:
        stmt_list = node.stmt_list()
    stmt_list.toplevel_comments = p[1]

    if len([fn for fn in stmt_list if fn.__class__ is node.function and not fn.end]):
        _pull_functions_toplevel(stmt_list, stmt_list)

    p[0] = stmt_list
示例#14
0
文件: parse.py 项目: RobBW/smop
def p_elseif_stmt(p):
    """
    elseif_stmt :
                | ELSE stmt_list_opt
                | ELSEIF expr sep stmt_list_opt elseif_stmt
    """
    if len(p) == 1:
        p[0] = node.stmt_list()
    elif len(p) == 3:
        p[0] = p[2]
    elif len(p) == 6:
        p[0] = node.if_stmt(cond_expr=p[2],
                            then_stmt=p[4],
                            else_stmt=p[5])
    else:
        assert 0
示例#15
0
def p_top(p):
    """
    top :
        | stmt_list
    """
    if len(p) == 2:
        stmt_list = p[1]
    else:
        stmt_list = node.stmt_list()

    if len([
            fn for fn in stmt_list
            if fn.__class__ is node.function and not fn.end
    ]):
        _pull_functions_toplevel(stmt_list, stmt_list)

    p[0] = stmt_list
示例#16
0
def p_top(p):
    """
    top : comments
        | comments stmt_list
    """
    if len(p) == 3:
        stmt_list = p[2]
    else:
        stmt_list = node.stmt_list()
    stmt_list.toplevel_comments = p[1]

    if len([
            fn for fn in stmt_list
            if fn.__class__ is node.function and not fn.end
    ]):
        _pull_functions_toplevel(stmt_list, stmt_list)

    p[0] = stmt_list
示例#17
0
文件: parse.py 项目: RobBW/smop
def p_case_list(p):
    """
    case_list :
              | CASE expr sep stmt_list_opt case_list
              | CASE expr error stmt_list_opt case_list
              | OTHERWISE stmt_list
    """
    if len(p) == 1:
        p[0] = node.stmt_list()
    elif len(p) == 3:
        assert isinstance(p[2],node.stmt_list)
        p[0] = p[2]
    elif len(p) == 6:
        p[0] = node.if_stmt(cond_expr=node.expr(op="==",
                                                args=node.expr_list([p[2]])),
                            then_stmt=p[4],
                            else_stmt=p[5])
        p[0].cond_expr.args.append(None) # None will be replaced using backpatch()
    else:
        assert 0
示例#18
0
def p_case_list(p):
    """
    case_list :
              | CASE expr sep stmt_list_opt case_list
              | CASE expr error stmt_list_opt case_list
              | OTHERWISE stmt_list
    """
    if len(p) == 1:
        p[0] = node.stmt_list()
    elif len(p) == 3:
        assert isinstance(p[2], node.stmt_list)
        p[0] = p[2]
    elif len(p) == 6:
        p[0] = node.if_stmt(cond_expr=node.expr(op="==",
                                                args=node.expr_list([p[2]])),
                            then_stmt=p[4],
                            else_stmt=p[5])
        p[0].cond_expr.args.append(
            None)  # None will be replaced using backpatch()
    else:
        assert 0