def test_if_elif(): ast = ctrl([ _if(_is(v("a"), c(8)), blk([asg(v("b"), c(3))])), _if(_is(v("a"), c(4)), blk([asg(v("b"), c(2))])) ], blk()) parser = Parser(Tokenizer("if a is 8 { b = 3 } elif a is 4 { b = 2 }")) assert parser.statement() == ast
def test_var(): ast = v("a") parser = Parser(Tokenizer("a")) assert parser.statement() == ast
def test_simple(): ast = _def(v("t"), sig([]), blk([asg(v("a"), c(1))])) parser = Parser(Tokenizer("func t() {a = 1}")) assert parser.statement() == ast
def test_invalid_statement_in_block(): parser = Parser(Tokenizer("func t() {a() {} }")) with pytest.raises(InvalidSyntaxException): parser.statement()
def test_call_with_add(): ast = asg(v("a"), call(v("t"), [add(v("b"), c(2)), add(c(1), v("c"))])) parser = Parser(Tokenizer("a = t(b+2, 1+c)")) assert parser.statement() == ast
def test_call(): ast = asg(v("a"), call(v("t"), [v("b"), v("c")])) parser = Parser(Tokenizer("a = t(b, c)")) assert parser.statement() == ast
def test_with_params(): ast = _def(v("t"), sig([v("b"), v("c")]), blk([asg(v("a"), c(1))])) parser = Parser(Tokenizer("func t(b, c) {a = 1}")) assert parser.statement() == ast
def test_simple_return_var(): ast = _def(v("t"), sig([]), blk([asg(v("a"), c(1)), ret(v("a"))])) parser = Parser(Tokenizer("func t() {a = 1\nreturn a}")) assert parser.statement() == ast
def test_simple_return(): ast = _def(v("t"), sig([]), blk([ret(c(1))])) parser = Parser(Tokenizer("func t() {return 1}")) assert parser.statement() == ast
def test_if(): ast = ctrl( [_if(_is(getitem(v("a"), c(0)), c(8)), blk([asg(v("b"), c(3))]))], blk()) parser = Parser(Tokenizer("if a[0] is 8 { b = 3 }")) assert parser.statement() == ast
def test_literals(var, var_str, exprs, expr_strs): ast = asg(var, Array(exprs)) s = "{} = [{}]".format(var_str, ",".join(expr_strs)) parser = Parser(Tokenizer(s)) assert parser.statement() == ast
def test_setitem(var, expr, val, var_str, expr_str, val_str): ast = setitem(var, expr, val) parser = Parser(Tokenizer(var_str + "[" + expr_str + "]" + "=" + val_str)) assert parser.statement() == ast
def test_simple(): ast = _for(asg(v("i"), c(0)), lt(v("i"), c(10)), asg(v("i"), add(v("i"), c(1))), blk([asg(v("a"), c(1))])) parser = Parser(Tokenizer("for i = 0; i < 10; i = i + 1 {a = 1}")) assert parser.statement() == ast
def test_array(): ast = _for(asg(v("i"), c(0)), lt(v("i"), c(10)), asg(v("i"), add(v("i"), c(1))), blk([asg(v("a"), getitem(v("b"), v("i")))])) parser = Parser(Tokenizer("for i = 0; i < 10; i = i + 1 {a = b[i]}")) assert parser.statement() == ast
def test_empty_if_with_new_line(): ast = ctrl([_if(_is(v("a"), c(8)), blk())], blk()) parser = Parser(Tokenizer("if a is 8 { \n }")) assert parser.statement() == ast
def test_if_with_new_lines(): ast = ctrl([_if(_is(v("a"), c(8)), blk([asg(v("b"), c(3))]))], blk()) parser = Parser(Tokenizer("if a is 8 {\nb = 3\n}")) assert parser.statement() == ast