Refactor Type System

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-06 07:46:18 -05:00
parent bc700509c1
commit c3fdfb62e8
11 changed files with 395 additions and 199 deletions
+33 -14
View File
@@ -29,47 +29,66 @@ import Macros
@Test func test_simple_parser() async throws {
let simple_parser_declaration = """
parser simple() {
parser main_parser() {
state start {
transition drop;
}
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
#expect(program.parsers.count == 1)
#expect(program.parsers[0].states.count == 1)
#expect(program.parsers[0].states[0].state_name == "start")
#expect(program.parsers[0].states[0].statements.count == 0)
#expect(parser.states.count == 1)
#expect(parser.states[0].state_name == "start")
#expect(parser.states[0].statements.count == 0)
}
@Test func test_simple_parser_syntax_error() async throws {
let simple_parser_declaration = """
parser simple() {
parser main_parser() {
state
transition drop;
}
}
};
"""
#expect(#RequireErrorResult(Error(withMessage: "Could not compile the P4 program"), Parser.Program(simple_parser_declaration)))
}
@Test func test_simple_parser_with_statement() async throws {
let simple_parser_declaration = """
parser simple() {
parser main_parser() {
state start {
true;
transition drop;
}
}
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
#expect(parser.states.count == 1)
#expect(parser.states[0].state_name == "start")
#expect(parser.states[0].statements.count == 1)
}
@Test func test_simple_parser_with_instantiation() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
true;
transition drop;
}
};
bool() main;
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
#expect(program.parsers.count == 1)
#expect(program.parsers[0].states.count == 1)
#expect(program.parsers[0].states[0].state_name == "start")
#expect(program.parsers[0].states[0].statements.count == 1)
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
#expect(parser.states.count == 1)
#expect(parser.states[0].state_name == "start")
#expect(parser.states[0].statements.count == 1)
}