From 228f447a066c0cdb9142f2dad1cb970e8c145ccc Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Thu, 12 Jan 2023 09:14:28 -0800 Subject: [PATCH] Switch fully to ArcIntern. --- src/lib.rs | 1 - src/syntax/parser.lalrpop | 4 +-- src/syntax/tokens.rs | 8 ++--- src/util.rs | 1 - src/util/istring.rs | 66 --------------------------------------- 5 files changed, 6 insertions(+), 74 deletions(-) delete mode 100644 src/util.rs delete mode 100644 src/util/istring.rs diff --git a/src/lib.rs b/src/lib.rs index 7a09d0c..b04da03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,5 @@ pub mod errors; pub mod passes; pub mod runtime; pub mod syntax; -pub mod util; pub mod variable_map; pub mod warnings; diff --git a/src/syntax/parser.lalrpop b/src/syntax/parser.lalrpop index 95fc2a5..4257d57 100644 --- a/src/syntax/parser.lalrpop +++ b/src/syntax/parser.lalrpop @@ -1,7 +1,7 @@ use crate::syntax::ast::{Program,Statement,Expression,Value}; use crate::syntax::tokens::Token; use crate::syntax::token_stream::{LexerError, Location}; -use crate::util::istring::InternedString; +use internment::ArcIntern; grammar; @@ -21,7 +21,7 @@ extern { "/" => Token::Operator('/'), "" => Token::Number((>,)), - "" => Token::Variable(), + "" => Token::Variable(>), } } diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 063f1ea..3120c63 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -1,4 +1,4 @@ -use crate::util::istring::InternedString; +use internment::ArcIntern; use logos::{Lexer, Logos}; use std::fmt; use std::num::ParseIntError; @@ -24,8 +24,8 @@ pub enum Token { #[regex(r"[0-9]+", |v| parse_number(None, v))] Number((Option, i128)), - #[regex(r"[a-z][a-zA-Z0-9_]*", |v| InternedString::new(v.slice()))] - Variable(InternedString), + #[regex(r"[a-z][a-zA-Z0-9_]*", |v| ArcIntern::new(v.slice().to_string()))] + Variable(ArcIntern), #[error] #[regex(r"[ \t\r\n\f]+", logos::skip)] @@ -57,7 +57,7 @@ impl fmt::Display for Token { #[cfg(test)] impl Token { pub(crate) fn var(s: &str) -> Token { - Token::Variable(InternedString::new(s)) + Token::Variable(ArcIntern::new(s.to_string())) } } diff --git a/src/util.rs b/src/util.rs deleted file mode 100644 index f324427..0000000 --- a/src/util.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod istring; diff --git a/src/util/istring.rs b/src/util/istring.rs deleted file mode 100644 index e730f87..0000000 --- a/src/util/istring.rs +++ /dev/null @@ -1,66 +0,0 @@ -use lazy_static::lazy_static; -use std::cmp::{max, Ordering}; -use std::collections::HashMap; -use std::fmt; -use std::sync::RwLock; - -lazy_static! { - static ref STRING_TABLE: RwLock> = RwLock::new(HashMap::new()); -} - -#[derive(Clone, Copy, PartialEq, Eq)] -pub struct InternedString { - index: u64, -} - -impl InternedString { - /// Return the `InternedString` equivalent of the provided string. This function is slow, and - /// should be used somewhat sparingly. - pub fn new(s: &str) -> Self { - let mut biggest_index = 0; - let mut table = STRING_TABLE.write().unwrap(); - - for (k, v) in table.iter() { - if v == s { - return InternedString { index: *k }; - } - biggest_index = max(biggest_index, *k); - } - - let res = biggest_index + 1; - table.insert(res, s.to_string()); - InternedString { index: res } - } -} - -impl fmt::Debug for InternedString { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match STRING_TABLE.read().unwrap().get(&self.index) { - None => write!(f, ""), - Some(x) => write!(f, "{:?}", x), - } - } -} - -impl fmt::Display for InternedString { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match STRING_TABLE.read().unwrap().get(&self.index) { - None => write!(f, ""), - Some(x) => write!(f, "{}", x), - } - } -} - -impl PartialOrd for InternedString { - fn partial_cmp(&self, other: &InternedString) -> Option { - let table = STRING_TABLE.read().unwrap(); - - if let Some(me) = table.get(&self.index) { - if let Some(them) = table.get(&other.index) { - return me.partial_cmp(them); - } - } - - None - } -}