Ejemplo n.º 1
0
 def test_assignment(self):
     assert parse("a = 123") == ast.Block(
         [ast.Assign(ast.Symbol("a"), ast.Vector([ast.FloatValue(123.)]))])
     assert parse("a <- 123") == ast.Block(
         [ast.Assign(ast.Symbol("a"), ast.Vector([ast.FloatValue(123.)]))])
     assert parse("123 -> a") == ast.Block(
         [ast.Assign(ast.Symbol("a"), ast.Vector([ast.FloatValue(123.)]))])
Ejemplo n.º 2
0
 def test_parses_block(self):
     source = dedent("""\
         {
             "String literal"
         }
         """)
     assert parse(source) == ast.Block(
         [ast.Block([ast.Vector([ast.CharValue(u"String literal")])])])
Ejemplo n.º 3
0
 def test_funcdef(self):
     assert parse("function() x") == ast.Block(
         [ast.Function(formals=ast.FormalList(), body=ast.Symbol("x"))])
     assert parse("function(x) x") == ast.Block([
         ast.Function(formals=ast.FormalList([(ast.Symbol("x"), None)]),
                      body=ast.Symbol("x"))
     ])
     assert parse("function(x) { x + 3 }") == ast.Block([
         ast.Function(formals=ast.FormalList([(ast.Symbol("x"), None)]),
                      body=ast.Block([
                          ast.BinaryOperation(
                              "+", ast.Symbol("x"),
                              ast.Vector([ast.FloatValue(3.)]))
                      ]))
     ])
     assert parse("function(x, y=0) { x + y }") == ast.Block([
         ast.Function(formals=ast.FormalList([
             (ast.Symbol("x"), None),
             (ast.Symbol("y"), ast.Vector([ast.FloatValue(0.)]))
         ]),
                      body=ast.Block([
                          ast.BinaryOperation("+", ast.Symbol("x"),
                                              ast.Symbol("y"))
                      ]))
     ])
Ejemplo n.º 4
0
 def test_constant(self):
     assert parse("3") == ast.Block([ast.Vector([ast.FloatValue(3.)])])
     assert parse("3e3") == ast.Block([ast.Vector([ast.FloatValue(3000.)])])
     assert parse("5e-1") == ast.Block([ast.Vector([ast.FloatValue(0.5)])])
     assert parse("NA") == ast.Block(
         [ast.Vector([ast.BoolValue(False, na=True)])])
Ejemplo n.º 5
0
 def test_unicode_symbol(self):
     assert parse(u"👺 = 5".encode("utf-8")) == ast.Block([
         ast.Assign(ast.Symbol(u"👺"), ast.Vector([ast.FloatValue(5.)]))
     ])
Ejemplo n.º 6
0
 def test_unicode_string(self):
     source = u'"👺"'
     assert parse(source.encode("utf-8")) == ast.Block([
         # strip quotes in a narrow-python friendly way
         ast.Vector([ast.CharValue(source[1:-1])])
     ])
Ejemplo n.º 7
0
 def test_binary_operation(self):
     assert parse("a + b") == ast.Block(
         [ast.BinaryOperation("+", ast.Symbol("a"), ast.Symbol("b"))])
Ejemplo n.º 8
0
 def test_simple_exprs(self):
     assert parse("(3)") == ast.Block([ast.Vector([ast.FloatValue(3.)])])
     assert parse("3;;\n3") == ast.Block([
         ast.Vector([ast.FloatValue(3.)]),
         ast.Vector([ast.FloatValue(3.)])
     ])
Ejemplo n.º 9
0
 def exprlist_none(self, p):
     # type: (List[Token]) -> ast.Block
     return ast.Block([])