diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 2e78910..1dd0e8c 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1 +1,2 @@ pub mod gg; +pub mod vm; diff --git a/rust/src/vm.rs b/rust/src/vm.rs new file mode 100644 index 0000000..26ee9bb --- /dev/null +++ b/rust/src/vm.rs @@ -0,0 +1,54 @@ + +use super::gg; +use std::collections::HashMap; + +pub enum Error{ + FunctionFromGraph(String), +} + +enum ResolvedValue<'a> { + Value(gg::Value), + Function(&'a dyn Fn(gg::Value) -> gg::Value), +} + +struct Scope<'a> { + parent: Option<&'a Scope<'a>>, + values: HashMap<&'a str, ResolvedValue<'a>>, +} + +impl<'a> Scope<'a> { + + fn new(parent: Option<&'a Scope<'a>>) -> Scope<'a> { + Scope { + parent: parent, + values: HashMap::new(), + } + } + + fn resolve(&self, name: &str) -> Option<&ResolvedValue> { + + if let Some(rval) = self.values.get(name) { + Some(rval) + + } else if let Some(parent) = self.parent { + parent.resolve(name) + + } else { + None + } + } + + fn insert(&mut self, name: &'a str, rval: ResolvedValue<'a>) { + self.values.insert(name, rval); + } +} + +fn always(value: gg::Value) -> Box gg::Value> { + Box::new(move |_: gg::Value| value) +} + +//fn function_from_graph<'a>( +// scope: &'a Scope<'a>, g: gg::Graph, +//) -> Result<&'a dyn Fn(gg::Value) -> gg::Value, Error> { +// +//}