compiler, runtime: Evaluate Expressions in Expression Statements

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-20 03:35:00 -04:00
parent 5bc9db9aca
commit f687353326
3 changed files with 17 additions and 5 deletions
+5 -2
View File
@@ -244,9 +244,12 @@ extension ExpressionStatement: CompilableStatement {
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
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)
}
}
}
+5 -1
View File
@@ -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 {
+7 -2
View File
@@ -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))
}
}
}