From 377f40447f800aa0758748df57199f4a94102a51 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Fri, 13 Mar 2026 07:03:31 -0400 Subject: [PATCH] Add update Methods for Compilation Context update method will allow the creation of a new compilation context based on the current one with new types/names. Signed-off-by: Will Hawkins --- Sources/P4Compiler/Compiler.swift | 32 +++++++++++++++++++++++++++++- Sources/P4Compiler/Parser.swift | 2 +- Sources/P4Compiler/Program.swift | 2 +- Sources/P4Compiler/Statement.swift | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Sources/P4Compiler/Compiler.swift b/Sources/P4Compiler/Compiler.swift index c8adc62..ff50cdd 100644 --- a/Sources/P4Compiler/Compiler.swift +++ b/Sources/P4Compiler/Compiler.swift @@ -40,10 +40,40 @@ public func ErrorOnNode(node: Node, withError error: String) -> Error { return Error(withMessage: "\(node.range): \(error)") } +/// Context for compilation. public struct CompilerContext { - public let names: LexicalScopes + let names: LexicalScopes + let types: LexicalScopes public init(withNames _names: LexicalScopes) { names = _names + types = LexicalScopes() } + + public init(withNames _names: LexicalScopes, withTypes _types: LexicalScopes) { + names = _names + types = _types + } + + /// Update a compiler context + /// + /// 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. + /// - Returns: A new compiler context based on the current with the same types and new names. + public func update(newNames names: LexicalScopes) -> CompilerContext { + return CompilerContext(withNames: names, withTypes: self.types) + } + + /// Update a compiler context + /// + /// 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. + /// - Returns: A new compiler context based on the current with the same names and new types. + public func update(newTypes types: LexicalScopes) -> CompilerContext { + return CompilerContext(withNames: self.names, withTypes: types) + } + + } \ No newline at end of file diff --git a/Sources/P4Compiler/Parser.swift b/Sources/P4Compiler/Parser.swift index e758b6e..17ed0ad 100644 --- a/Sources/P4Compiler/Parser.swift +++ b/Sources/P4Compiler/Parser.swift @@ -338,7 +338,7 @@ public struct Parser { // Parse a state in a nested scope. switch Parser.State.Compile( - node: parser_state, withContext: CompilerContext(withNames: current_context.names.enter())) + node: parser_state, withContext: context.update(newNames: current_context.names.enter())) { case Result.Ok(let (state, updated_context)): parser.states = parser.states.append(state: state) diff --git a/Sources/P4Compiler/Program.swift b/Sources/P4Compiler/Program.swift index 2a7a638..4b826cb 100644 --- a/Sources/P4Compiler/Program.swift +++ b/Sources/P4Compiler/Program.swift @@ -168,7 +168,7 @@ public struct Program { { case Result.Ok((let parser, let updated_context)): // Create a new context with the name of the parser that was just compiled in scope. - compilation_context = CompilerContext(withNames: updated_context.names.declare(identifier: parser.name, withValue: parser)) + compilation_context = compilation_context.update(newNames: updated_context.names.declare(identifier: parser.name, withValue: parser)) case Result.Error(let error): errors.append(error) } diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index 9e27d8b..660655a 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -220,7 +220,7 @@ extension VariableDeclarationStatement: CompilableStatement { VariableDeclarationStatement( identifier: parsed_variablename, withInitializer: parsed_rvalue), // Context with updated names to include the newly declared name. - CompilerContext(withNames: context.names.declare( + context.update(newNames: context.names.declare( identifier: parsed_variablename, withValue: declaration_p4_type)) ))