|
| 1 | +use crate::context::Context; |
| 2 | +use crate::context::signal::Signal; |
| 3 | +use crate::error::RuntimeError; |
| 4 | +use std::collections::HashMap; |
| 5 | +use tucana::shared::{NodeFunction, NodeParameter}; |
| 6 | +use crate::context::argument::{Argument, ParameterNode}; |
| 7 | +use crate::context::registry::FunctionStore; |
| 8 | + |
| 9 | +pub struct Executor<'a> { |
| 10 | + functions: &'a FunctionStore, |
| 11 | + nodes: HashMap<i64, NodeFunction>, |
| 12 | + context: Context, |
| 13 | +} |
| 14 | + |
| 15 | +type HandleNodeParameterFn = fn(&mut Executor, node_parameter: &NodeParameter) -> Signal; |
| 16 | + |
| 17 | +impl<'a> Executor<'a> { |
| 18 | + pub fn new( |
| 19 | + functions: &'a FunctionStore, |
| 20 | + nodes: HashMap<i64, NodeFunction>, |
| 21 | + context: Context, |
| 22 | + ) -> Self { |
| 23 | + Executor { |
| 24 | + functions, |
| 25 | + nodes, |
| 26 | + context, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn execute(&mut self, starting_node_id: i64) -> Signal { |
| 31 | + let mut current_node_id = starting_node_id; |
| 32 | + |
| 33 | + loop { |
| 34 | + let node = match self.nodes.get(¤t_node_id) { |
| 35 | + None => { |
| 36 | + return Signal::Failure(RuntimeError::simple_str( |
| 37 | + "NodeNotFound", |
| 38 | + "The node with the id was not found", |
| 39 | + )); |
| 40 | + } |
| 41 | + Some(n) => n.clone(), |
| 42 | + }; |
| 43 | + |
| 44 | + |
| 45 | + let entry = match self.functions.get(node.runtime_function_id.as_str()) { |
| 46 | + None => { |
| 47 | + return Signal::Failure(RuntimeError::simple_str("FunctionNotFound","The function was not found")) |
| 48 | + }, |
| 49 | + Some(f) => f, |
| 50 | + }; |
| 51 | + |
| 52 | + let mut args: Vec<Argument> = Vec::with_capacity(node.parameters.len()); |
| 53 | + for parameter in &node.parameters { |
| 54 | + let node_value = match ¶meter.value { |
| 55 | + Some(v) => v, |
| 56 | + None => return Signal::Failure(RuntimeError::simple_str("NodeValueNotFound","Missing parameter value")), |
| 57 | + }; |
| 58 | + let value = match &node_value.value { |
| 59 | + Some(v) => v, |
| 60 | + None => return Signal::Failure(RuntimeError::simple_str("NodeValueNotFound","Missing inner value")), |
| 61 | + }; |
| 62 | + |
| 63 | + match value { |
| 64 | + tucana::shared::node_value::Value::LiteralValue(val) => { |
| 65 | + args.push(Argument::Eval(val.clone())) |
| 66 | + } |
| 67 | + tucana::shared::node_value::Value::ReferenceValue(_r) => { |
| 68 | + unimplemented!("ReferenceValue") |
| 69 | + } |
| 70 | + tucana::shared::node_value::Value::NodeFunctionId(id) => { |
| 71 | + args.push(Argument::Thunk(*id)) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + // Eagerly evaluate args that the function *declares* as Eager |
| 78 | + for (i, a) in args.iter_mut().enumerate() { |
| 79 | + let mode = entry.param_modes.get(i).copied().unwrap_or(ParameterNode::Eager); |
| 80 | + if matches!(mode, ParameterNode::Eager) { |
| 81 | + if let Argument::Thunk(id) = *a { |
| 82 | + match self.execute(id) { |
| 83 | + Signal::Success(v) => *a = Argument::Eval(v), |
| 84 | + // propagate control flow immediately |
| 85 | + s @ (Signal::Failure(_) | Signal::Return(_) | Signal::Respond(_) | Signal::Stop) => return s, |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Provide a runner for Lazy params |
| 92 | + let mut run = |node_id: i64| self.execute(node_id); |
| 93 | + |
| 94 | + // Call the handler (no special cases anywhere) |
| 95 | + let result = (entry.handler)(&args, &mut self.context, &mut run); |
| 96 | + |
| 97 | + match result { |
| 98 | + Signal::Success(value) => { |
| 99 | + if let Some(next_node_id) = node.next_node_id { |
| 100 | + current_node_id = next_node_id; |
| 101 | + continue; |
| 102 | + } else { |
| 103 | + return Signal::Success(value); |
| 104 | + } |
| 105 | + } |
| 106 | + Signal::Failure(e) => return Signal::Failure(e), |
| 107 | + Signal::Return(v) => return Signal::Return(v), |
| 108 | + Signal::Respond(v) => return Signal::Respond(v), |
| 109 | + Signal::Stop => return Signal::Stop, |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments