compiler, runtime, common: Support (in)out Parameters

When a function is called, if there is an (in)out parameter,
make sure that updated values are propogated to the calling
function.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-16 06:58:45 -04:00
parent 94086c8e17
commit 82c125e4d1
14 changed files with 322 additions and 139 deletions
+29 -1
View File
@@ -172,6 +172,34 @@ import TreeSitterP4
x = true;
};
"""
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration)))
#expect(
#RequireErrorResult(
Error(
withMessage:
"{56, 21}: Failed to parse a statement element: {63, 9}: Failed to parse a statement element: {63, 1}: Cannot assign value with type Boolean to identifier x that is in parameter"
),
Program.Compile(simple_parser_declaration))
)
}
@Test func test_function_declaration_with_parameters_and_direction_struct() async throws {
let simple_parser_declaration = """
struct Testing {
bool yesno;
int count;
};
bool functionb(in Testing x, out string y, inout int z) {
x.yesno = true;
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"{113, 27}: Failed to parse a statement element: {120, 15}: Failed to parse a statement element: {120, 7}: Cannot assign to field yesno of x that is in parameter"
),
Program.Compile(simple_parser_declaration))
)
}
@@ -83,6 +83,35 @@ import TreeSitterP4
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
}
@Test func test_function_call_scoped_name_collision_inout() async throws {
let simple_parser_declaration = """
bool functionb(inout bool c) {
c = true;
return c;
};
parser main_parser() {
state start {
bool c = false;
bool b = functionb(c);
transition select (c) {
false: reject;
true: accept;
};
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
}
@Test func test_function_call_integer_return_value() async throws {
let simple_parser_declaration = """
int functionb(int c) {
+2 -2
View File
@@ -327,7 +327,7 @@ import TreeSitterP4
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int to field with type Boolean"
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int to field yesno of type Boolean"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
@@ -457,7 +457,7 @@ import TreeSitterP4
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field with type Int"
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)