compiler: Refactor Compiler To Remove Ambiguities
Continuous Integration / Grammar Tests (push) Successful in 39s
Continuous Integration / Library Format Tests (push) Successful in 1m51s
Continuous Integration / Library Tests (push) Failing after 4m44s

There were significant overlaps in the names of data structures
between the compiler and the language that made it necessary
to litter the code with P4Lang.xxxx. This refactor removes that
requirement in most places (Parser is ambiguous wherever TreeSitter
is used -- cannot avoid that!)

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-27 12:59:29 -04:00
parent 61d8f601e8
commit 294f76acd4
39 changed files with 735 additions and 699 deletions
@@ -44,7 +44,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -73,7 +73,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -101,7 +101,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -128,7 +128,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -154,7 +154,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -180,7 +180,7 @@ import TreeSitterP4
};
"""
let error = try #UseErrorResult(Program.Compile(simple_parser_declaration))
let error = try #UseErrorResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(error.msg().contains("{29, 12}: Type of expression in return statement (Boolean) is not compatible with function return type (Int (width: Infinite))"))
}
@@ -39,7 +39,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -61,7 +61,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -83,7 +83,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -105,7 +105,7 @@ import TreeSitterP4
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -134,7 +134,7 @@ import TreeSitterP4
withMessage:
"Error(s) parsing select cases: {81, 4}: Key expression of type Boolean is not compatible with selector type Int (width: Infinite)"
),
Program.Compile(simple_parser_declaration)))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
}
@Test func test_select_expression_selection_order() async throws {
@@ -149,7 +149,7 @@ import TreeSitterP4
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -171,7 +171,7 @@ import TreeSitterP4
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([
@@ -192,7 +192,7 @@ import TreeSitterP4
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([