cl-typeck: Remove NameCollectable trait, use NameCollector instead :D

This commit is contained in:
John 2024-04-27 16:05:40 -05:00
parent b9085551e1
commit 9566f098ac
2 changed files with 7 additions and 24 deletions

View File

@ -1,11 +1,11 @@
use cl_ast::{ use cl_ast::{
ast_visitor::fold::Fold, ast_visitor::{Fold, Visit},
desugar::{squash_groups::SquashGroups, while_else::WhileElseDesugar}, desugar::{squash_groups::SquashGroups, while_else::WhileElseDesugar},
}; };
use cl_lexer::Lexer; use cl_lexer::Lexer;
use cl_parser::{inliner::ModuleInliner, Parser}; use cl_parser::{inliner::ModuleInliner, Parser};
use cl_typeck::{ use cl_typeck::{
definition::Def, name_collector::NameCollectable, project::Project, type_resolver::resolve, definition::Def, name_collector::NameCollector, project::Project, type_resolver::resolve,
}; };
use repline::{error::Error as RlError, prebaked::*}; use repline::{error::Error as RlError, prebaked::*};
use std::{error::Error, path}; use std::{error::Error, path};
@ -38,8 +38,7 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
}; };
let code = inline_modules(code, concat!(env!("PWD"), "/stdlib")); let code = inline_modules(code, concat!(env!("PWD"), "/stdlib"));
NameCollector::new(&mut prj).visit_file(unsafe { TREES.push(code) });
unsafe { TREES.push(code) }.collect_in_root(&mut prj)?;
main_menu(&mut prj)?; main_menu(&mut prj)?;
Ok(()) Ok(())
@ -84,7 +83,7 @@ fn enter_code(prj: &mut Project) -> Result<(), RlError> {
let code = inline_modules(code, ""); let code = inline_modules(code, "");
let code = WhileElseDesugar.fold_file(code); let code = WhileElseDesugar.fold_file(code);
// Safety: this is totally unsafe // Safety: this is totally unsafe
unsafe { TREES.push(code) }.collect_in_root(prj)?; NameCollector::new(prj).visit_file(unsafe { TREES.push(code) });
Ok(Response::Accept) Ok(Response::Accept)
}) })

View File

@ -8,24 +8,6 @@ use crate::{
use cl_ast::{ast_visitor::Visit, *}; use cl_ast::{ast_visitor::Visit, *};
use std::mem; use std::mem;
pub trait NameCollectable<'a> {
/// Collects the identifiers within this node,
/// returning a new [DefID] if any were allocated,
/// else returning the parent [DefID]
fn collect(&'a self, c: &mut Prj<'a>, parent: DefID) -> Result<DefID, &'static str>;
fn collect_in_root(&'a self, c: &mut Prj<'a>) -> Result<DefID, &'static str> {
self.collect(c, c.root)
}
}
impl<'a> NameCollectable<'a> for File {
fn collect(&'a self, prj: &mut Prj<'a>, parent: DefID) -> Result<DefID, &'static str> {
NameCollector::with_root(prj, parent).visit_file(self);
Ok(parent)
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct NameCollector<'prj, 'a> { pub struct NameCollector<'prj, 'a> {
prj: &'prj mut Prj<'a>, prj: &'prj mut Prj<'a>,
@ -34,9 +16,11 @@ pub struct NameCollector<'prj, 'a> {
} }
impl<'prj, 'a> NameCollector<'prj, 'a> { impl<'prj, 'a> NameCollector<'prj, 'a> {
/// Constructs a new [NameCollector] out of a [Project](Prj)
pub fn new(prj: &'prj mut Prj<'a>) -> Self { pub fn new(prj: &'prj mut Prj<'a>) -> Self {
Self { parent: prj.root, prj, retval: None } Self { parent: prj.root, prj, retval: None }
} }
/// Constructs a new [NameCollector] out of a [Project](Prj) and a parent [DefID]
pub fn with_root(prj: &'prj mut Prj<'a>, parent: DefID) -> Self { pub fn with_root(prj: &'prj mut Prj<'a>, parent: DefID) -> Self {
Self { prj, parent, retval: None } Self { prj, parent, retval: None }
} }
@ -56,7 +40,7 @@ impl<'prj, 'a> NameCollector<'prj, 'a> {
} }
} }
impl<'prj, 'a> ast_visitor::Visit<'a> for NameCollector<'prj, 'a> { impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> {
fn visit_item(&mut self, i: &'a Item) { fn visit_item(&mut self, i: &'a Item) {
let Item { extents: _, attrs, vis, kind } = i; let Item { extents: _, attrs, vis, kind } = i;
if let Some(def) = self.returns(kind, Self::visit_item_kind) { if let Some(def) = self.returns(kind, Self::visit_item_kind) {