compiler, runtime: Evaluate Expressions in Expression Statements
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -244,9 +244,12 @@ extension ExpressionStatement: CompilableStatement {
|
|||||||
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
|
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
|
||||||
node: node, type: "expressionStatement", nice_type_name: "expression statement")
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,11 @@
|
|||||||
import Common
|
import Common
|
||||||
|
|
||||||
public struct ExpressionStatement {
|
public struct ExpressionStatement {
|
||||||
public init() {}
|
public let expression: EvaluatableExpression
|
||||||
|
|
||||||
|
public init(_ expr: EvaluatableExpression) {
|
||||||
|
self.expression = expr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Program {
|
public struct Program {
|
||||||
|
|||||||
@@ -105,8 +105,13 @@ extension ConditionalStatement: EvaluatableStatement {
|
|||||||
|
|
||||||
extension ExpressionStatement: EvaluatableStatement {
|
extension ExpressionStatement: EvaluatableStatement {
|
||||||
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user