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:
@@ -24,18 +24,30 @@ import TreeSitterP4
|
||||
|
||||
public struct Program {
|
||||
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
|
||||
return Program.Compile(source, withGlobalInstances: .none, withGlobalTypes: .none, withFFIs: [])
|
||||
|
||||
// Certain names are always in scope during compilation.
|
||||
var globals = StaticVarValueScopes().enter()
|
||||
globals = globals.declare(
|
||||
identifier: accept.state().getName(),
|
||||
withValue: (P4QualifiedType(accept.type()), P4Value(accept))
|
||||
)
|
||||
.declare(
|
||||
identifier: reject.state.getName(),
|
||||
withValue: (P4QualifiedType(reject.type()), P4Value(reject)))
|
||||
|
||||
return Program.Compile(
|
||||
source, withGlobalInstances: globals, withGlobalTypes: .none, withFFIs: [])
|
||||
}
|
||||
|
||||
public static func Compile(
|
||||
_ source: String, withGlobalInstances globalInstances: VarTypeScopes
|
||||
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes
|
||||
) -> Result<P4Lang.Program> {
|
||||
return Program.Compile(
|
||||
source, withGlobalInstances: globalInstances, withGlobalTypes: .none, withFFIs: [])
|
||||
}
|
||||
|
||||
public static func Compile(
|
||||
_ source: String, withGlobalInstances globalInstances: VarTypeScopes?,
|
||||
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes?,
|
||||
withGlobalTypes globalTypes: TypeTypeScopes?, withFFIs ffis: [P4FFI] = Array()
|
||||
) -> Result<P4Lang.Program> {
|
||||
|
||||
@@ -122,8 +134,10 @@ public struct Program {
|
||||
|
||||
// Any of the instances that are in the top-level scope should go into the program!
|
||||
program.instances = Array(
|
||||
compilation_context.instances.map { (_, v) in
|
||||
v
|
||||
compilation_context.instances.filter { (_, v) in
|
||||
v.1 != nil
|
||||
}.map { (_, v) in
|
||||
v.1!
|
||||
})
|
||||
|
||||
// Any of the types that are in the top-level scope should go into the program!
|
||||
|
||||
Reference in New Issue
Block a user