Skip to content

Commit

Permalink
first draft for import
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Jul 31, 2023
1 parent aa949b4 commit 865f1b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
53 changes: 28 additions & 25 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Package {
name: Symbol,
interned: HashSet<String>,
resolved: HashMap<String, Symbol>,
resolvable: HashSet<Symbol>,
}

impl Package {
Expand All @@ -15,14 +16,10 @@ impl Package {
name,
interned: Default::default(),
resolved: Default::default(),
resolvable: Default::default(),
}
}

#[inline]
pub fn root() -> Self {
Self::new(Symbol::root())
}

#[inline]
pub fn name(&self) -> &Symbol {
&self.name
Expand All @@ -33,29 +30,35 @@ impl Package {
self.resolved.get(symbol_name)
}

pub fn intern(&mut self, symbol_name: &str) -> &Symbol {
if self.interned.contains(symbol_name) {
self.resolve(symbol_name)
.expect("symbol must have been interned")
} else {
let symbol_name_clone = symbol_name.to_string();
let symbol = self.name.direct_child(symbol_name_clone.clone());
self.interned.insert(symbol_name_clone.clone());
self.resolved.insert(symbol_name_clone, symbol);
self.resolve(symbol_name).unwrap()
}
pub fn intern(&mut self, symbol_name: String) -> &Symbol {
let symbol = self
.resolved
.entry(symbol_name)
.or_insert_with_key(|symbol_name| {
self.interned.insert(symbol_name.into());
self.name.direct_child(symbol_name)
});
self.resolvable.insert(symbol.clone());
symbol
}

pub fn import_symbols(&mut self, package_name: &Symbol, symbol_names: &HashSet<String>) {
symbol_names.iter().for_each(|symbol_name| {
let symbol_name = symbol_name.to_string();
let symbol = package_name.direct_child(symbol_name.clone());
self.resolved.insert(symbol_name, symbol);
})
#[inline]
fn can_resolve(&self, symbol: &Symbol) -> bool {
self.resolvable.contains(symbol)
}

#[inline]
pub fn use_package(&mut self, package: &Self) {
self.import_symbols(&package.name, &package.interned);
pub fn import(&mut self, symbols: &[Symbol]) {
symbols.iter().for_each(|symbol| {
if !self.can_resolve(symbol) {
let symbol_name = if symbol.is_root() {
// is this correct?
String::default()
} else {
symbol.path[symbol.path.len() - 1].clone()
};
self.resolved.insert(symbol_name, symbol.clone());
self.resolvable.insert(symbol.clone());
}
})
}
}
14 changes: 7 additions & 7 deletions src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl Symbol {
}

pub fn direct_parent(&self) -> Option<Symbol> {
if self.path.is_empty() {
if self.is_root() {
None
} else {
Some(Self {
Expand All @@ -209,9 +209,9 @@ impl Symbol {
}
}

pub fn direct_child(&self, child: String) -> Symbol {
pub fn direct_child(&self, child: &str) -> Symbol {
let mut path = self.path.clone();
path.push(child);
path.push(child.into());
Self { path }
}
}
Expand Down Expand Up @@ -459,8 +459,8 @@ pub mod test {
fn test_sym() {
assert_eq!("a.b.c", format!("{}", Symbol::new(&["a", "b", "c"])));
let root = Symbol::root();
let a = root.direct_child("a".into());
let a_b = a.direct_child("b".into());
let a = root.direct_child("a");
let a_b = a.direct_child("b");
let a_b_path = vec!["a", "b"];

assert_eq!("a", format!("{}", a));
Expand All @@ -476,8 +476,8 @@ pub mod test {
let root = Symbol::root();
let key_root = Symbol::new(&["keyword"]);

let apple = root.direct_child("apple".into());
let orange = key_root.direct_child("orange".into());
let apple = root.direct_child("apple");
let orange = key_root.direct_child("orange");

assert_eq!("apple", format!("{}", apple));
assert_eq!(":orange", format!("{}", orange));
Expand Down

0 comments on commit 865f1b7

Please sign in to comment.