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_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_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_func_str(): ast = _def(v("t"), sig([]), blk([asg(v("a"), c(1)), ret(v("a"))])) assert str(ast) == "func t() {\n" + TAB + "a = 1\n" + TAB + "return a\n}"
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