From 5ab4694c664a8a48e70df4e18181ddafd0ac6874 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 14 Oct 2023 15:19:27 +0200 Subject: [PATCH] I don't know what I was doing here but I probably won't return to this branch anyway --- rust/src/lib.rs | 1 + rust/src/vm.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 rust/src/vm.rs 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> { +// +//}