I don't know what I was doing here but I probably won't return to this branch anyway

This commit is contained in:
Brian Picciano 2023-10-14 15:19:27 +02:00
parent 92f93ff076
commit 5ab4694c66
2 changed files with 55 additions and 0 deletions

View File

@ -1 +1,2 @@
pub mod gg;
pub mod vm;

54
rust/src/vm.rs Normal file
View File

@ -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<dyn Fn(gg::Value) -> 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> {
//
//}