Skip to content

Commit

Permalink
update intern_symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 4, 2023
1 parent 9d3ef2c commit 9c2ce35
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/lem/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ impl<F: LurkField> Store<F> {
}
}

pub fn intern_symbol(&mut self, s: &Symbol) -> Ptr<F> {
let path_ptr = self.intern_symbol_path(s.path());
if s == &lurk_sym("nil") {
pub fn intern_symbol(&mut self, sym: &Symbol) -> Ptr<F> {
let path_ptr = self.intern_symbol_path(sym.path());
if sym == &lurk_sym("nil") {
path_ptr.cast(Tag::Expr(Nil))
} else if !s.is_keyword() {
} else if !sym.is_keyword() {
path_ptr
} else {
path_ptr.cast(Tag::Expr(Key))
Expand Down
73 changes: 30 additions & 43 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,53 +481,40 @@ impl<F: LurkField> Store<F> {
.fold(self.nil_ptr(), |acc, elt| self.intern_cons(*elt, acc))
}

pub fn intern_symbol_path(&mut self, path: &[String]) -> Ptr<F> {
path.iter().fold(self.symnil(), |acc, s| {
let s_ptr = self.intern_string(s);
self.intern_symcons(s_ptr, acc)
})
}

pub fn intern_symbol(&mut self, sym: &Symbol) -> Ptr<F> {
let ptr = if sym.path().is_empty() {
Ptr::null(ExprTag::Sym)
} else {
let mut ptr = self.symnil();
for s in sym.path().iter() {
let str_ptr = self.intern_string(s);
ptr = self.intern_symcons(str_ptr, ptr);
}
if *sym == lurk_sym("nil") {
Ptr {
tag: ExprTag::Nil,
raw: ptr.raw,
_f: ptr._f,
}
} else if sym.is_keyword() {
Ptr {
tag: ExprTag::Key,
raw: ptr.raw,
_f: ptr._f,
}
} else {
ptr
match self.symbol_cache.get(sym) {
Some(ptr) => **ptr,
None => {
use crate::tag::ExprTag::{Key, Nil};
let path_ptr = self.intern_symbol_path(sym.path());
let sym_ptr = if sym == &lurk_sym("nil") {
Ptr {
tag: Nil,
raw: path_ptr.raw,
_f: path_ptr._f,
}
} else if !sym.is_keyword() {
path_ptr
} else {
Ptr {
tag: Key,
raw: path_ptr.raw,
_f: path_ptr._f,
}
};
self.symbol_cache.insert(sym.clone(), sym_ptr.into());
sym_ptr
}
};
self.symbol_cache.insert(sym.clone(), Box::new(ptr));
ptr
}
}

// pub fn get_sym(&self, sym: &Symbol) -> Option<Ptr<F>> {
// let ptr = self.symbol_cache.get(sym).cloned()?;
// if *sym == lurk_sym("nil") {
// Some(Ptr {
// tag: ExprTag::Nil,
// raw: ptr.raw,
// _f: ptr._f,
// })
// } else {
// Some(*ptr)
// }
// }

// pub fn get_lurk_sym<T: AsRef<str>>(&self, name: T) -> Option<Ptr<F>> {
// let sym = Symbol::lurk_sym(name.as_ref());
// self.get_sym(&sym)
// }

pub fn intern_num<T: Into<Num<F>>>(&mut self, num: T) -> Ptr<F> {
let num = num.into();
let num = match num {
Expand Down

0 comments on commit 9c2ce35

Please sign in to comment.