mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-09-11 11:56:54 +03:00
Fix import order dependent bugs
This commit is contained in:
parent
92c75d7322
commit
d1072da0a9
@ -1,6 +1,6 @@
|
|||||||
use super::{
|
use super::{
|
||||||
parser::{ParseBook, TermParser},
|
parser::{ParseBook, TermParser},
|
||||||
Book,
|
Book, Name,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
diagnostics::{Diagnostics, DiagnosticsConfig},
|
diagnostics::{Diagnostics, DiagnosticsConfig},
|
||||||
@ -23,24 +23,16 @@ pub fn load_file_to_book(
|
|||||||
pub fn load_to_book<T: Display>(
|
pub fn load_to_book<T: Display>(
|
||||||
origin: T,
|
origin: T,
|
||||||
code: &str,
|
code: &str,
|
||||||
mut package_loader: impl PackageLoader,
|
package_loader: impl PackageLoader,
|
||||||
diag: DiagnosticsConfig,
|
diag: DiagnosticsConfig,
|
||||||
) -> Result<Book, Diagnostics> {
|
) -> Result<Book, Diagnostics> {
|
||||||
let builtins = ParseBook::builtins();
|
let builtins = ParseBook::builtins();
|
||||||
let mut book = do_parse_book(code, origin, builtins)?;
|
let book = do_parse_book(code, origin, builtins)?;
|
||||||
|
book.load_imports(package_loader, diag)
|
||||||
let diag = &mut Diagnostics::new(diag);
|
|
||||||
book.imports.load_imports(None, &mut package_loader, diag)?;
|
|
||||||
book.apply_imports(diag)?;
|
|
||||||
eprint!("{}", diag);
|
|
||||||
|
|
||||||
let mut book = book.to_fun()?;
|
|
||||||
book.desugar_ctr_use();
|
|
||||||
|
|
||||||
Ok(book)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn do_parse_book<T: Display>(code: &str, origin: T, book: ParseBook) -> Result<ParseBook, String> {
|
pub fn do_parse_book<T: Display>(code: &str, origin: T, mut book: ParseBook) -> Result<ParseBook, String> {
|
||||||
|
book.source = Name::new(format!("{origin}"));
|
||||||
TermParser::new(code).parse_book(book, false).map_err(|e| format!("In {} :\n{}", origin, e))
|
TermParser::new(code).parse_book(book, false).map_err(|e| format!("In {} :\n{}", origin, e))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1070,7 +1070,9 @@ impl Name {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn def_name_from_generated(&self) -> Name {
|
pub fn def_name_from_generated(&self) -> Name {
|
||||||
if let Some((nam, _)) = self.split_once("__") {
|
if let Some(nam) = self.strip_prefix("__") {
|
||||||
|
Name::new(nam)
|
||||||
|
} else if let Some((nam, _)) = self.split_once("__") {
|
||||||
Name::new(nam)
|
Name::new(nam)
|
||||||
} else {
|
} else {
|
||||||
self.clone()
|
self.clone()
|
||||||
|
@ -36,6 +36,9 @@ pub struct ParseBook {
|
|||||||
|
|
||||||
/// Imported packages to be loaded in the program
|
/// Imported packages to be loaded in the program
|
||||||
pub imports: Imports,
|
pub imports: Imports,
|
||||||
|
|
||||||
|
/// Source of the book
|
||||||
|
pub source: Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParseBook {
|
impl ParseBook {
|
||||||
|
@ -19,7 +19,7 @@ impl ParseBook {
|
|||||||
self.fun_defs.insert(name, def.to_fun(source)?);
|
self.fun_defs.insert(name, def.to_fun(source)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ParseBook { fun_defs: defs, imp_defs: _, hvm_defs, adts, ctrs, imports } = self;
|
let ParseBook { fun_defs: defs, hvm_defs, adts, ctrs, imports, .. } = self;
|
||||||
Ok(Book { defs, hvm_defs, adts, ctrs, entrypoint: None, imports: imports.to_names() })
|
Ok(Book { defs, hvm_defs, adts, ctrs, entrypoint: None, imports: imports.to_names() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
303
src/imports.rs
303
src/imports.rs
@ -1,11 +1,14 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
diagnostics::{Diagnostics, WarningType},
|
diagnostics::{Diagnostics, DiagnosticsConfig, WarningType},
|
||||||
fun::{load_book::do_parse_book, parser::ParseBook, Adt, Definition, Name, Source, Term},
|
fun::{load_book::do_parse_book, parser::ParseBook, Adt, Book, Definition, Name, Rule, Source, Term},
|
||||||
imp::{Expr, Stmt},
|
imp::{Expr, Stmt},
|
||||||
};
|
};
|
||||||
use indexmap::{map::Entry, IndexMap};
|
use indexmap::{map::Entry, IndexMap};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::{collections::HashSet, path::PathBuf};
|
use std::{
|
||||||
|
collections::{HashSet, VecDeque},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct Imports {
|
pub struct Imports {
|
||||||
@ -15,10 +18,25 @@ pub struct Imports {
|
|||||||
/// Map from bound names to source package.
|
/// Map from bound names to source package.
|
||||||
map: ImportsMap,
|
map: ImportsMap,
|
||||||
|
|
||||||
/// Imported packages to be loaded in the program.
|
/// Imported packages names.
|
||||||
/// When loaded, the book contents are drained to the parent book,
|
pkgs: Vec<Name>,
|
||||||
/// adjusting def names accordingly.
|
}
|
||||||
pkgs: IndexMap<Name, ParseBook>,
|
|
||||||
|
impl Imports {
|
||||||
|
pub fn add_import(&mut self, import: Name, import_type: ImportType) {
|
||||||
|
self.names.push((import, import_type));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_names(self) -> Vec<(Name, ImportType)> {
|
||||||
|
self.names
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum ImportType {
|
||||||
|
Simple(Option<Name>),
|
||||||
|
List(Vec<(Name, Option<Name>)>),
|
||||||
|
Glob,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
@ -31,115 +49,172 @@ impl ImportsMap {
|
|||||||
fn iter(&self) -> impl DoubleEndedIterator<Item = (&Name, &Name)> {
|
fn iter(&self) -> impl DoubleEndedIterator<Item = (&Name, &Name)> {
|
||||||
self.binds.iter().map(|(n, u)| (n, &self.sources[*u]))
|
self.binds.iter().map(|(n, u)| (n, &self.sources[*u]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_bind(&mut self, name: Name, alias: Option<Name>, src: &str, diag: &mut Diagnostics) {
|
||||||
|
let aliased = alias.unwrap_or(name);
|
||||||
|
|
||||||
|
if let Some(old) = self.binds.get(&aliased) {
|
||||||
|
let old = &self.sources[*old];
|
||||||
|
let warn = format!("The import '{src}' shadows the imported name '{old}'");
|
||||||
|
diag.add_book_warning(warn, WarningType::ImportShadow);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.binds.insert(aliased, self.sources.len());
|
||||||
|
self.sources.push(Name::new(src));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Default)]
|
||||||
pub enum ImportType {
|
struct Packages {
|
||||||
Simple(Option<Name>),
|
books: IndexMap<Name, ParseBook>,
|
||||||
List(Vec<(Name, Option<Name>)>),
|
loaded_adts: IndexMap<Name, IndexMap<Name, Vec<Name>>>,
|
||||||
Glob,
|
load_queue: VecDeque<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Imports {
|
impl Packages {
|
||||||
pub fn add_import(&mut self, import: Name, import_type: ImportType) {
|
pub fn new(book: ParseBook) -> Self {
|
||||||
self.names.push((import, import_type));
|
Self {
|
||||||
|
books: IndexMap::from([(Name::default(), book)]),
|
||||||
|
load_queue: VecDeque::new(),
|
||||||
|
loaded_adts: IndexMap::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_names(self) -> Vec<(Name, ImportType)> {
|
fn get_book(&self, idx: usize) -> &ParseBook {
|
||||||
self.names
|
self.books.get_index(idx).unwrap().1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_book_mut(&mut self, idx: usize) -> &mut ParseBook {
|
||||||
|
self.books.get_index_mut(idx).unwrap().1
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_imports(
|
pub fn load_imports(
|
||||||
&mut self,
|
&mut self,
|
||||||
dir: Option<Name>,
|
|
||||||
loader: &mut impl PackageLoader,
|
loader: &mut impl PackageLoader,
|
||||||
diag: &mut Diagnostics,
|
diag: &mut Diagnostics,
|
||||||
) -> Result<(), Diagnostics> {
|
) -> Result<ParseBook, Diagnostics> {
|
||||||
diag.start_pass();
|
diag.start_pass();
|
||||||
|
|
||||||
for (src, imp_type) in &self.names {
|
self.load_imports_go(0, None, loader)?;
|
||||||
|
|
||||||
|
while let Some(idx) = self.load_queue.pop_front() {
|
||||||
|
let psrc = &self.get_book(idx).source;
|
||||||
|
let parent_dir = psrc.rsplit_once('/').map_or_else(|| psrc.clone(), |(s, _)| Name::new(s));
|
||||||
|
self.load_imports_go(idx, Some(parent_dir), loader)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx in 0..self.books.len() {
|
||||||
|
self.load_binds(idx, diag);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (_, book) = self.books.swap_remove_index(0).unwrap();
|
||||||
|
|
||||||
|
diag.fatal(book)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_imports_go(
|
||||||
|
&mut self,
|
||||||
|
idx: usize,
|
||||||
|
dir: Option<Name>,
|
||||||
|
loader: &mut impl PackageLoader,
|
||||||
|
) -> Result<(), Diagnostics> {
|
||||||
|
let names = self.get_book(idx).imports.names.clone();
|
||||||
|
|
||||||
|
for (src, imp_type) in names {
|
||||||
// TODO: Would this be correct when handling non-local imports? Do we want relative online sources?
|
// TODO: Would this be correct when handling non-local imports? Do we want relative online sources?
|
||||||
// TODO: Normalize paths when using `..`
|
// TODO: Normalize paths when using `..`
|
||||||
let src = dir.as_ref().map_or(src.clone(), |p| Name::new(format!("{}/{}", p, src)));
|
let src = dir.as_ref().map_or(src.clone(), |dir| Name::new(format!("{}/{}", dir, src)));
|
||||||
|
|
||||||
let packages = loader.load(src.clone(), imp_type)?;
|
let mut sources = IndexMap::new();
|
||||||
|
let packages = loader.load(src.clone(), &imp_type, &mut sources)?;
|
||||||
|
|
||||||
for (psrc, code) in packages {
|
self.get_book_mut(idx).imports.pkgs.extend(packages);
|
||||||
let mut module = do_parse_book(&code, &psrc, ParseBook::default())?;
|
|
||||||
let parent_dir = psrc.rsplit_once('/').map(|(s, _)| Name::new(s)).unwrap_or(psrc.clone());
|
for (psrc, code) in sources {
|
||||||
module.imports.load_imports(Some(parent_dir), loader, diag)?;
|
let module = do_parse_book(&code, &psrc, ParseBook::default())?;
|
||||||
self.pkgs.insert(psrc, module);
|
|
||||||
|
self.load_queue.push_back(self.books.len());
|
||||||
|
self.books.insert(psrc, module);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_binds(&mut self, idx: usize, diag: &mut Diagnostics) {
|
||||||
|
let book = self.get_book(idx);
|
||||||
|
let pkgs = book.imports.pkgs.iter();
|
||||||
|
let names = book.imports.names.iter();
|
||||||
|
let binds = pkgs.zip(names).map(|(src, (_, imp))| (src.clone(), imp.clone())).collect_vec();
|
||||||
|
|
||||||
|
for (src, imp_type) in binds {
|
||||||
match imp_type {
|
match imp_type {
|
||||||
ImportType::Simple(alias) => {
|
ImportType::Simple(alias) => {
|
||||||
let name = Name::new(src.split('/').last().unwrap());
|
let name = Name::new(src.split('/').last().unwrap());
|
||||||
let src = format!("{}/{}", src, name);
|
let src = format!("{}/{}", src, name);
|
||||||
add_bind(&mut self.map, name, alias.clone(), &src, diag);
|
|
||||||
|
let book = self.get_book_mut(idx);
|
||||||
|
book.imports.map.add_bind(name, alias, &src, diag);
|
||||||
}
|
}
|
||||||
ImportType::List(names) => {
|
ImportType::List(names) => {
|
||||||
let book = self.pkgs.get(&src).unwrap();
|
let bound_book = self.books.get(&src).unwrap();
|
||||||
|
|
||||||
for (sub, alias) in names {
|
for (sub, _) in &names {
|
||||||
if !book.top_level_names().contains(sub) {
|
if !bound_book.top_level_names().contains(&sub) {
|
||||||
let err = format!("Package '{src}' does not contain the top level name '{sub}'");
|
let err = format!("Package '{src}' does not contain the top level name '{sub}'");
|
||||||
diag.add_book_error(err);
|
diag.add_book_error(err);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let book = self.get_book_mut(idx);
|
||||||
|
|
||||||
|
for (sub, alias) in names {
|
||||||
let src = format!("{}/{}", src, sub);
|
let src = format!("{}/{}", src, sub);
|
||||||
add_bind(&mut self.map, sub.clone(), alias.clone(), &src, diag);
|
book.imports.map.add_bind(sub, alias, &src, diag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ImportType::Glob => {
|
ImportType::Glob => {
|
||||||
let book = self.pkgs.get(&src).unwrap();
|
let bound_book = self.books.get(&src).unwrap();
|
||||||
|
let names = bound_book.top_level_names().cloned().collect_vec();
|
||||||
|
|
||||||
for sub in book.top_level_names() {
|
let book = self.get_book_mut(idx);
|
||||||
|
|
||||||
|
for sub in names {
|
||||||
let src = format!("{}/{}", src, sub);
|
let src = format!("{}/{}", src, sub);
|
||||||
add_bind(&mut self.map, sub.clone(), None, &src, diag);
|
book.imports.map.add_bind(sub, None, &src, diag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
diag.fatal(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_bind(map: &mut ImportsMap, name: Name, alias: Option<Name>, src: &str, diag: &mut Diagnostics) {
|
|
||||||
let aliased = alias.unwrap_or(name);
|
|
||||||
|
|
||||||
if let Some(old) = map.binds.get(&aliased) {
|
|
||||||
let old = &map.sources[*old];
|
|
||||||
let warn = format!("The import '{src}' shadows the imported name '{old}'");
|
|
||||||
diag.add_book_warning(warn, WarningType::ImportShadow);
|
|
||||||
}
|
|
||||||
|
|
||||||
map.binds.insert(aliased, map.sources.len());
|
|
||||||
map.sources.push(Name::new(src))
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ParseBook {
|
impl ParseBook {
|
||||||
fn top_level_names(&self) -> impl Iterator<Item = &Name> {
|
pub fn load_imports(
|
||||||
let imp_defs = self.imp_defs.keys();
|
self,
|
||||||
let fun_defs = self.fun_defs.keys();
|
mut loader: impl PackageLoader,
|
||||||
let adts = self.adts.keys();
|
diag_config: DiagnosticsConfig,
|
||||||
let ctrs = self.ctrs.keys();
|
) -> Result<Book, Diagnostics> {
|
||||||
|
let diag = &mut Diagnostics::new(diag_config);
|
||||||
|
let pkgs = &mut Packages::new(self);
|
||||||
|
let mut book = pkgs.load_imports(&mut loader, diag)?;
|
||||||
|
|
||||||
imp_defs.chain(fun_defs).chain(adts).chain(ctrs)
|
book.apply_imports(None, diag, pkgs)?;
|
||||||
|
eprint!("{}", diag);
|
||||||
|
|
||||||
|
let mut book = book.to_fun()?;
|
||||||
|
book.desugar_ctr_use();
|
||||||
|
Ok(book)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_imports(&mut self, diag: &mut Diagnostics) -> Result<(), Diagnostics> {
|
fn apply_imports(
|
||||||
self.apply_imports_go(None, diag)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn apply_imports_go(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
main_imports: Option<&ImportsMap>,
|
main_imports: Option<&ImportsMap>,
|
||||||
diag: &mut Diagnostics,
|
diag: &mut Diagnostics,
|
||||||
|
pkgs: &mut Packages,
|
||||||
) -> Result<(), Diagnostics> {
|
) -> Result<(), Diagnostics> {
|
||||||
self.load_packages(main_imports, diag)?;
|
self.load_packages(main_imports, diag, pkgs)?;
|
||||||
self.apply_import_binds(main_imports, diag);
|
self.apply_import_binds(main_imports, diag, pkgs);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,21 +224,26 @@ impl ParseBook {
|
|||||||
&mut self,
|
&mut self,
|
||||||
main_imports: Option<&ImportsMap>,
|
main_imports: Option<&ImportsMap>,
|
||||||
diag: &mut Diagnostics,
|
diag: &mut Diagnostics,
|
||||||
|
pkgs: &mut Packages,
|
||||||
) -> Result<(), Diagnostics> {
|
) -> Result<(), Diagnostics> {
|
||||||
diag.start_pass();
|
diag.start_pass();
|
||||||
|
|
||||||
for (src, mut package) in std::mem::take(&mut self.imports.pkgs) {
|
for src in self.imports.pkgs.clone() {
|
||||||
|
let Some(mut package) = pkgs.books.swap_remove(&src) else { continue };
|
||||||
|
|
||||||
// Can not be done outside the loop/function because of the borrow checker.
|
// Can not be done outside the loop/function because of the borrow checker.
|
||||||
// Just serves to pass only the import map of the first call to `apply_imports_go`.
|
// Just serves to pass only the import map of the first call to `apply_imports_go`.
|
||||||
let main_imports = main_imports.unwrap_or(&self.imports.map);
|
let main_imports = main_imports.unwrap_or(&self.imports.map);
|
||||||
|
|
||||||
package.apply_imports_go(Some(main_imports), diag)?;
|
package.apply_imports(Some(main_imports), diag, pkgs)?;
|
||||||
let new_adts = package.apply_adts(&src, main_imports);
|
let new_adts = package.apply_adts(&src, main_imports);
|
||||||
package.apply_defs(&src, main_imports);
|
package.apply_defs(&src, main_imports);
|
||||||
|
|
||||||
let book = package.to_fun()?;
|
let book = package.to_fun()?;
|
||||||
|
|
||||||
for (name, adt) in new_adts {
|
for (name, adt) in new_adts {
|
||||||
|
let adts = pkgs.loaded_adts.entry(src.clone()).or_default();
|
||||||
|
adts.insert(name.clone(), adt.ctrs.keys().cloned().collect_vec());
|
||||||
self.add_imported_adt(name, adt, diag);
|
self.add_imported_adt(name, adt, diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,15 +258,20 @@ impl ParseBook {
|
|||||||
/// Applies a chain of `use bind = src` to every local definition.
|
/// Applies a chain of `use bind = src` to every local definition.
|
||||||
///
|
///
|
||||||
/// Must be used after `load_packages`
|
/// Must be used after `load_packages`
|
||||||
fn apply_import_binds(&mut self, main_imports: Option<&ImportsMap>, diag: &mut Diagnostics) {
|
fn apply_import_binds(
|
||||||
|
&mut self,
|
||||||
|
main_imports: Option<&ImportsMap>,
|
||||||
|
diag: &mut Diagnostics,
|
||||||
|
pkgs: &Packages,
|
||||||
|
) {
|
||||||
// Can not be done outside the function because of the borrow checker.
|
// Can not be done outside the function because of the borrow checker.
|
||||||
// Just serves to pass only the import map of the first call to `apply_imports_go`.
|
// Just serves to pass only the import map of the first call to `apply_imports_go`.
|
||||||
let main_imports = main_imports.unwrap_or(&self.imports.map);
|
let main_imports = main_imports.unwrap_or(&self.imports.map);
|
||||||
|
|
||||||
let mut local_imports: IndexMap<Name, Name> = IndexMap::new();
|
let mut local_imports: IndexMap<Name, Name> = IndexMap::new();
|
||||||
|
|
||||||
// Collect local imports binds, surrounded by `__` if not imported by the main book.
|
// Collect local imports binds, starting with `__` if not imported by the main book.
|
||||||
for (bind, src) in self.imports.map.iter().rev() {
|
'outer: for (bind, src) in self.imports.map.iter().rev() {
|
||||||
if self.contains_def(bind) {
|
if self.contains_def(bind) {
|
||||||
let warn = format!("The local definition '{bind}' shadows the imported name '{src}'");
|
let warn = format!("The local definition '{bind}' shadows the imported name '{src}'");
|
||||||
diag.add_book_warning(warn, WarningType::ImportShadow);
|
diag.add_book_warning(warn, WarningType::ImportShadow);
|
||||||
@ -206,24 +291,32 @@ impl ParseBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let nam =
|
let nam =
|
||||||
if main_imports.sources.contains(src) { src.clone() } else { Name::new(format!("__{}__", src)) };
|
if main_imports.sources.contains(src) { src.clone() } else { Name::new(format!("__{}", src)) };
|
||||||
|
|
||||||
if let Some(adt) = &self.adts.get(&nam) {
|
if let Some(adt) = self.adts.get(&nam) {
|
||||||
for (ctr, _) in adt.ctrs.iter().rev() {
|
for (ctr, _) in adt.ctrs.iter().rev() {
|
||||||
let ctr_name = ctr.rsplit("__").nth(1).unwrap_or(ctr.as_ref());
|
add_bind_to_map(&mut local_imports, bind, src, ctr);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(a) = ctr_name.strip_prefix(src.as_ref()) {
|
for pkg in &self.imports.pkgs {
|
||||||
let bind = Name::new(format!("{}{}", bind, a));
|
if let Some(book) = pkgs.loaded_adts.get(pkg) {
|
||||||
local_imports.insert(bind, ctr.clone());
|
if let Some(ctrs) = book.get(&nam) {
|
||||||
|
for ctr in ctrs.iter().rev() {
|
||||||
|
add_bind_to_map(&mut local_imports, bind, src, ctr);
|
||||||
|
}
|
||||||
|
continue 'outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
local_imports.insert(bind.clone(), nam);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local_imports.insert(bind.clone(), nam);
|
||||||
}
|
}
|
||||||
|
|
||||||
for def in self.fun_defs.values_mut().filter(|d| matches!(d.source, Source::Local(..))) {
|
for def in self.fun_defs.values_mut().filter(|d| matches!(d.source, Source::Local(..))) {
|
||||||
for rule in &mut def.rules {
|
for rule in &mut def.rules {
|
||||||
|
rename_ctr_patterns(rule, &local_imports);
|
||||||
rule.body = std::mem::take(&mut rule.body).fold_uses(local_imports.iter());
|
rule.body = std::mem::take(&mut rule.body).fold_uses(local_imports.iter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,7 +349,7 @@ impl ParseBook {
|
|||||||
|
|
||||||
if mangle_ctr {
|
if mangle_ctr {
|
||||||
mangle_adt_name = true;
|
mangle_adt_name = true;
|
||||||
ctr_name = Name::new(format!("__{}__", ctr_name));
|
ctr_name = Name::new(format!("__{}", ctr_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
ctrs_map.insert(ctr, ctr_name.clone());
|
ctrs_map.insert(ctr, ctr_name.clone());
|
||||||
@ -264,7 +357,7 @@ impl ParseBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if mangle_adt_name {
|
if mangle_adt_name {
|
||||||
name = Name::new(format!("__{}__", name));
|
name = Name::new(format!("__{}", name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,6 +373,7 @@ impl ParseBook {
|
|||||||
|
|
||||||
for def in self.fun_defs.values_mut().filter(|d| matches!(d.source, Source::Local(..))) {
|
for def in self.fun_defs.values_mut().filter(|d| matches!(d.source, Source::Local(..))) {
|
||||||
for rule in &mut def.rules {
|
for rule in &mut def.rules {
|
||||||
|
rename_ctr_patterns(rule, &ctrs_map);
|
||||||
rule.body = std::mem::take(&mut rule.body).fold_uses(ctrs_map.iter().rev());
|
rule.body = std::mem::take(&mut rule.body).fold_uses(ctrs_map.iter().rev());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -297,7 +391,7 @@ impl ParseBook {
|
|||||||
let mut def_map: IndexMap<_, _> = IndexMap::new();
|
let mut def_map: IndexMap<_, _> = IndexMap::new();
|
||||||
|
|
||||||
// Rename the definitions to their source name
|
// Rename the definitions to their source name
|
||||||
// Surrounded with `__` if not imported by the main book.
|
// Starting with `__` if not imported by the main book.
|
||||||
for def in self.fun_defs.values_mut() {
|
for def in self.fun_defs.values_mut() {
|
||||||
update_name(&mut def.name, def.source.clone(), src, main_imp, &mut def_map);
|
update_name(&mut def.name, def.source.clone(), src, main_imp, &mut def_map);
|
||||||
}
|
}
|
||||||
@ -344,7 +438,7 @@ impl ParseBook {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.adts.insert(nam.clone(), adt);
|
self.adts.insert(nam, adt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,6 +454,32 @@ impl ParseBook {
|
|||||||
|
|
||||||
self.fun_defs.insert(name.clone(), def);
|
self.fun_defs.insert(name.clone(), def);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn top_level_names(&self) -> impl Iterator<Item = &Name> {
|
||||||
|
let imp_defs = self.imp_defs.keys();
|
||||||
|
let fun_defs = self.fun_defs.keys();
|
||||||
|
let adts = self.adts.keys();
|
||||||
|
let ctrs = self.ctrs.keys();
|
||||||
|
|
||||||
|
imp_defs.chain(fun_defs).chain(adts).chain(ctrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rename_ctr_patterns(rule: &mut Rule, map: &IndexMap<Name, Name>) {
|
||||||
|
for pat in &mut rule.pats {
|
||||||
|
for bind in pat.binds_mut().flatten() {
|
||||||
|
if let Some(alias) = map.get(bind) {
|
||||||
|
*bind = alias.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_bind_to_map(map: &mut IndexMap<Name, Name>, bind: &Name, src: &Name, ctr: &Name) {
|
||||||
|
let full_ctr_name = ctr.split("__").nth(1).unwrap_or(ctr.as_ref());
|
||||||
|
let ctr_name = full_ctr_name.strip_prefix(src.as_ref()).unwrap();
|
||||||
|
let bind = Name::new(format!("{}{}", bind, ctr_name));
|
||||||
|
map.insert(bind, ctr.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_name(
|
fn update_name(
|
||||||
@ -374,7 +494,7 @@ fn update_name(
|
|||||||
let mut new_name = Name::new(format!("{}/{}", src, def_name));
|
let mut new_name = Name::new(format!("{}/{}", src, def_name));
|
||||||
|
|
||||||
if !main_imp.sources.contains(&new_name) {
|
if !main_imp.sources.contains(&new_name) {
|
||||||
new_name = Name::new(format!("__{}__", new_name));
|
new_name = Name::new(format!("__{}", new_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
def_map.insert(def_name.clone(), new_name.clone());
|
def_map.insert(def_name.clone(), new_name.clone());
|
||||||
@ -391,17 +511,17 @@ fn update_name(
|
|||||||
|
|
||||||
impl Term {
|
impl Term {
|
||||||
fn fold_uses<'a>(self, map: impl Iterator<Item = (&'a Name, &'a Name)>) -> Self {
|
fn fold_uses<'a>(self, map: impl Iterator<Item = (&'a Name, &'a Name)>) -> Self {
|
||||||
map.fold(self, |acc, (bind, nam)| Term::Use {
|
map.fold(self, |acc, (bind, nam)| Self::Use {
|
||||||
nam: Some(bind.clone()),
|
nam: Some(bind.clone()),
|
||||||
val: Box::new(Term::Var { nam: nam.clone() }),
|
val: Box::new(Self::Var { nam: nam.clone() }),
|
||||||
nxt: Box::new(acc),
|
nxt: Box::new(acc),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stmt {
|
impl Stmt {
|
||||||
fn fold_uses<'a>(self, map: impl Iterator<Item = (&'a Name, &'a Name)>) -> Stmt {
|
fn fold_uses<'a>(self, map: impl Iterator<Item = (&'a Name, &'a Name)>) -> Self {
|
||||||
map.fold(self, |acc, (bind, nam)| Stmt::Use {
|
map.fold(self, |acc, (bind, nam)| Self::Use {
|
||||||
nam: bind.clone(),
|
nam: bind.clone(),
|
||||||
val: Box::new(Expr::Var { nam: nam.clone() }),
|
val: Box::new(Expr::Var { nam: nam.clone() }),
|
||||||
nxt: Box::new(acc),
|
nxt: Box::new(acc),
|
||||||
@ -409,8 +529,10 @@ impl Stmt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Sources = IndexMap<Name, String>;
|
||||||
|
|
||||||
pub trait PackageLoader {
|
pub trait PackageLoader {
|
||||||
fn load(&mut self, name: Name, import_type: &ImportType) -> Result<Vec<(Name, String)>, String>;
|
fn load(&mut self, name: Name, import_type: &ImportType, pkgs: &mut Sources) -> Result<Vec<Name>, String>;
|
||||||
fn is_loaded(&self, name: &Name) -> bool;
|
fn is_loaded(&self, name: &Name) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -420,16 +542,17 @@ pub struct DefaultLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PackageLoader for DefaultLoader {
|
impl PackageLoader for DefaultLoader {
|
||||||
fn load(&mut self, name: Name, _import_type: &ImportType) -> Result<Vec<(Name, String)>, String> {
|
fn load(&mut self, name: Name, _import_type: &ImportType, pkgs: &mut Sources) -> Result<Vec<Name>, String> {
|
||||||
if !self.is_loaded(&name) {
|
if !self.is_loaded(&name) {
|
||||||
// TODO: Should the local filesystem be searched anyway for each sub_name?
|
// TODO: Should the local filesystem be searched anyway for each sub_name?
|
||||||
// TODO: If the name to load is a folder, and we have sub names, we should load those files instead?
|
// TODO: If the name to load is a folder, and we have sub names, we should load those files instead?
|
||||||
self.loaded.insert(name.clone());
|
self.loaded.insert(name.clone());
|
||||||
let path = self.local_path.parent().unwrap().join(name.as_ref()).with_extension("bend");
|
let path = self.local_path.parent().unwrap().join(name.as_ref()).with_extension("bend");
|
||||||
std::fs::read_to_string(path).map_err(|e| e.to_string()).map(|c| vec![(name, c)])
|
let code = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
|
||||||
} else {
|
pkgs.insert(name.clone(), code);
|
||||||
Ok(Vec::new())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(vec![name])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_loaded(&self, name: &Name) -> bool {
|
fn is_loaded(&self, name: &Name) -> bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user