compiler, language, runtime: Separate Parser Type From Instances
Continuous Integration / Grammar Tests (push) Successful in 4m2s
Continuous Integration / Library Format Tests (push) Successful in 5m0s
Continuous Integration / Library Tests (push) Successful in 8m1s

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:
Will Hawkins
2026-05-27 05:41:23 -04:00
parent 925f20a13b
commit 61d8f601e8
36 changed files with 1058 additions and 796 deletions
+20 -15
View File
@@ -105,7 +105,7 @@ extension FunctionDeclaration: CompilableDeclaration {
var function_scope = context.instances.enter()
for parameter in function_parameters.parameters {
function_scope = function_scope.declare(
identifier: parameter.name, withValue: parameter.type)
identifier: parameter.name, withValue: (parameter.type, .none))
}
let maybe_function_body = BlockStatement.Compile(
@@ -342,7 +342,7 @@ extension P4Lang.Parser: CompilableDeclaration {
for parameter in parameter_list.parameters {
current_context = current_context.update(
newInstances: current_context.instances.declare(
identifier: parameter.name, withValue: parameter.type))
identifier: parameter.name, withValue: (parameter.type, .none)))
}
walker.next()
@@ -380,6 +380,7 @@ extension P4Lang.Parser: CompilableDeclaration {
return .Error(Error(withMessage: "Missing parser states in parser declaration"))
}
/// TODO: Handle extern parsers.
switch Parser.Compile(
withName: parser_name!, withParameters: parameter_list, node: current_node!,
withContext: current_context)
@@ -392,10 +393,11 @@ extension P4Lang.Parser: CompilableDeclaration {
(
parser_declaration,
context.extern_context
? context
: context.update(
newInstances: updated_context.instances.declare(
identifier: parser.name, withValue: parser_declaration.identifier.type))
? updated_context.update(
newExterns: updated_context.externs.declare(
identifier: parser.name, withValue: parser_declaration))
: updated_context.update(
newTypes: updated_context.types.declare(identifier: parser.name, withValue: parser))
))
case Result.Error(let error): return .Error(error)
}
@@ -450,7 +452,7 @@ extension Control: CompilableDeclaration {
var control_scope = local_context.instances.enter()
for parameter in control_parameters.parameters {
control_scope = control_scope.declare(
identifier: parameter.name, withValue: parameter.type)
identifier: parameter.name, withValue: (parameter.type, .none))
}
local_context = local_context.update(newInstances: control_scope)
@@ -543,14 +545,15 @@ extension Control: CompilableDeclaration {
))
}
let control = Control(
named: control_name, withParameters: control_parameters, withTable: tables[0],
withActions: Actions(withActions: actions), withApply: apply)
let declared_control =
Declaration(
TypedIdentifier(
id: control_name,
withType: P4QualifiedType(
Control(
named: control_name, withParameters: control_parameters, withTable: tables[0],
withActions: Actions(withActions: actions), withApply: apply))))
withType: P4QualifiedType(control)))
// Don't forget to add the newly declared Control to the instance that we were given
// (and not the one that we entered to do the parsing of this Control).
@@ -558,10 +561,12 @@ extension Control: CompilableDeclaration {
(
declared_control,
context.extern_context
? context
? context.update(
newTypes: context.externs.declare(
identifier: control_name, withValue: declared_control))
: context.update(
newInstances: context.instances.declare(
identifier: control_name, withValue: declared_control.identifier.type))
newTypes: context.types.declare(
identifier: control_name, withValue: control))
))
}
}
@@ -638,7 +643,7 @@ extension Action: Compilable {
var function_scope = context.instances.enter()
for parameter in action_parameters.parameters {
function_scope = function_scope.declare(
identifier: parameter.name, withValue: parameter.type)
identifier: parameter.name, withValue: (parameter.type, .none))
}
let maybe_action_body = BlockStatement.Compile(