compiler, language, runtime: Separate Parser Type From Instances
In P4, parsers are considered types. Those parsers are instantiated. The instantiated parsers are values. Previously, gp4 treated a parser type and a parser value as identical. This PR makes that difference clear _and_ sets the stage for the future. TODO: Make the same distinction between control and action types and values. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -179,13 +179,13 @@ import TreeSitterP4
|
||||
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 5w5 == specific_width_int == true
|
||||
// true == true
|
||||
// true
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer_specific_width_int_type2() async throws {
|
||||
@@ -204,16 +204,15 @@ import TreeSitterP4
|
||||
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 5w6 == specific_width_int == false
|
||||
// false == false
|
||||
// true
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
|
||||
@Test func test_expression_in_declaration_initializer() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
@@ -228,13 +227,13 @@ import TreeSitterP4
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 5 == 5 == true
|
||||
// true == true
|
||||
// true
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer2() async throws {
|
||||
@@ -251,13 +250,13 @@ import TreeSitterP4
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 5 == 5 == true
|
||||
// true == false
|
||||
// false
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
|
||||
#expect(state_result == P4Lang.reject)
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer_false() async throws {
|
||||
@@ -274,13 +273,13 @@ import TreeSitterP4
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 6 == 5 == true
|
||||
// false == true
|
||||
// false
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
|
||||
#expect(state_result == P4Lang.reject)
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer_false2() async throws {
|
||||
@@ -297,13 +296,13 @@ import TreeSitterP4
|
||||
"""
|
||||
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
|
||||
let runtime = try #UseOkResult(
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
|
||||
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
|
||||
let (state_result, _) = try! #UseOkResult(runtime.run())
|
||||
|
||||
// 6 == 5 == false
|
||||
// false == false
|
||||
// true
|
||||
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
|
||||
#expect(state_result == P4Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_expression_in_declaration_initializer_invalid_types() async throws {
|
||||
@@ -341,17 +340,18 @@ import TreeSitterP4
|
||||
}
|
||||
};
|
||||
"""
|
||||
var test_types = VarTypeScopes().enter()
|
||||
test_types = test_types.declare(
|
||||
var test_declarations = StaticVarValueScopes().enter()
|
||||
test_declarations = test_declarations.declare(
|
||||
identifier: Identifier(name: "ta"),
|
||||
withValue: P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))))
|
||||
withValue: (P4QualifiedType(P4Array(withValueType: P4QualifiedType(P4Int()))), .none))
|
||||
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(
|
||||
withMessage:
|
||||
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type Int (width: Infinite)"
|
||||
),
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_types)))
|
||||
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)))
|
||||
}
|
||||
|
||||
@Test func test_simple_compiler_parser_parameters_invalid_types() async throws {
|
||||
|
||||
Reference in New Issue
Block a user