Ejemplo n.º 1
0
def test_parse_and_ast_assignment():
    token_tree, remainder = parse(g, simple_assign)
    # Just check it does not crash for now.
    my_ast = to_ast(token_tree)

    token_tree, remainder = parse(g, assign_string)
    # Just check it does not crash for now.
    my_ast = to_ast(token_tree)
Ejemplo n.º 2
0
def test_ast_binop_precedence():
    binop = "(1 + 2) * 3"
    token_tree, remainder = parse(g, binop)
    assert remainder == '', 'Parsing failed, check parser tests.'

    ast = to_ast(token_tree)
    nodes = list(ast.walk())

    expected_order = [BinOp.MULTIPLY, BinOp.ADD]
    assert [node.operation for node in nodes if isinstance(node, BinOp)] == expected_order

    binop_2 = "1 + 2 * 3"
    ast = get_ast(binop_2)
    nodes = list(ast.walk())
    expected_order = [BinOp.ADD, BinOp.MULTIPLY]
    assert [node.operation for node in nodes if isinstance(node, BinOp)] == expected_order

    binop_3 = "1 * 2 + 3"
    ast = get_ast(binop_3)
    nodes = list(ast.walk())
    expected_order = [BinOp.ADD, BinOp.MULTIPLY]
    assert [node.operation for node in nodes if isinstance(node, BinOp)] == expected_order

    binop_3 = "4 / 3 * 2"
    ast = get_ast(binop_3)
    nodes = list(ast.walk())
    expected_order = [BinOp.MULTIPLY, BinOp.DIVIDE]
    assert [node.operation for node in nodes if isinstance(node, BinOp)] == expected_order
Ejemplo n.º 3
0
def test_simple_module_function_to_llvm():
    token_tree, remainder = parse(g, simple_function)
    my_ast = to_ast(token_tree)

    ir_code = to_llvm(my_ast)

    expected_ir_code = """define i64 @"fibo"() 
{
entry:
  ret i64 42
}"""
    assert expected_ir_code.strip() in str(ir_code)
Ejemplo n.º 4
0
def test_function_node_to_llvm():
    token_tree, remainder = parse(g, simple_function)

    my_ast = to_ast(token_tree)
    # Let's get the function node
    func_node = None
    for node in my_ast.walk():
        if isinstance(node, Function):
            func_node = node
            break

    module = ir.Module('test')
    ir_code = function_to_llvm(func_node, module)

    expected_ir_code = """define i64 @"fibo"() 
{
entry:
  ret i64 42
}"""
    assert expected_ir_code.strip() in str(ir_code)
Ejemplo n.º 5
0
def compile(source_file):
    token_list, remainder = parse(g, source_file.read())
    assert remainder.strip() == '', 'Failed to parse!'
    ast = to_ast(token_list)
    print(to_llvm(ast))
Ejemplo n.º 6
0
def get_ast(source):
    token_tree, remainder = parse(g, source)
    assert remainder == '', 'Parsing failed, check parser tests.'

    return to_ast(token_tree)