Refactor Compilation Interface

Compilation interface(s) now contain a compilation context.
The change will make it easier to expand on what each step
of the compilation process needs to know to complete its task
without having to make major changes to the interface.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-13 06:25:30 -04:00
parent df841a0a1b
commit 2f7d05a3fd
7 changed files with 111 additions and 98 deletions
+8 -7
View File
@@ -40,8 +40,8 @@ public struct Program {
var program = P4Lang.Program()
// Set up a lexical scope for parsing.
var program_scope = LexicalScopes().enter()
// Set up a context for parsing.
var compilation_context = CompilerContext(withNames: LexicalScopes().enter())
var errors: [Error] = Array()
@@ -107,7 +107,7 @@ public struct Program {
}
currentChild = type_node?.child(at: currentChildIdx)
switch Identifier.Compile(node: currentChild!, withTypesInScopes: program_scope) {
switch Identifier.Compile(node: currentChild!, withContext: compilation_context) {
case .Ok(let id): parser_name = id
case .Error(let e):
errors.append(e)
@@ -164,10 +164,11 @@ public struct Program {
}
switch Parser.Compile(
withName: parser_name!, node: currentChild!, withTypesInScope: program_scope)
withName: parser_name!, node: currentChild!, withContext: compilation_context)
{
case Result.Ok((let parser, let new_program_scope)):
program_scope = new_program_scope.declare(identifier: parser.name, withValue: parser)
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))
case Result.Error(let error): errors.append(error)
}
@@ -183,7 +184,7 @@ public struct Program {
}
// Any of the types that are in the top-level scope should go into the program!
program.types = Array(program_scope)
program.types = Array(compilation_context.names)
return Result.Ok(program)
}
}