Files
gp4/Sources/P4Runtime/Statements.swift
T
Will Hawkins 44e93e4cda
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
compiler, runtime: Begin Runtime Refactor
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>
2026-05-29 08:41:54 -04:00

194 lines
6.8 KiB
Swift

// 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
import P4Lang
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
) { (cf, execution) in
switch cf {
case ControlFlow.Return(let value): return (ControlFlow.Return(value), execution)
case ControlFlow.Next: return (cf, execution)
case ControlFlow.Error: return (ControlFlow.Error, execution)
default:
return (
ControlFlow.Error,
execution.setError(
error: Error(withMessage: "Invalid control flow \(cf) in block statement"))
)
}
}
}
}
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)
case (.Ok(let initial_value), let execution) = execution.evaluator.EvaluateExpression(
self.initializer, inExecution: execution)
else {
return (
ControlFlow.Error,
execution.setError(error: Error(withMessage: "Could not evaluate \(self.initializer)"))
)
}
let new_scopes = execution.scopes.declare(identifier: self.identifier, withValue: initial_value)
execution.scopes = new_scopes
return (ControlFlow.Next, execution)
}
}
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)
case (.Ok(let evaluated_condition), let execution) = execution.evaluator.EvaluateExpression(
self.condition, inExecution: execution)
else {
return (
ControlFlow.Error,
execution.setError(error: Error(withMessage: "Could not evaluate \(self.condition)"))
)
}
if !evaluated_condition.type().baseType().eq(rhs: P4Boolean()) {
return (
ControlFlow.Error,
execution.setError(error: Error(withMessage: "Condition expression is not a Boolean"))
)
}
if evaluated_condition.dataValue().eq(rhs: P4BooleanValue.init(withValue: true)) {
let execution = execution.enter_scope()
switch self.thenn.evaluate(execution: execution) {
case (ControlFlow.Next, let result): return (ControlFlow.Next, result.exit_scope())
case (ControlFlow.Error, let result): return (ControlFlow.Error, result.exit_scope())
case (let cf, let result):
return (
ControlFlow.Next,
result.setError(
error: Error(withMessage: "Invalid control flow \(cf) in conditional statement"))
)
}
} else if let elss = self.elss {
let execution = execution.enter_scope()
switch elss.evaluate(execution: execution) {
case (ControlFlow.Next, let result): return (ControlFlow.Next, result.exit_scope())
case (ControlFlow.Error, let result): return (ControlFlow.Error, result.exit_scope())
case (let cf, let result):
return (
ControlFlow.Next,
result.setError(
error: Error(withMessage: "Invalid control flow \(cf) in conditional statement"))
)
}
}
return (ControlFlow.Next, execution)
}
}
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!
//return switch self.expression.evaluate(execution: execution) {
return switch execution.evaluator.EvaluateExpression(self.expression, inExecution: execution) {
case (.Ok(_), let updated_context): (ControlFlow.Next, updated_context)
case (.Error(let e), let updated_context):
(ControlFlow.Next, updated_context.setError(error: e))
}
}
}
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) {
case (.Ok(let v), let execution): (ControlFlow.Return(v), execution)
case (.Error(let e), let execution): (ControlFlow.Error, execution.setError(error: e))
}
}
}
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)
}
}