def test_call_with_named_args(self): r = self.parse('x(a=1, b=2)') assert r == ast.Call(ast.Identifier('x'), [], [ ast.NamedArg('a', ast.Number(1)), ast.NamedArg('b', ast.Number(2)), ]) r = self.parse('x(1, b=2)') assert r == ast.Call(ast.Identifier('x'), [ast.Number(1)], [ ast.NamedArg('b', ast.Number(2)), ]) self.parse_bad('x(1=b)') self.parse_bad('x(1, a=2, 3')
def test_various_kinds_of_calls(self): r = self.parse('x(1, 2, 3)') assert r == ast.Call( ast.Identifier('x'), [ast.Number(1), ast.Number(2), ast.Number(3)]) r = self.parse('(1)(2)') assert r == ast.Call(ast.Number(1), [ast.Number(2)])
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_var(self): r = self.parse(''' var x; x = 3; x = x + 1; ''') assert r == [ ast.VarDeclaration(['x']), ast.Assignment('x', ast.Number(3)), ast.Assignment('x', ast.BinOp('+', ast.Identifier('x'), ast.Number(1))) ]
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_while_loop(self): r = self.parse(''' var i, s; i = 0; while i < 10 { i = i + 1; s = s + i; } return s; ''') assert r == [ ast.VarDeclaration(['i', 's']), ast.Assignment('i', ast.Number(0)), ast.While(ast.BinOp('<', ast.Identifier('i'), ast.Number(10)), [ ast.Assignment( 'i', ast.BinOp('+', ast.Identifier('i'), ast.Number(1))), ast.Assignment( 's', ast.BinOp('+', ast.Identifier('s'), ast.Identifier('i'))) ]), ast.Return(ast.Identifier('s')) ]
def atom_identifier(state, p): return ast.Identifier(p[0].getstr(), srcpos=sr(p))
def expression_identifier(state, p): return ast.Identifier(p[0].getstr())