replace Rc with Arc so that Lamm may be used in an async context

This commit is contained in:
2024-10-22 13:26:48 -04:00
parent 0a40cca6ff
commit d9a576897b
3 changed files with 26 additions and 26 deletions

View File

@@ -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<Item = Result<ParseTree, ParseError>>
{
exprs: &'a mut I,
globals: &'a mut HashMap<String, Rc<RefCell<Object>>>,
locals: HashMap<String, Rc<RefCell<Object>>>,
globals: &'a mut HashMap<String, Arc<RefCell<Object>>>,
locals: HashMap<String, Arc<RefCell<Object>>>,
}
impl<'a, I> Executor<'a, I>
where
I: Iterator<Item = Result<ParseTree, ParseError>>,
{
pub fn new(exprs: &'a mut I, globals: &'a mut HashMap<String, Rc<RefCell<Object>>>) -> Self {
pub fn new(exprs: &'a mut I, globals: &'a mut HashMap<String, Arc<RefCell<Object>>>) -> Self {
Self {
exprs,
globals,
@@ -67,27 +67,27 @@ where
}
}
pub fn _add_global(self, k: String, v: Rc<RefCell<Object>>) -> Self {
pub fn _add_global(self, k: String, v: Arc<RefCell<Object>>) -> Self {
self.globals.insert(k, v);
self
}
pub fn locals(mut self, locals: HashMap<String, Rc<RefCell<Object>>>) -> Self {
pub fn locals(mut self, locals: HashMap<String, Arc<RefCell<Object>>>) -> Self {
self.locals = locals;
self
}
pub fn add_local(mut self, k: String, v: Rc<RefCell<Object>>) -> Self {
pub fn add_local(mut self, k: String, v: Arc<RefCell<Object>>) -> Self {
self.locals.insert(k, v);
self
}
fn _get_object(&self, ident: &String) -> Result<&Rc<RefCell<Object>>, RuntimeError> {
fn _get_object(&self, ident: &String) -> Result<&Arc<RefCell<Object>>, 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<RefCell<Object>>, RuntimeError> {
fn get_object_mut(&mut self, ident: &String) -> Result<&mut Arc<RefCell<Object>>, 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) => {