From f687353326190f1447b86c517cda416b2fc49f61 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Mon, 20 Apr 2026 03:35:00 -0400 Subject: [PATCH] compiler, runtime: Evaluate Expressions in Expression Statements Signed-off-by: Will Hawkins --- Sources/P4Compiler/Statement.swift | 7 +++++-- Sources/P4Lang/Program.swift | 6 +++++- Sources/P4Runtime/Statements.swift | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index 4ffdebf..d855699 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -244,9 +244,12 @@ extension ExpressionStatement: CompilableStatement { #RequireNodeType( node: node, type: "expressionStatement", nice_type_name: "expression statement") - let _ = node.child(at: 0) + let expression_node = node.child(at: 0)! - return Result.Ok((ExpressionStatement(), context)) + return switch Expression.Compile(node: expression_node, withContext: context) { + case .Ok(let expression): .Ok((ExpressionStatement(expression), context)) + case .Error(let e): .Error(e) + } } } diff --git a/Sources/P4Lang/Program.swift b/Sources/P4Lang/Program.swift index 6bc7810..504f1f0 100644 --- a/Sources/P4Lang/Program.swift +++ b/Sources/P4Lang/Program.swift @@ -18,7 +18,11 @@ import Common public struct ExpressionStatement { - public init() {} + public let expression: EvaluatableExpression + + public init(_ expr: EvaluatableExpression) { + self.expression = expr + } } public struct Program { diff --git a/Sources/P4Runtime/Statements.swift b/Sources/P4Runtime/Statements.swift index 70cf241..252bf5e 100644 --- a/Sources/P4Runtime/Statements.swift +++ b/Sources/P4Runtime/Statements.swift @@ -105,8 +105,13 @@ extension ConditionalStatement: EvaluatableStatement { extension ExpressionStatement: EvaluatableStatement { public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) { - // TODO: Should this do something? Side effects? - return (ControlFlow.Next, execution) + + // Evaluate, there might be side effects! + return switch self.expression.evaluate(execution: execution) { + case (.Ok(_), let updated_context): (ControlFlow.Next, updated_context) + case (.Error(let e), let updated_context): + (ControlFlow.Next, updated_context.setError(error: e)) + } } }