def arglist_non_empty(state, p): if len(p) == 5: vars = ([ast.Var(p[1].getstr(), p[2], None, p[1].getsrcpos())] + p[3].get_vars()) else: vars = ([ast.Var(p[1].getstr(), p[4], p[3], p[1].getsrcpos())] + p[5].get_vars()) return ast.ArgList(vars, srcpos=sr(p))
def var_declaration(state, p): if len(p) == 5: vars = [ast.Var(p[1].getstr(), p[2], None, srcpos=sr([p[1], p[2]]))] + \ p[3].get_vars() else: vars = [ast.Var(p[1].getstr(), p[4], p[3], srcpos=sr([p[1], p[2], p[3]]))] + \ p[5].get_vars() return ast.VarDeclaration(vars, srcpos=sr(p))
def test_function_declaration_args(self): r = self.parse(''' def foo(a0, a1) { } ''') expected = ast.Program([ ast.Function('foo', [ast.Var('a0', ast.NoTypeDecl(), None), ast.Var('a1', ast.NoTypeDecl(), None)], [ ], lineno=1) ]) assert r == expected
def test_var(self): r = self.parse(''' let x; x = 3; x = x + 1; ''') assert r == [ ast.VarDeclaration([ast.Var('x', ast.NoTypeDecl(), None)]), ast.Assignment('x', ast.Number(3)), ast.Assignment('x', ast.BinOp('+', ast.Identifier('x'), ast.Number(1), oppos=(46, 47)))]
def test_while_loop(self): r = self.parse(''' let i, s; i = 0; while i < 10 { i = i + 1; s = s + i; } return s; ''') assert r == [ ast.VarDeclaration([ast.Var('i', ast.NoTypeDecl(), None), ast.Var('s', ast.NoTypeDecl(), None)]), ast.Assignment('i', ast.Number(0)), ast.While( ast.BinOp('<', ast.Identifier('i'), ast.Number(10), oppos=(51, 52)), [ ast.Assignment('i', ast.BinOp( '+', ast.Identifier('i'), ast.Number(1), oppos=(72, 73))), ast.Assignment('s', ast.BinOp( '+', ast.Identifier('s'), ast.Identifier('i'), oppos=(91, 92)))]), ast.Return(ast.Identifier('s'))]
def test_function_declaration(self): r = self.parse(''' def foo() { let x; } def main() { } ''') expected = ast.Program([ ast.Function('foo', [], [ ast.VarDeclaration([ast.Var('x', ast.NoTypeDecl(), None)]) ], lineno=1), ast.Function('main', [], [], lineno=4) ]) assert r == expected
def arglist_non_empty(state, p): vars = [ast.Var(p[1].getstr(), p[2], p[1].getsrcpos()) ] + p[3].get_vars() return ast.ArgList(vars, srcpos=sr(p))
def var_declaration_basic(state, p): vars = [ast.Var(p[1].getstr(), p[2], srcpos=sr([p[1], p[2]]))] + \ p[3].get_vars() return ast.VarDeclaration(vars, srcpos=sr(p))