From d9a576897b14730bbf5df79d90ebdb6371523886 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Tue, 22 Oct 2024 13:26:48 -0400 Subject: [PATCH] replace Rc with Arc so that Lamm may be used in an async context --- src/executor.rs | 24 ++++++++++++------------ src/function.rs | 10 +++++----- src/lib.rs | 18 +++++++++--------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 3d8f4d3..394ccee 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use std::fmt::Display; use std::error::Error; use std::io; -use std::rc::Rc; +use std::sync::Arc; use std::cell::RefCell; #[derive(Debug)] @@ -51,15 +51,15 @@ where I: Iterator> { exprs: &'a mut I, - globals: &'a mut HashMap>>, - locals: HashMap>>, + globals: &'a mut HashMap>>, + locals: HashMap>>, } impl<'a, I> Executor<'a, I> where I: Iterator>, { - pub fn new(exprs: &'a mut I, globals: &'a mut HashMap>>) -> Self { + pub fn new(exprs: &'a mut I, globals: &'a mut HashMap>>) -> Self { Self { exprs, globals, @@ -67,27 +67,27 @@ where } } - pub fn _add_global(self, k: String, v: Rc>) -> Self { + pub fn _add_global(self, k: String, v: Arc>) -> Self { self.globals.insert(k, v); self } - pub fn locals(mut self, locals: HashMap>>) -> Self { + pub fn locals(mut self, locals: HashMap>>) -> Self { self.locals = locals; self } - pub fn add_local(mut self, k: String, v: Rc>) -> Self { + pub fn add_local(mut self, k: String, v: Arc>) -> Self { self.locals.insert(k, v); self } - fn _get_object(&self, ident: &String) -> Result<&Rc>, RuntimeError> { + fn _get_object(&self, ident: &String) -> Result<&Arc>, RuntimeError> { self.locals.get(ident).or(self.globals.get(ident)) .ok_or(RuntimeError::VariableUndefined(ident.clone())) } - fn get_object_mut(&mut self, ident: &String) -> Result<&mut Rc>, RuntimeError> { + fn get_object_mut(&mut self, ident: &String) -> Result<&mut Arc>, RuntimeError> { self.locals.get_mut(ident).or(self.globals.get_mut(ident)) .ok_or(RuntimeError::VariableUndefined(ident.clone())) } @@ -238,7 +238,7 @@ where Executor::new(self.exprs, &mut self.globals) .locals(self.locals.clone()) - .add_local(ident, Rc::new(RefCell::new(Object::value(value, g, self.locals.to_owned())))) + .add_local(ident, Arc::new(RefCell::new(Object::value(value, g, self.locals.to_owned())))) .exec(scope) } }, @@ -249,7 +249,7 @@ where let g = self.globals.clone(); Executor::new(self.exprs, &mut self.globals) .locals(self.locals.clone()) - .add_local(ident, Rc::new(RefCell::new(Object::variable(*body, g, self.locals.to_owned())))) + .add_local(ident, Arc::new(RefCell::new(Object::variable(*body, g, self.locals.to_owned())))) .exec(scope) } }, @@ -257,7 +257,7 @@ where let g = self.globals.clone(); Executor::new(self.exprs, &mut self.globals) .locals(self.locals.clone()) - .add_local(func.name().unwrap().to_string(), Rc::new(RefCell::new(Object::function(func, g, self.locals.clone())))) + .add_local(func.name().unwrap().to_string(), Arc::new(RefCell::new(Object::function(func, g, self.locals.clone())))) .exec(scope) }, ParseTree::Compose(x, y) => { diff --git a/src/function.rs b/src/function.rs index 4290f10..c357454 100644 --- a/src/function.rs +++ b/src/function.rs @@ -5,7 +5,7 @@ use crate::{Type, Object, Value}; use std::collections::HashMap; use std::fmt::{self, Display}; -use std::rc::Rc; +use std::sync::Arc; #[derive(Clone, Debug, PartialEq)] pub struct FunctionType(pub Box, pub Vec); @@ -52,8 +52,8 @@ impl Function { } pub(crate) fn call(&mut self, - mut globals: HashMap>>, - locals: HashMap>>, + mut globals: HashMap>>, + locals: HashMap>>, args: Vec) -> Result { let mut tree = vec![Ok(*self.body.clone())].into_iter(); @@ -63,11 +63,11 @@ impl Function { .locals(locals.clone()); for (obj, name) in std::iter::zip(args.into_iter(), self.arg_names.clone().into_iter()) { - exec = exec.add_local(name.clone(), Rc::new(RefCell::new(obj))); + exec = exec.add_local(name.clone(), Arc::new(RefCell::new(obj))); } if let Some(name) = self.name().map(|x| x.to_string()) { - exec = exec.add_local(name, Rc::new(RefCell::new(Object::function(self.clone(), g, locals)))); + exec = exec.add_local(name, Arc::new(RefCell::new(Object::function(self.clone(), g, locals)))); } exec.next().unwrap() diff --git a/src/lib.rs b/src/lib.rs index 57545fe..4cc12e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ use std::fmt::Display; use std::io::BufRead; use std::fmt; use std::iter::Peekable; -use std::rc::Rc; +use std::sync::Arc; use std::cell::RefCell; #[derive(Clone, Debug)] @@ -103,13 +103,13 @@ enum Cache { #[derive(Clone, Debug, PartialEq)] struct Object { - locals: HashMap>>, - globals: HashMap>>, + locals: HashMap>>, + globals: HashMap>>, value: Cache, } impl Object { - pub fn variable(tree: ParseTree, globals: HashMap>>, locals: HashMap>>) -> Self { + pub fn variable(tree: ParseTree, globals: HashMap>>, locals: HashMap>>) -> Self { Self { locals, globals, @@ -117,7 +117,7 @@ impl Object { } } - pub fn value(v: Value, globals: HashMap>>, locals: HashMap>>) -> Self { + pub fn value(v: Value, globals: HashMap>>, locals: HashMap>>) -> Self { Self { locals, globals, @@ -125,7 +125,7 @@ impl Object { } } - pub fn function(func: Function, globals: HashMap>>, locals: HashMap>>) -> Self { + pub fn function(func: Function, globals: HashMap>>, locals: HashMap>>) -> Self { Self { locals, globals, @@ -152,11 +152,11 @@ impl Object { } } - pub fn locals(&self) -> HashMap>> { + pub fn locals(&self) -> HashMap>> { self.locals.clone() } - pub fn globals(&self) -> HashMap>> { + pub fn globals(&self) -> HashMap>> { self.globals.clone() } } @@ -164,7 +164,7 @@ impl Object { pub struct Runtime<'a, R: BufRead> { tokenizer: Peekable>, global_types: HashMap, - globals: HashMap>>, + globals: HashMap>>, parser: Option>>, }