Пример #1
0
def test_create_table_and_get_symbol():
    symbol_table = SymbolTable(name="GLOBAL", parent_scope=None)
    symbol = VariableSymbol(name="foo", symbol_type="INT")
    symbol_table.add(symbol)

    result = symbol_table.lookup("foo")
    assert symbol == result
Пример #2
0
def test_get_symbol_from_parent():
    parent_scope = SymbolTable(name="GLOBAL", parent_scope=None)
    sub_scope = SymbolTable(name="GLOBAL", parent_scope=parent_scope)
    symbol = VariableSymbol(name="foo", symbol_type="INT")
    parent_scope.add(symbol)

    result = sub_scope.lookup("foo")
    assert symbol == result
Пример #3
0
def test_chained_array_declartion():
    source_file = (
        "DEF Foo()\n"
        "DECL INT bar1[1, 1], bar2[10]\n"
        "END"
    )

    awaited_ast = [
        SourceFile(name="Foo", statements=[
            FunctionDefinition(name="Foo", body=[
                VariableSymbol(name="bar1", symbol_type="INT",
                               dimensions=[1, 1]),
                VariableSymbol(name="bar2", symbol_type="INT",
                               dimensions=[10])])])]

    parser = Parser()
    parser.add_source_file("Foo", source_file)

    assert awaited_ast == parser.ast
Пример #4
0
def test_multiple_variable_declartion():
    source_file = (
        "DEF Foo()\n"
        "DECL INT foo\n"
        "DECL INT bar\n"
        "END"
    )

    awaited_ast = [
        SourceFile(name="Foo", statements=[
            FunctionDefinition(name="Foo", body=[
                VariableSymbol(name="foo", symbol_type="INT"),
                VariableSymbol(name="bar", symbol_type="INT")])
        ])]

    parser = Parser()
    parser.add_source_file("Foo", source_file)

    assert awaited_ast == parser.ast
Пример #5
0
def test_multi_dimensional_array_declartion():
    source_file = (
        "DEF Foo()\n"
        "DECL INT bar[1, 2, 3]\n"
        "END"
    )

    awaited_ast = [
        SourceFile(name="Foo", statements=[
            FunctionDefinition(name="Foo", body=[
                VariableSymbol(name="bar", symbol_type="INT",
                               dimensions=[1, 2, 3])])
        ])]

    parser = Parser()
    parser.add_source_file("Foo", source_file)

    assert awaited_ast == parser.ast
Пример #6
0
def test_declarations_and_statements():
    source_file = (
        "DEF Foo()\n"
        "DECL INT bar\n"
        "\n"
        "FooBar()"
        "END"
    )

    awaited_ast = [
        SourceFile(name="Foo", statements=[
            FunctionDefinition(name="Foo", body=[
                VariableSymbol(name="bar", symbol_type="INT",
                               dimensions=[0]),
                FunctionCall(name="FooBar")])])]

    parser = Parser()
    parser.add_source_file("Foo", source_file)

    assert awaited_ast == parser.ast