forked from Orchid/orchid
Transfer commit
This commit is contained in:
@@ -1,73 +1,80 @@
|
||||
use std::collections::HashMap;
|
||||
use mappable_rc::Mrc;
|
||||
use std::rc::Rc;
|
||||
use itertools::Itertools;
|
||||
use lasso::Spur;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::utils::{Stackframe, to_mrc_slice};
|
||||
use crate::utils::Stackframe;
|
||||
|
||||
use crate::ast::{Expr, Clause};
|
||||
|
||||
type ImportMap = HashMap<String, Mrc<[String]>>;
|
||||
type ImportMap = HashMap<Spur, Rc<Vec<Spur>>>;
|
||||
|
||||
#[derive(Debug, Clone, Error)]
|
||||
pub enum ResolutionError<Err> {
|
||||
#[error("Reference cycle at {0:?}")]
|
||||
Cycle(Vec<Mrc<[String]>>),
|
||||
Cycle(Vec<Rc<Vec<Spur>>>),
|
||||
#[error("No module provides {0:?}")]
|
||||
NoModule(Mrc<[String]>),
|
||||
NoModule(Rc<Vec<Spur>>),
|
||||
#[error(transparent)]
|
||||
Delegate(#[from] Err)
|
||||
}
|
||||
|
||||
type ResolutionResult<E> = Result<Mrc<[String]>, ResolutionError<E>>;
|
||||
type ResolutionResult<E> = Result<Rc<Vec<Spur>>, ResolutionError<E>>;
|
||||
|
||||
/// Recursively resolves symbols to their original names in expressions while caching every
|
||||
/// resolution. This makes the resolution process lightning fast and invalidation completely
|
||||
/// impossible since the intermediate steps of a resolution aren't stored.
|
||||
/// Recursively resolves symbols to their original names in expressions
|
||||
/// while caching every resolution. This makes the resolution process
|
||||
/// lightning fast and invalidation completely impossible since
|
||||
/// the intermediate steps of a resolution aren't stored.
|
||||
pub struct NameResolver<FSplit, FImps, E> {
|
||||
cache: HashMap<Mrc<[String]>, ResolutionResult<E>>,
|
||||
get_modname: FSplit,
|
||||
cache: HashMap<Rc<Vec<Spur>>, ResolutionResult<E>>,
|
||||
split: FSplit,
|
||||
get_imports: FImps
|
||||
}
|
||||
|
||||
impl<FSplit, FImps, E> NameResolver<FSplit, FImps, E>
|
||||
where
|
||||
FSplit: FnMut(Mrc<[String]>) -> Option<Mrc<[String]>>,
|
||||
FImps: FnMut(Mrc<[String]>) -> Result<ImportMap, E>,
|
||||
FSplit: FnMut(Rc<Vec<Spur>>) -> Option<(Rc<Vec<Spur>>, Rc<Vec<Spur>>)>,
|
||||
FImps: FnMut(Rc<Vec<Spur>>) -> Result<ImportMap, E>,
|
||||
E: Clone
|
||||
{
|
||||
pub fn new(get_modname: FSplit, get_imports: FImps) -> Self {
|
||||
pub fn new(split: FSplit, get_imports: FImps) -> Self {
|
||||
Self {
|
||||
cache: HashMap::new(),
|
||||
get_modname,
|
||||
split,
|
||||
get_imports
|
||||
}
|
||||
}
|
||||
|
||||
fn split(&self, symbol: Rc<Vec<Spur>>)
|
||||
-> Result<(Rc<Vec<Spur>>, Rc<Vec<Spur>>), ResolutionError<E>> {
|
||||
let (path, name) = (self.split)(symbol.clone())
|
||||
.ok_or_else(|| ResolutionError::NoModule(symbol.clone()))?;
|
||||
if name.is_empty() {
|
||||
panic!("get_modname matched all to module and nothing to name")
|
||||
}
|
||||
Ok((path, name))
|
||||
}
|
||||
|
||||
/// Obtains a symbol's originnal name
|
||||
/// Uses a substack to detect loops
|
||||
fn find_origin_rec(
|
||||
&mut self,
|
||||
symbol: Mrc<[String]>,
|
||||
import_path: Stackframe<Mrc<[String]>>
|
||||
) -> Result<Mrc<[String]>, ResolutionError<E>> {
|
||||
symbol: Rc<Vec<Spur>>,
|
||||
import_path: Stackframe<Rc<Vec<Spur>>>
|
||||
) -> Result<Rc<Vec<Spur>>, ResolutionError<E>> {
|
||||
if let Some(cached) = self.cache.get(&symbol) {
|
||||
return cached.as_ref().map_err(|e| e.clone()).map(Mrc::clone)
|
||||
return cached.clone()
|
||||
}
|
||||
// The imports and path of the referenced file and the local name
|
||||
let path = (self.get_modname)(Mrc::clone(&symbol)).ok_or_else(|| {
|
||||
ResolutionError::NoModule(Mrc::clone(&symbol))
|
||||
})?;
|
||||
let name = &symbol[path.len()..];
|
||||
if name.is_empty() {
|
||||
panic!("get_modname matched all to module and nothing to name in {:?}", import_path)
|
||||
}
|
||||
let imports = (self.get_imports)(Mrc::clone(&path))?;
|
||||
let (path, name) = self.split(symbol)?;
|
||||
let imports = (self.get_imports)(path.clone())?;
|
||||
let result = if let Some(source) = imports.get(&name[0]) {
|
||||
let new_sym: Vec<String> = source.iter().chain(name.iter()).cloned().collect();
|
||||
let new_sym = source.iter().chain(name.iter()).cloned().collect_vec();
|
||||
if import_path.iter().any(|el| el.as_ref() == new_sym.as_slice()) {
|
||||
Err(ResolutionError::Cycle(import_path.iter().map(Mrc::clone).collect()))
|
||||
Err(ResolutionError::Cycle(import_path.iter().cloned().collect()))
|
||||
} else {
|
||||
self.find_origin_rec(to_mrc_slice(new_sym), import_path.push(Mrc::clone(&symbol)))
|
||||
self.find_origin_rec(Rc::new(new_sym), import_path.push(symbol.clone()))
|
||||
}
|
||||
} else {
|
||||
Ok(symbol.clone()) // If not imported, it must be locally defined
|
||||
@@ -81,30 +88,27 @@ where
|
||||
}
|
||||
|
||||
fn process_exprmrcopt_rec(&mut self,
|
||||
exbo: &Option<Mrc<Expr>>
|
||||
) -> Result<Option<Mrc<Expr>>, ResolutionError<E>> {
|
||||
exbo.iter().map(|exb| Ok(Mrc::new(self.process_expression_rec(exb.as_ref())?)))
|
||||
exbo: &Option<Rc<Expr>>
|
||||
) -> Result<Option<Rc<Expr>>, ResolutionError<E>> {
|
||||
exbo.iter().map(|exb| Ok(Rc::new(self.process_expression_rec(exb)?)))
|
||||
.next().transpose()
|
||||
}
|
||||
|
||||
fn process_clause_rec(&mut self, tok: &Clause) -> Result<Clause, ResolutionError<E>> {
|
||||
Ok(match tok {
|
||||
Clause::S(c, exv) => Clause::S(*c, to_mrc_slice(
|
||||
exv.as_ref().iter().map(|e| self.process_expression_rec(e))
|
||||
.collect::<Result<Vec<Expr>, ResolutionError<E>>>()?
|
||||
Clause::S(c, exv) => Clause::S(*c, Rc::new(
|
||||
exv.iter().map(|e| self.process_expression_rec(e))
|
||||
.collect::<Result<_, _>>()?
|
||||
)),
|
||||
Clause::Lambda(name, typ, body) => Clause::Lambda(name.clone(),
|
||||
to_mrc_slice(self.process_exprv_rec(typ.as_ref())?),
|
||||
to_mrc_slice(self.process_exprv_rec(body.as_ref())?)
|
||||
Rc::new(self.process_exprv_rec(&typ)?),
|
||||
Rc::new(self.process_exprv_rec(&body)?)
|
||||
),
|
||||
Clause::Auto(name, typ, body) => Clause::Auto(name.clone(),
|
||||
to_mrc_slice(self.process_exprv_rec(typ.as_ref())?),
|
||||
to_mrc_slice(self.process_exprv_rec(body.as_ref())?)
|
||||
Rc::new(self.process_exprv_rec(&typ)?),
|
||||
Rc::new(self.process_exprv_rec(&body)?)
|
||||
),
|
||||
Clause::Name{local, qualified} => Clause::Name{
|
||||
local: local.clone(),
|
||||
qualified: self.find_origin(Mrc::clone(qualified))?
|
||||
},
|
||||
Clause::Name(name) => Clause::Name(self.find_origin(name.clone())?),
|
||||
x => x.clone()
|
||||
})
|
||||
}
|
||||
@@ -112,12 +116,14 @@ where
|
||||
fn process_expression_rec(&mut self, Expr(token, typ): &Expr) -> Result<Expr, ResolutionError<E>> {
|
||||
Ok(Expr(
|
||||
self.process_clause_rec(token)?,
|
||||
typ.iter().map(|t| self.process_clause_rec(t)).collect::<Result<_, _>>()?
|
||||
Rc::new(typ.iter().map(|t| {
|
||||
self.process_clause_rec(t)
|
||||
}).collect::<Result<_, _>>()?)
|
||||
))
|
||||
}
|
||||
|
||||
pub fn find_origin(&mut self, symbol: Mrc<[String]>) -> Result<Mrc<[String]>, ResolutionError<E>> {
|
||||
self.find_origin_rec(Mrc::clone(&symbol), Stackframe::new(symbol))
|
||||
pub fn find_origin(&mut self, symbol: Rc<Vec<Spur>>) -> Result<Rc<Vec<Spur>>, ResolutionError<E>> {
|
||||
self.find_origin_rec(symbol.clone(), Stackframe::new(symbol))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
Reference in New Issue
Block a user