@@ -0,0 +1,19 @@
|
||||
// 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
|
||||
@@ -382,3 +382,40 @@ extension KeysetExpression: EvaluatableExpression {
|
||||
return self.kse_type()
|
||||
}
|
||||
}
|
||||
|
||||
extension FunctionCall: EvaluatableExpression {
|
||||
public func evaluate(execution: Common.ProgramExecution) -> Common.Result<any Common.P4Value> {
|
||||
|
||||
guard let body = self.callee.body else {
|
||||
return .Error(Error(withMessage: "No body for called function (\(self.callee.name))"))
|
||||
}
|
||||
|
||||
// Put the arguments into scope
|
||||
|
||||
var called_execution = execution.enter_scope()
|
||||
for (parameter, argument) in zip(self.callee.params.parameters, arguments.arguments) {
|
||||
let arg_idx = argument.index
|
||||
let arg_value = argument.argument
|
||||
let maybe_argument_value = arg_value.evaluate(execution: called_execution)
|
||||
guard case .Ok(let argument_value) = maybe_argument_value else {
|
||||
return .Error(Error(withMessage: "Cannot evaluate argument \(arg_idx): \(argument)"))
|
||||
}
|
||||
called_execution = called_execution.declare(identifier: parameter.name, withValue: argument_value)
|
||||
}
|
||||
|
||||
let (control_flow, _) = body.evaluate(execution: called_execution)
|
||||
|
||||
return switch control_flow {
|
||||
case ControlFlow.Return(let value): if let value = value {
|
||||
.Ok(value)
|
||||
} else {
|
||||
.Error(Error(withMessage: "No value returned from called function (\(self.callee.name))"))
|
||||
}
|
||||
default: .Error(Error(withMessage: "No value returned from called function (\(self.callee.name))"))
|
||||
}
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
return self.callee.tipe
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,20 +19,20 @@ import Common
|
||||
import P4Lang
|
||||
|
||||
extension ParserAssignmentStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
let result = self.value.evaluate(execution: execution)
|
||||
guard case Result.Ok(let value) = result else {
|
||||
return execution.setError(error: result.error()!)
|
||||
return (ControlFlow.Error, execution.setError(error: result.error()!))
|
||||
}
|
||||
|
||||
let maybe_updated_scopes = self.lvalue.set(
|
||||
to: value, inScopes: execution.scopes, duringExecution: execution)
|
||||
guard case Result.Ok(let updated_scopes) = maybe_updated_scopes else {
|
||||
return execution.setError(error: maybe_updated_scopes.error()!)
|
||||
return (ControlFlow.Error, execution.setError(error: maybe_updated_scopes.error()!))
|
||||
}
|
||||
execution.scopes = updated_scopes.0
|
||||
|
||||
return execution
|
||||
return (ControlFlow.Next, execution)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,12 @@ extension ParserStateDirectTransition: EvaluatableParserState {
|
||||
var program = program.enter_scope()
|
||||
|
||||
for statement in statements {
|
||||
program = statement.evaluate(execution: program)
|
||||
let (control_flow, next_program) = statement.evaluate(execution: program)
|
||||
switch control_flow {
|
||||
case .Next: program = next_program // Ok!
|
||||
case .Error: return (reject, next_program)
|
||||
default: return (reject, next_program.setError(error: Error(withMessage: "Invalid control flow (\(control_flow) in parser)")))
|
||||
}
|
||||
}
|
||||
let res = program.scopes.lookup(identifier: get_next_state())
|
||||
|
||||
@@ -93,7 +98,12 @@ extension ParserStateSelectTransition: EvaluatableParserState {
|
||||
|
||||
// First, evaluate the statements.
|
||||
for statement in statements {
|
||||
program = statement.evaluate(execution: program)
|
||||
let (control_flow, next_program) = statement.evaluate(execution: program)
|
||||
switch control_flow {
|
||||
case .Next: program = next_program // Ok!
|
||||
case .Error: return (reject, next_program)
|
||||
default: return (reject, next_program.setError(error: Error(withMessage: "Invalid control flow (\(control_flow) in parser)")))
|
||||
}
|
||||
}
|
||||
|
||||
let res = self.selectExpression.evaluate(execution: program)
|
||||
@@ -159,8 +169,8 @@ extension Parser: CallableExecution {
|
||||
}
|
||||
|
||||
for (parameter, argument) in zip(self.parameters.parameters, arguments.arguments) {
|
||||
let arg_idx = argument.0
|
||||
let arg_value = argument.1
|
||||
let arg_idx = argument.index
|
||||
let arg_value = argument.argument
|
||||
let maybe_argument_value = arg_value.evaluate(execution: execution)
|
||||
guard case .Ok(let argument_value) = maybe_argument_value else {
|
||||
return (
|
||||
|
||||
@@ -22,12 +22,6 @@ public protocol Execution {
|
||||
func execute(execution: ProgramExecution) -> ProgramExecution
|
||||
}
|
||||
|
||||
public protocol Compilable {
|
||||
associatedtype ToCompile
|
||||
associatedtype Compiled
|
||||
static func compile(_: ToCompile) -> Result<Compiled>
|
||||
}
|
||||
|
||||
public protocol EvaluatableParserState: P4Value {
|
||||
func execute(program: ProgramExecution) -> (EvaluatableParserState, ProgramExecution)
|
||||
func done() -> Bool
|
||||
|
||||
@@ -19,51 +19,99 @@ import Common
|
||||
import P4Lang
|
||||
|
||||
extension BlockStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
var execution = execution
|
||||
for s in self.statements {
|
||||
execution = s.evaluate(execution: execution)
|
||||
let (control_flow, next_execution) = s.evaluate(execution: execution)
|
||||
switch control_flow {
|
||||
case ControlFlow.Return(let value): return (ControlFlow.Return(value), next_execution)
|
||||
case ControlFlow.Next: execution = next_execution
|
||||
case ControlFlow.Error: return (ControlFlow.Error, next_execution)
|
||||
default:
|
||||
return (
|
||||
ControlFlow.Next,
|
||||
next_execution.setError(
|
||||
error: Error(withMessage: "Invalid control flow \(control_flow) in block statement"))
|
||||
)
|
||||
}
|
||||
}
|
||||
return execution
|
||||
return (ControlFlow.Next, execution)
|
||||
}
|
||||
}
|
||||
|
||||
extension VariableDeclarationStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
guard case .Ok(let initial_value) = self.initializer.evaluate(execution: execution) else {
|
||||
return execution.setError(error: Error(withMessage: "Could not evaluate \(self.initializer)"))
|
||||
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 execution
|
||||
return (ControlFlow.Next, execution)
|
||||
}
|
||||
}
|
||||
|
||||
extension ConditionalStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
guard case .Ok(let evaluated_condition) = self.condition.evaluate(execution: execution) else {
|
||||
return execution.setError(error: Error(withMessage: "Could not evaluate \(self.condition)"))
|
||||
return (
|
||||
ControlFlow.Error,
|
||||
execution.setError(error: Error(withMessage: "Could not evaluate \(self.condition)"))
|
||||
)
|
||||
}
|
||||
|
||||
if !evaluated_condition.type().eq(rhs: P4Boolean()) {
|
||||
return execution.setError(error: Error(withMessage: "Condition expression is not a Boolean"))
|
||||
return (
|
||||
ControlFlow.Error,
|
||||
execution.setError(error: Error(withMessage: "Condition expression is not a Boolean"))
|
||||
)
|
||||
}
|
||||
|
||||
if evaluated_condition.eq(rhs: P4BooleanValue.init(withValue: true)) {
|
||||
let execution = execution.enter_scope()
|
||||
var result = self.thenn.evaluate(execution: execution)
|
||||
result = result.exit_scope()
|
||||
return result
|
||||
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()
|
||||
var result = elss.evaluate(execution: execution)
|
||||
result = result.exit_scope()
|
||||
return result
|
||||
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 execution
|
||||
return (ControlFlow.Next, execution)
|
||||
}
|
||||
}
|
||||
|
||||
extension ExpressionStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||
return execution
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
// TODO: Should this do something? Side effects?
|
||||
return (ControlFlow.Next, execution)
|
||||
}
|
||||
}
|
||||
|
||||
extension ReturnStatement: EvaluatableStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
||||
return switch self.value.evaluate(execution: execution) {
|
||||
case .Ok(let v): (ControlFlow.Return(v), execution)
|
||||
case .Error(let e): (ControlFlow.Error, execution.setError(error: e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user