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
Beispiel #2
0
    def compile(self, prog, features=Features.ALL):
        """Currently this compiler simply returns an interpreter instead of compiling
        TODO: Write this compiler to increase LPProg run speed and to prevent exceeding maximum recursion depth

        Args:
            prog (str): A string containing the program.
            features (FeatureSet): The set of features to enable during compilation.

        Returns:
            LPProg
        """
        return LPProg(
            Parser(Tokenizer(prog, features), features).program(), features)
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_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_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():
    ast = _def(v("t"), sig([]), blk([ret(c(1))]))

    parser = Parser(Tokenizer("func t() {return 1}"))
    assert parser.statement() == ast
Beispiel #7
0
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
Beispiel #8
0
def test_getitem(var, expr, var_str, expr_str):
    ast = getitem(var, expr)
    parser = Parser(Tokenizer(var_str + "[" + expr_str + "]"))
    assert parser.variable() == ast
def test_multiple_same_priority_bins_4():
    ast1 = sub(c(1), add(v("b"), sub(c(4), v("a"))))
    ast2 = sub(add(sub(c(1), v("b")), c(4)), v("a"))
    parser = Parser(Tokenizer("1-b+4-a"))
    expr = parser.expression()
    assert expr == ast1 or expr == ast2
def test_multiple_same_priority_bins_2():
    ast1 = add(c(1), add(v("b"), add(c(4), v("a"))))
    ast2 = add(add(add(c(1), v("b")), c(4)), v("a"))
    parser = Parser(Tokenizer("1+b+4+a"))
    expr = parser.expression()
    assert expr == ast1 or expr == ast2
def test_const_bin_var():
    ast = add(c(1), v("b"))
    parser = Parser(Tokenizer("1+b"))
    assert parser.expression() == ast
def test_var_bin_var():
    ast = add(v("a"), v("b"))
    parser = Parser(Tokenizer("a+b"))
    assert parser.expression() == ast
def test_unary_var():
    ast = _not(v("b"))
    parser = Parser(Tokenizer("not b"))
    assert parser.expression() == ast
def test_eat_wrong_type_error():
    parser = Parser(Tokenizer("not b+1"))
    with pytest.raises(Exception):
        parser.eat(TokenTypes.INT)
    parser.eat(TokenTypes.NOT)
    with pytest.raises(Exception):
        parser.eat(TokenTypes.INT)
    parser.eat(TokenTypes.VAR)
    with pytest.raises(Exception):
        parser.eat(TokenTypes.INT)
    parser.eat(TokenTypes.ADD)
    with pytest.raises(Exception):
        parser.eat(TokenTypes.VAR)
    parser.eat(TokenTypes.INT)
def test_error():
    parser = Parser(Tokenizer(""))
    with pytest.raises(Exception) as err:
        parser.error("Bla bla bla")
    assert "Invalid syntax: Bla bla bla" in str(err.value)
def test_binary_ops_all_levels(op, left, right, op_str, left_str, right_str):
    ast = op(left, right)
    s = " ".join((left_str, op_str, right_str))
    parser = Parser(Tokenizer(s))
    assert parser.expression() == ast
def test_binary_ops_level_4_and_below(op, left, right, op_str, left_str,
                                      right_str):
    ast = op(left, right)
    s = left_str + op_str + right_str
    parser = Parser(Tokenizer(s))
    assert parser.expression() == ast
def test_differing_priority_bins_1():
    ast = add(c(1), div(v("b"), c(4)))
    parser = Parser(Tokenizer("1+b/4"))
    assert parser.expression() == ast
Beispiel #19
0
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_uniary_ops(op, operand, op_str, operand_str):
    ast = op(operand)
    s = op_str + " " + operand_str
    parser = Parser(Tokenizer(s))
    assert parser.expression() == ast
Beispiel #21
0
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_differing_priority_bins_2():
    ast = add(div(c(1), v("b")), c(4))
    parser = Parser(Tokenizer("1/b+4"))
    assert parser.expression() == 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_parens():
    ast = div(add(c(1), v("b")), c(4))
    parser = Parser(Tokenizer("(1+b)/4"))
    assert parser.expression() == 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_tailing_new_line():
    ast = add(v("a"), v("b"))
    parser = Parser(Tokenizer("a+b\n"))
    assert parser.expression() == ast
    assert parser.cur_token == EOF
def test_invalid_statement_in_block():
    parser = Parser(Tokenizer("func t() {a() {} }"))
    with pytest.raises(InvalidSyntaxException):
        parser.statement()
def test_lt_1():
    ast = lt(div(add(c(1), c(2)), c(3)), add(c(4), c(5)))
    parser = Parser(Tokenizer("(1+2)/3 < 4 + 5"))
    assert parser.expression() == ast
def test_var():
    ast = v("a")
    parser = Parser(Tokenizer("a"))
    assert parser.statement() == ast
def test_gt_2():
    ast = gt(div(add(c(1), c(2)), c(3)), add(c(1), c(2)))
    parser = Parser(Tokenizer("(1+2)/3 > 1 + 2"))
    assert parser.expression() == ast