From 60df8b4d6490ec9baa176bdb0f8f3eee1df90d21 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 24 Sep 2024 20:32:52 -0400 Subject: [PATCH] Remove identiifer cache and extend some tests --- dust-lang/src/identifier.rs | 23 ++--------------------- dust-lang/src/instruction.rs | 4 ++-- dust-lang/src/parser/mod.rs | 30 +++++++++++------------------- dust-lang/src/type.rs | 2 +- dust-lang/tests/expressions.rs | 4 ++++ 5 files changed, 20 insertions(+), 43 deletions(-) diff --git a/dust-lang/src/identifier.rs b/dust-lang/src/identifier.rs index c35d41a..f6744eb 100644 --- a/dust-lang/src/identifier.rs +++ b/dust-lang/src/identifier.rs @@ -14,22 +14,12 @@ //! assert_eq!(foo.strong_count(), 4); // One for each of the above and one for the cache. //! ``` use std::{ - collections::HashMap, fmt::{self, Display, Formatter}, - hash::Hash, - sync::{Arc, OnceLock, RwLock}, + sync::Arc, }; use serde::{de::Visitor, Deserialize, Serialize}; -/// In-use identifiers. -static IDENTIFIER_CACHE: OnceLock>> = OnceLock::new(); - -/// Returns the identifier cache. -fn identifier_cache<'a>() -> &'a RwLock> { - IDENTIFIER_CACHE.get_or_init(|| RwLock::new(HashMap::new())) -} - /// Key used to identify a value or type. /// /// See the [module-level documentation](index.html) for more information. @@ -40,17 +30,8 @@ impl Identifier { /// Creates a new identifier or returns a clone of an existing one from a cache. pub fn new(text: T) -> Self { let string = text.to_string(); - let mut cache = identifier_cache().write().unwrap(); - if let Some(old) = cache.get(&string) { - old.clone() - } else { - let new = Identifier(Arc::new(string.clone())); - - cache.insert(string, new.clone()); - - new - } + Identifier(Arc::new(string.clone())) } pub fn as_str(&self) -> &str { diff --git a/dust-lang/src/instruction.rs b/dust-lang/src/instruction.rs index b74691c..06a9972 100644 --- a/dust-lang/src/instruction.rs +++ b/dust-lang/src/instruction.rs @@ -389,10 +389,10 @@ impl Instruction { } else { "???".to_string() }; - let mutable_display = if self.c_as_boolean() { "mut " } else { "" }; + let mutable_display = if self.c_as_boolean() { "mut" } else { "" }; Some(format!( - "L{local_index} = R{destination} {mutable_display}{identifier_display}" + "L{local_index} = R{destination} {mutable_display} {identifier_display}" )) } Operation::GetLocal => { diff --git a/dust-lang/src/parser/mod.rs b/dust-lang/src/parser/mod.rs index 36d419d..9b695f2 100644 --- a/dust-lang/src/parser/mod.rs +++ b/dust-lang/src/parser/mod.rs @@ -727,22 +727,22 @@ impl<'src> Parser<'src> { } fn parse_if(&mut self, allow_assignment: bool, allow_return: bool) -> Result<(), ParseError> { - self.advance()?; + let length = self.chunk.len(); + self.advance()?; self.parse_expression()?; - let is_explicit_true = matches!(self.previous_token, Token::Boolean("true")) - && matches!(self.current_token, Token::LeftCurlyBrace); - - let (mut load_boolean, load_boolean_position) = - self.chunk.pop_instruction(self.current_position)?; - - debug_assert_eq!(load_boolean.operation(), Operation::LoadBoolean); - - load_boolean.set_c_to_boolean(is_explicit_true); - self.emit_instruction(load_boolean, load_boolean_position); + let is_explicit_true = + matches!(self.previous_token, Token::Boolean("true")) && length == self.chunk.len() - 1; if is_explicit_true { + let (mut load_boolean, load_boolean_position) = + self.chunk.pop_instruction(self.current_position)?; + + debug_assert_eq!(load_boolean.operation(), Operation::LoadBoolean); + + load_boolean.set_c_to_boolean(true); + self.emit_instruction(load_boolean, load_boolean_position); self.increment_register()?; } @@ -763,12 +763,6 @@ impl<'src> Parser<'src> { }; let jump_start = self.chunk.len(); - let load_boolean_index = - if let Operation::LoadBoolean = self.chunk.get_last_operation().unwrap() { - Some(self.chunk.len().saturating_sub(1)) - } else { - None - }; if let Token::LeftCurlyBrace = self.current_token { self.parse_block(allow_assignment, allow_return)?; @@ -793,8 +787,6 @@ impl<'src> Parser<'src> { jump_position, ); - if let Some(index) = load_boolean_index {} - Ok(()) } diff --git a/dust-lang/src/type.rs b/dust-lang/src/type.rs index 5161f28..4e97b06 100644 --- a/dust-lang/src/type.rs +++ b/dust-lang/src/type.rs @@ -1,4 +1,4 @@ -//! Description of a kind of value. +//! Value types. //! //! Most types are concrete and specific, the exceptions are the Generic and Any types. //! diff --git a/dust-lang/tests/expressions.rs b/dust-lang/tests/expressions.rs index 441a3e2..501147d 100644 --- a/dust-lang/tests/expressions.rs +++ b/dust-lang/tests/expressions.rs @@ -21,11 +21,13 @@ fn r#if() { #[test] fn less_than() { assert_eq!(run("1 < 2"), Ok(Some(Value::boolean(true)))); + assert_eq!(run("2 < 1"), Ok(Some(Value::boolean(false)))); } #[test] fn greater_than() { assert_eq!(run("1 > 2"), Ok(Some(Value::boolean(false)))); + assert_eq!(run("2 > 1"), Ok(Some(Value::boolean(true)))); } #[test] @@ -43,11 +45,13 @@ fn greater_than_or_equal() { #[test] fn equal() { assert_eq!(run("1 == 1"), Ok(Some(Value::boolean(true)))); + assert_eq!(run("1 == 2"), Ok(Some(Value::boolean(false)))); } #[test] fn not_equal() { assert_eq!(run("1 != 1"), Ok(Some(Value::boolean(false)))); + assert_eq!(run("2 != 1"), Ok(Some(Value::boolean(true)))); } #[test]