compiler, runtime: Begin Runtime Refactor
Continuous Integration / Grammar Tests (push) Failing after 39s
Continuous Integration / Library Format Tests (push) Successful in 1m46s
Continuous Integration / Library Tests (push) Successful in 4m38s

Ultimately, the goal is to completely separate the compilation from
the runtime to make it possible to have the interpreter/evaluator
be "just another" entity that can perform meaningful work when
given a parsed GP4 program.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-29 08:41:49 -04:00
parent 18461a9215
commit 44e93e4cda
30 changed files with 1264 additions and 854 deletions
+64 -6
View File
@@ -18,7 +18,11 @@
import Common
import P4Lang
extension BlockStatement: EvaluatableStatement {
extension BlockStatement: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
return execution.evaluator.ExecuteStatements(
self.statements, inExecution: execution
@@ -38,7 +42,14 @@ extension BlockStatement: EvaluatableStatement {
}
}
extension VariableDeclarationStatement: EvaluatableStatement {
extension VariableDeclarationStatement: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context.update(
newInstances: context.instances.declare(
identifier: self.identifier, withValue: (self.identifier.type(), .none)))
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
guard
//case (.Ok(let initial_value), let execution) = self.initializer.evaluate(execution: execution)
@@ -56,7 +67,11 @@ extension VariableDeclarationStatement: EvaluatableStatement {
}
}
extension ConditionalStatement: EvaluatableStatement {
extension ConditionalStatement: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
guard
//case (.Ok(let evaluated_condition), let execution) = self.condition.evaluate(execution: execution)
@@ -105,7 +120,11 @@ extension ConditionalStatement: EvaluatableStatement {
}
}
extension ExpressionStatement: EvaluatableStatement {
extension ExpressionStatement: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
// Evaluate, there might be side effects!
@@ -118,7 +137,12 @@ extension ExpressionStatement: EvaluatableStatement {
}
}
extension ReturnStatement: EvaluatableStatement {
extension ReturnStatement: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
//return switch self.value.evaluate(execution: execution) {
return switch execution.evaluator.EvaluateExpression(self.value, inExecution: execution) {
@@ -128,8 +152,42 @@ extension ReturnStatement: EvaluatableStatement {
}
}
extension ApplyStatement: EvaluatableStatement {
extension ApplyStatement: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
return (ControlFlow.Next, execution)
}
}
extension Instantiation: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
return context
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
return (ControlFlow.Next, execution)
}
}
extension Declaration: P4Statement {
public func effect(context: Common.CompilerContext) -> Common.CompilerContext {
if self.extern {
return context.update(
newExterns: context.externs.declare(
identifier: self.identifier, withValue: self))
}
return context.update(
newTypes: context.types.declare(
identifier: self.identifier, withValue: self.identifier.type().baseType()))
}
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
//// TODO
return (ControlFlow.Next, execution)
}
}