Beispiel #1
0
def test_operators():
    cases = [
        ('1 + 1', [BinOp(Num(1), '+', Num(1))]),
    ]
    for string, ast in cases:
        tokens = lexer.tokenize(string)
        assert bcparser.parse(tokens) == ast
Beispiel #2
0
def compile_bytecode(name):
    with open(name, 'r') as f:
        text = f.read()
    tokens = tokenize(text)
    ast = parse(tokens)
    fns, stack = codegen(ast)
    return fns, stack
Beispiel #3
0
def test_if_else():
    cases = [
        ('if 1 {}', [IfElse(Num(1), [], None)]),
        ('if 1 { return 0; }', [IfElse(Num(1), [Return(Num(0))], None)]),
        ('if 1 {} else {}', [IfElse(Num(1), [], [])]),
        ('if 1 {} else if 0 {}',
         [IfElse(Num(1), [], [IfElse(Num(0), [], None)])]),
        ('if 1 {} else if 0 {} else {}',
         [IfElse(Num(1), [], [IfElse(Num(0), [], [])])]),
    ]
    for string, ast in cases:
        tokens = lexer.tokenize(string)
        assert bcparser.parse(tokens) == ast
Beispiel #4
0
def test_basic_operators():
    ops = ['+', '-', '*', '/', '^', '<', '>', '<=', '>=', '==', '!=']
    for op in ops:
        tokens = lexer.tokenize('1 {} 1'.format(op))
        assert bcparser.parse(tokens) == [BinOp(Num(1), op, Num(1))]