예제 #1
0
def test_string():
    assert parse('"foo";') == Block([Stmt(ConstantString("foo"))])
    assert parse('"foo" + "bar";') == Block(
        [
            Stmt(
                BinOp(
                    "+",
                    ConstantString("foo"),
                    ConstantString("bar")
                )
            )
        ]
    )
예제 #2
0
def test_float():
    assert parse('1.0;') == Block([Stmt(ConstantFloat(1.0))])
    assert parse('0.5;') == Block([Stmt(ConstantFloat(0.5))])
    assert parse('0.0;') == Block([Stmt(ConstantFloat(0.0))])
    assert parse('-1.0;') == Block([Stmt(ConstantFloat(-1.0))])
    assert parse('10.0;') == Block([Stmt(ConstantFloat(10.0))])
    assert parse('.1;') == Block([Stmt(ConstantFloat(.1))])
    assert parse('1.0e5;') == Block([Stmt(ConstantFloat(1.0e5))])
    assert parse('1.0E-5;') == Block([Stmt(ConstantFloat(1.0E-5))])
    assert parse('1.0e+11;') == Block([Stmt(ConstantFloat(1.0e11))])
예제 #3
0
    def runstring(self, s):
        ast = parse(s)

        bc = self.compiler.compile(ast)
        if self.debug:  # pragma: no cover
            print bc.dump()

        return self.run(bc)
예제 #4
0
def test_multiple_statements():
    assert parse('''
    1 + 2;
    c;
    e;
    ''') == Block([Stmt(BinOp("+", ConstantInt(1), ConstantInt(2))),
                   Stmt(Variable('c')),
                   Stmt(Variable('e'))])
예제 #5
0
def interpret(source):
    return Interpreter().run(Compiler().compile(parse(source)))
예제 #6
0
 def check_compile(self, source, expected):
     bc = compile_ast(parse(source))
     self.check_bytecode(bc, expected)
     return bc
예제 #7
0
def test_parse_basic():
    assert parse('13;') == Block([Stmt(ConstantInt(13))])
    assert parse('1 + 2;') == Block([Stmt(BinOp("+", ConstantInt(1),
                                                ConstantInt(2)))])
    assert parse('1 + a;') == Block([Stmt(BinOp('+', ConstantInt(1),
                                                Variable('a')))])
예제 #8
0
def test_print():
    assert parse("print x;") == Block([Print(Variable("x"))])
예제 #9
0
def test_func_call():
    assert parse("foo();") == Block([Stmt(Call(Variable("foo")))])
예제 #10
0
def test_func_decl():
    assert parse("func foo() { print 1; }") == Block([
        Function("foo", Block([
            Print(ConstantInt(1))
        ]))
    ])
예제 #11
0
def test_if():
    assert parse("if (1) { a; }") == Block([If(ConstantInt(1),
                                               Block([Stmt(Variable("a"))]))])
예제 #12
0
def test_while():
    assert parse('while (1) { a = 3; }') == Block(
        [While(ConstantInt(1), Block([Assignment('a', ConstantInt(3))]))])
예제 #13
0
def test_assignment():
    assert parse('a = 3;') == Block([Assignment('a', ConstantInt(3))])