From ad66e839fa7f2b355ff03806a53153ece6139f34 Mon Sep 17 00:00:00 2001 From: Roman Godmaire Date: Sat, 17 Feb 2024 07:13:15 -0500 Subject: [PATCH] misc: remove unnecessary constants This is premature optimization. --- src/evaluator/env.rs | 12 ++++++------ src/evaluator/mod.rs | 14 ++++---------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/evaluator/env.rs b/src/evaluator/env.rs index 91289eb..92a76bd 100644 --- a/src/evaluator/env.rs +++ b/src/evaluator/env.rs @@ -1,6 +1,6 @@ use std::{borrow::Borrow, cell::RefCell, collections::HashMap, rc::Rc}; -use super::{eval_ast_node, Error, Expression, FALSE, TRUE}; +use super::{eval_ast_node, Error, Expression}; use crate::parser::Node; pub type RawEnvironment = HashMap>; @@ -135,10 +135,10 @@ pub fn core_environment() -> Rc { } if args[0] == args[1] { - return Ok(TRUE.with(|inner| inner.clone())); + return Ok(Expression::Boolean(true).into()); } - return Ok(FALSE.with(|inner| inner.clone())); + Ok(Expression::Boolean(false).into()) }), ), ( @@ -148,11 +148,11 @@ pub fn core_environment() -> Rc { Err(Error::MismatchedArgCount(1, args.len()))? } - if FALSE.with(|inner| &args[0] == inner) { - return Ok(TRUE.with(|inner| inner.clone())); + if *args[0] == Expression::Boolean(false) { + return Ok(Expression::Boolean(true).into()); } - return Ok(FALSE.with(|inner| inner.clone())); + Ok(Expression::Boolean(false).into()) }), ), // Branching diff --git a/src/evaluator/mod.rs b/src/evaluator/mod.rs index f9daf24..010b773 100644 --- a/src/evaluator/mod.rs +++ b/src/evaluator/mod.rs @@ -8,12 +8,6 @@ use crate::parser::Node; mod env; pub use env::{core_environment, Environment}; -thread_local! { - static TRUE: Rc = Rc::new(Expression::Boolean(true)); - static FALSE: Rc = Rc::new(Expression::Boolean(false)); - static NIL: Rc = Rc::new(Expression::Nil); -} - #[derive(Debug, PartialEq)] pub enum Expression { // Values @@ -108,7 +102,7 @@ fn eval_ast_node(env: Rc, ast_node: Node) -> Result> Node::List(list) => { // Empty lists are nil in scheme if list.is_empty() { - return Ok(NIL.with(|v| v.clone())); + return Ok(Expression::Nil.into()); } // We always assume lists evaluate @@ -163,9 +157,9 @@ fn eval_ast_node(env: Rc, ast_node: Node) -> Result> Node::Keyword(val) => Expression::Keyword(val).into(), Node::String(s) => Expression::String(s).into(), - Node::True => TRUE.with(|val| val.clone()), - Node::False => FALSE.with(|val| val.clone()), - Node::Nil => NIL.with(|val| val.clone()), + Node::True => Expression::Boolean(true).into(), + Node::False => Expression::Boolean(false).into(), + Node::Nil => Expression::Nil.into(), }; Ok(expr)