use crate::{
Edge,
node::{self, Node},
};
use steel::steel_vm::engine::Engine;
pub trait Visitor {
fn visit_pre(&mut self, _ctx: Ctx, _node: &dyn Node) {}
fn visit_post(&mut self, _ctx: Ctx, _node: &dyn Node) {}
}
#[derive(Clone, Copy, Debug)]
pub struct Ctx<'a> {
path: &'a [node::Id],
inputs: &'a [(node::Id, Edge)],
}
pub(crate) struct Register<'vm>(pub(crate) &'vm mut Engine);
impl<'a> Ctx<'a> {
pub fn new(path: &'a [node::Id], inputs: &'a [(node::Id, Edge)]) -> Self {
Self { path, inputs }
}
pub fn path(&self) -> &[node::Id] {
self.path
}
pub fn id(&self) -> node::Id {
*self.path.last().expect("path cannot be empty")
}
pub fn inputs(&self) -> &[(node::Id, Edge)] {
self.inputs
}
}
impl<'vm> Visitor for Register<'vm> {
fn visit_pre(&mut self, ctx: Ctx, node: &dyn Node) {
node.register(ctx.path(), self.0);
}
}