Refactor: Use Rc/RefCell for shared mutable state

This commit replaces `Arc<Mutex<T>>` with `Rc<RefCell<T>>` for managing
shared mutable state within the AST and VM. This change is primarily an
internal refactoring to leverage Rust's standard library more
effectively for single-threaded scenarios, improving performance by
avoiding the overhead of mutexes.

The following types and their usage have been updated:
- `Environment.global_names` and `Environment.global_values`
- `Binder.globals`
- `bound_nodes::BoundKind::Lambda.body` (now `Rc<Node>`)
- `ast::types::NodeIdentity` (now `Rc<NodeIdentity>`)
- `ast::types::Value::List`, `Value::Record`, `Value::Function`,
  `Value::Object`, `Value::Cell`
- `ast::nodes::Scope` and `ast::nodes::Context`
- `ast::vm::Closure.function_node` and `Closure.upvalues`
- `ast::vm::VM.globals`

This change does not alter the external behavior of the library but
streamlines internal data management.
This commit is contained in:
Michael Schimmel
2026-02-17 13:00:25 +01:00
parent ce166f39e3
commit d55422272b
9 changed files with 112 additions and 148 deletions
+11 -11
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::rc::Rc;
use crate::ast::lexer::{Lexer, Token, TokenKind};
use crate::ast::types::{Identity, NodeIdentity, Value, Keyword};
use crate::ast::nodes::{Node, UntypedKind, Context};
@@ -30,7 +30,7 @@ impl<'a> Parser<'a> {
TokenKind::LeftBracket => self.parse_vector_literal(),
TokenKind::LeftBrace => self.parse_map_literal(),
TokenKind::Quote => {
let identity = Arc::new(NodeIdentity { location: self.current_token.location });
let identity = Rc::new(NodeIdentity { location: self.current_token.location });
self.advance()?; // consume '
let expr = self.parse_expression()?;
Ok(Node {
@@ -48,7 +48,7 @@ impl<'a> Parser<'a> {
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
let token = self.advance()?;
let identity = Arc::new(NodeIdentity { location: token.location });
let identity = Rc::new(NodeIdentity { location: token.location });
let kind = match token.kind {
TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)),
@@ -72,7 +72,7 @@ impl<'a> Parser<'a> {
fn parse_list(&mut self) -> Result<Node<UntypedKind>, String> {
let start_loc = self.advance()?.location; // consume '('
let identity = Arc::new(NodeIdentity { location: start_loc });
let identity = Rc::new(NodeIdentity { location: start_loc });
if *self.peek() == TokenKind::RightParen {
return Err(format!("Empty list () is not a valid expression at {:?}", start_loc));
@@ -161,13 +161,13 @@ impl<'a> Parser<'a> {
identity,
kind: UntypedKind::Lambda {
params,
body: Arc::new(body),
body: Rc::new(body),
},
ty: (),
})
}
fn parse_param_vector(&mut self) -> Result<Vec<Arc<str>>, String> {
fn parse_param_vector(&mut self) -> Result<Vec<Rc<str>>, String> {
if *self.peek() != TokenKind::LeftBracket {
return Err(format!("Expected parameter vector [...] for fn, found {:?}", self.peek()));
}
@@ -215,8 +215,8 @@ impl<'a> Parser<'a> {
self.expect(TokenKind::RightBracket)?;
Ok(Node {
identity: Arc::new(NodeIdentity { location: token.location }),
kind: UntypedKind::Constant(Value::List(Arc::new(elements))),
identity: Rc::new(NodeIdentity { location: token.location }),
kind: UntypedKind::Constant(Value::List(Rc::new(elements))),
ty: (),
})
}
@@ -248,8 +248,8 @@ impl<'a> Parser<'a> {
self.expect(TokenKind::RightBrace)?;
Ok(Node {
identity: Arc::new(NodeIdentity { location: token.location }),
kind: UntypedKind::Constant(Value::Record(Arc::new(map))),
identity: Rc::new(NodeIdentity { location: token.location }),
kind: UntypedKind::Constant(Value::Record(Rc::new(map))),
ty: (),
})
}
@@ -266,7 +266,7 @@ impl<'a> Parser<'a> {
fn make_id_node(&self, name: &str, identity: Identity) -> Node<UntypedKind> {
Node {
identity,
kind: UntypedKind::Identifier(Arc::from(name)),
kind: UntypedKind::Identifier(Rc::from(name)),
ty: (),
}
}