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
+6 -6
View File
@@ -28,7 +28,7 @@ public struct ExpressionStatement {
public struct Program {
public var types: [P4Type] = Array()
public var externs: [P4Type] = Array()
public var instances: [P4QualifiedType] = Array()
public var instances: [P4Value] = Array()
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)``
public typealias TypeFilter = (P4QualifiedType) -> Bool
@@ -36,7 +36,7 @@ public struct Program {
public typealias DataTypeFilter = (P4Type) -> Bool
/// Retrieve global instances in the compiled P4 program.
public func InstancesWithTypes() -> [P4QualifiedType] {
public func InstancesWithTypes() -> [P4Value] {
return self.instances
}
@@ -52,9 +52,9 @@ public struct Program {
///
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
///
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4QualifiedType] {
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Value] {
return self.instances.filter { instance in
filter(instance)
filter(instance.type())
}
}
@@ -112,8 +112,8 @@ public struct Program {
}
public func find_parser(withName name: Identifier) -> Result<Parser> {
for instance in self.instances {
guard let parser = instance.baseType() as? Parser else {
for instance in self.types {
guard let parser = instance as? Parser else {
continue
}
if parser.name == name {