Switch fully to ArcIntern.

This commit is contained in:
2023-01-12 09:14:28 -08:00
parent cd3247ac9f
commit 228f447a06
5 changed files with 6 additions and 74 deletions

View File

@@ -4,6 +4,5 @@ pub mod errors;
pub mod passes; pub mod passes;
pub mod runtime; pub mod runtime;
pub mod syntax; pub mod syntax;
pub mod util;
pub mod variable_map; pub mod variable_map;
pub mod warnings; pub mod warnings;

View File

@@ -1,7 +1,7 @@
use crate::syntax::ast::{Program,Statement,Expression,Value}; use crate::syntax::ast::{Program,Statement,Expression,Value};
use crate::syntax::tokens::Token; use crate::syntax::tokens::Token;
use crate::syntax::token_stream::{LexerError, Location}; use crate::syntax::token_stream::{LexerError, Location};
use crate::util::istring::InternedString; use internment::ArcIntern;
grammar; grammar;
@@ -21,7 +21,7 @@ extern {
"/" => Token::Operator('/'), "/" => Token::Operator('/'),
"<num>" => Token::Number((<Option<u8>>,<i128>)), "<num>" => Token::Number((<Option<u8>>,<i128>)),
"<var>" => Token::Variable(<InternedString>), "<var>" => Token::Variable(<ArcIntern<String>>),
} }
} }

View File

@@ -1,4 +1,4 @@
use crate::util::istring::InternedString; use internment::ArcIntern;
use logos::{Lexer, Logos}; use logos::{Lexer, Logos};
use std::fmt; use std::fmt;
use std::num::ParseIntError; use std::num::ParseIntError;
@@ -24,8 +24,8 @@ pub enum Token {
#[regex(r"[0-9]+", |v| parse_number(None, v))] #[regex(r"[0-9]+", |v| parse_number(None, v))]
Number((Option<u8>, i128)), Number((Option<u8>, i128)),
#[regex(r"[a-z][a-zA-Z0-9_]*", |v| InternedString::new(v.slice()))] #[regex(r"[a-z][a-zA-Z0-9_]*", |v| ArcIntern::new(v.slice().to_string()))]
Variable(InternedString), Variable(ArcIntern<String>),
#[error] #[error]
#[regex(r"[ \t\r\n\f]+", logos::skip)] #[regex(r"[ \t\r\n\f]+", logos::skip)]
@@ -57,7 +57,7 @@ impl fmt::Display for Token {
#[cfg(test)] #[cfg(test)]
impl Token { impl Token {
pub(crate) fn var(s: &str) -> Token { pub(crate) fn var(s: &str) -> Token {
Token::Variable(InternedString::new(s)) Token::Variable(ArcIntern::new(s.to_string()))
} }
} }

View File

@@ -1 +0,0 @@
pub mod istring;

View File

@@ -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<HashMap<u64, String>> = 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, "<BROKEN-INTERN>"),
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, "<BROKEN-INTERN>"),
Some(x) => write!(f, "{}", x),
}
}
}
impl PartialOrd<InternedString> for InternedString {
fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
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
}
}