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_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_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_func_call(): ast = asg(v("a"), call(v("t"), [add(v("b"), c(2)), add(c(1), v("c"))])) assert "a = t(b + 2, 1 + c)" == str(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_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_if_else(): ast = ctrl([_if(_is(v("a"), c(8)), blk([asg(v("b"), c(3))]))], blk([asg(v("b"), c(2))])) parser = Parser(Tokenizer("if a is 8 { b = 3 } else { b = 2 }")) 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