Refactor Names of Scope Types

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-20 04:23:53 -04:00
parent 6a9a138afe
commit f0d816b99f
12 changed files with 144 additions and 163 deletions
+9 -9
View File
@@ -42,15 +42,15 @@ public func ErrorOnNode(node: Node, withError error: String) -> Error {
/// Context for compilation.
public struct CompilerContext {
let names: LexicalScopes
let types: LexicalScopes
let names: VarTypeScopes
let types: TypeTypeScopes
public init(withNames _names: LexicalScopes) {
public init(withNames _names: VarTypeScopes) {
names = _names
types = LexicalScopes()
types = TypeTypeScopes()
}
public init(withNames _names: LexicalScopes, withTypes _types: LexicalScopes) {
public init(withNames _names: VarTypeScopes, withTypes _types: TypeTypeScopes) {
names = _names
types = _types
}
@@ -59,9 +59,9 @@ public struct CompilerContext {
///
/// Create a new compiler context based on the current with the same types and new names.
///
/// - Parameter names: a ``LexicalScopes`` with the updated names for the newly created compiler context.
/// - Parameter names: a ``TypeScopes`` with the updated names for the newly created compiler context.
/// - Returns: A new compiler context based on the current with the same types and new names.
public func update(newNames names: LexicalScopes) -> CompilerContext {
public func update(newNames names: VarTypeScopes) -> CompilerContext {
return CompilerContext(withNames: names, withTypes: self.types)
}
@@ -69,9 +69,9 @@ public struct CompilerContext {
///
/// Create a new compiler context based on the current with the same names and new types.
///
/// - Parameter types: a ``LexicalScopes`` with the updated types for the newly created compiler context.
/// - Parameter types: a ``TypeScopes`` with the updated types for the newly created compiler context.
/// - Returns: A new compiler context based on the current with the same names and new types.
public func update(newTypes types: LexicalScopes) -> CompilerContext {
public func update(newTypes types: TypeTypeScopes) -> CompilerContext {
return CompilerContext(withNames: self.names, withTypes: types)
}
-21
View File
@@ -1,21 +0,0 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
public typealias LexicalScope = Scope<P4Type>
public typealias LexicalScopes = Scopes<P4Type>
+13 -4
View File
@@ -24,11 +24,15 @@ import TreeSitterP4
public struct Program {
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
return Program.Compile(source, withGlobalTypes: .none)
return Program.Compile(source, withGlobalInstances: .none, withGlobalTypes: .none)
}
public static func Compile(_ source: String, withGlobalInstances globalInstances: VarTypeScopes) -> Result<P4Lang.Program> {
return Program.Compile(source, withGlobalInstances: globalInstances, withGlobalTypes: .none)
}
public static func Compile(
_ source: String, withGlobalTypes globalTypes: LexicalScopes?
_ source: String, withGlobalInstances globalInstances: VarTypeScopes?, withGlobalTypes globalTypes: TypeTypeScopes?
) -> Result<P4Lang.Program> {
let maybe_parser = ConfigureP4Parser()
@@ -47,13 +51,18 @@ public struct Program {
var program = P4Lang.Program()
// Set up a context for parsing.
var compilation_context = CompilerContext(withNames: LexicalScopes().enter())
var compilation_context = CompilerContext(withNames: VarTypeScopes().enter())
var errors: [Error] = Array()
// If the caller gave any global instances, add them here.
if let globalInstances = globalInstances {
compilation_context = compilation_context.update(newNames: globalInstances)
}
// If the caller gave any global types, add them here.
if let globalTypes = globalTypes {
compilation_context = compilation_context.update(newNames: globalTypes)
compilation_context = compilation_context.update(newTypes: globalTypes)
}
result?.rootNode?.enumerateNamedChildren { declaration_node in