WIP redoing name analysis.

This commit is contained in:
Jesse Brault 2025-09-29 09:39:13 -05:00
parent d6faa37515
commit c32ae72beb
5 changed files with 1489 additions and 1346 deletions

View File

@ -41,12 +41,33 @@ pub fn make_ast_node_impl(build_spec: &BuildSpec) -> Option<TokenStream> {
}
}
pub fn make_ast_enum_member(build_spec: &BuildSpec) -> Option<TokenStream> {
pub fn make_ast_enum_member(build_spec: &BuildSpec) -> Option<TokenStream> {
match build_spec {
BuildSpec::Struct(struct_spec) => {
let type_ident = format_ident!("{}", struct_spec.build());
Some(quote! { #type_ident(#type_ident) })
Some(format_ident!("{}", struct_spec.build()))
}
_ => None,
BuildSpec::LeafStruct(leaf_struct) => {
Some(format_ident!("{}", leaf_struct.build()))
}
BuildSpec::Enum(enum_spec) => {
Some(format_ident!("{}", enum_spec.build()))
}
BuildSpec::LeafEnum(leaf_enum) => {
Some(format_ident!("{}", leaf_enum.build()))
}
BuildSpec::PolymorphicType(polymorphic_type) => {
Some(format_ident!("{}", polymorphic_type.name()))
},
BuildSpec::PolymorphicEnumLoop(polymorphic_enum_loop) => {
Some(format_ident!("{}", polymorphic_enum_loop.name()))
}
BuildSpec::PolymorphicPassThrough(_) => None,
BuildSpec::Production(_) => None,
BuildSpec::NodeProduction(_) => None,
}
.map(|type_ident| {
quote! {
#type_ident(#type_ident)
}
})
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -19,15 +19,16 @@ The resolve phase has one main responsibility: resolve all references based on t
`scope_id` property.
*/
use crate::ast::node::CompilationUnit;
use crate::ast::node::{CompilationUnit, Identifier};
use crate::diagnostic::DmDiagnostic;
use crate::name_analysis::gather::gather_compilation_unit;
use crate::name_analysis::resolve::resolve_compilation_unit;
// use crate::name_analysis::resolve::resolve_compilation_unit;
use crate::name_analysis::symbol_table::SymbolTable;
use std::collections::HashMap;
mod fqn_context;
mod gather;
mod resolve;
// mod resolve;
pub mod symbol;
pub mod symbol_table;
@ -36,15 +37,21 @@ pub fn analyze_names(
symbol_table: &mut SymbolTable,
) -> Vec<DmDiagnostic> {
let mut diagnostics = vec![];
let mut identifier_scope_ids: HashMap<&Identifier, usize> = HashMap::new();
// gather symbols
for compilation_unit in compilation_units.iter_mut() {
gather_compilation_unit(compilation_unit, symbol_table, &mut diagnostics);
gather_compilation_unit(
compilation_unit,
symbol_table,
&mut identifier_scope_ids,
&mut diagnostics,
);
}
// resolve symbols
for compilation_unit in compilation_units.iter_mut() {
resolve_compilation_unit(compilation_unit, symbol_table, &mut diagnostics);
// resolve_compilation_unit(compilation_unit, symbol_table, &mut diagnostics);
}
diagnostics.into()
@ -64,7 +71,7 @@ pub fn analyze_names(
// use indoc::indoc;
// use pest::Parser;
// use std::collections::HashMap;
//
//
// fn assert_number_of_diagnostics(
// sources: HashMap<&str, &str>,
// symbol_table: &mut SymbolTable,
@ -72,7 +79,7 @@ pub fn analyze_names(
// ) -> Vec<CompilationUnit> {
// let mut files = SimpleFiles::new();
// let mut compilation_units = vec![];
//
//
// for (file_name, source) in sources {
// let file_id = files.add(file_name, source);
// let parse_result = DeimosParser::parse(Rule::CompilationUnit, source);
@ -83,35 +90,35 @@ pub fn analyze_names(
// if pairs.as_str().trim() != source.trim() {
// panic!("Parsing did not consume entire input.");
// }
//
//
// compilation_units.push(build_ast(file_name, file_id, pairs.next().unwrap()));
// }
//
//
// let diagnostics = analyze_names(&mut compilation_units, symbol_table);
//
//
// if diagnostics.len() != n_diagnostics {
// let writer = StandardStream::stderr(ColorChoice::Always);
// let config = term::Config::default();
//
//
// for diagnostic in &diagnostics {
// term::emit(&mut writer.lock(), &config, &files, &diagnostic).unwrap();
// }
//
//
// eprintln!("{}", symbol_table);
// }
//
//
// assert_eq!(n_diagnostics, diagnostics.len());
//
//
// compilation_units
// }
//
//
// fn assert_no_diagnostics(
// sources: HashMap<&str, &str>,
// symbol_table: &mut SymbolTable,
// ) -> Vec<CompilationUnit> {
// assert_number_of_diagnostics(sources, symbol_table, 0)
// }
//
//
// fn assert_saved_symbols(compilation_unit: &CompilationUnit) {
// walk_depth_first(compilation_unit, &mut |node_ref| match node_ref {
// NodeRef::Identifier(identifier) => {
@ -130,7 +137,7 @@ pub fn analyze_names(
// _ => {}
// })
// }
//
//
// fn assert_resolved_symbols(compilation_unit: &CompilationUnit) {
// walk_depth_first(compilation_unit, &mut |node_ref| match node_ref {
// NodeRef::UseStatement(use_statement) => match use_statement {
@ -139,7 +146,7 @@ pub fn analyze_names(
// _ => {}
// })
// }
//
//
// #[test]
// fn params_seen() {
// let sources: HashMap<&str, &str> = HashMap::from([(
@ -149,13 +156,13 @@ pub fn analyze_names(
// let x = args;
// }"},
// )]);
//
//
// let cus = assert_no_diagnostics(sources, &mut SymbolTable::new());
// for ref cu in cus {
// assert_saved_symbols(cu);
// }
// }
//
//
// #[test]
// fn two_files() {
// let sources: HashMap<&str, &str> = HashMap::from([
@ -169,19 +176,19 @@ pub fn analyze_names(
// "deps.dm",
// indoc! {"
// ns test;
//
//
// pub class Greeter {}
// "},
// ),
// ]);
//
//
// let cus = assert_no_diagnostics(sources, &mut SymbolTable::new());
// for ref cu in cus {
// assert_saved_symbols(cu);
// assert_resolved_symbols(cu);
// }
// }
//
//
// #[test]
// fn sees_std_core_println() {
// let sources: HashMap<&str, &str> = HashMap::from([(
@ -192,7 +199,7 @@ pub fn analyze_names(
// }
// "},
// )]);
//
//
// let mut symbol_table = SymbolTable::new();
// add_std_core_symbols(&mut symbol_table).expect("Failed to add std::core symbols.");
// let cus = assert_no_diagnostics(sources, &mut symbol_table);
@ -201,7 +208,7 @@ pub fn analyze_names(
// assert_resolved_symbols(cu);
// }
// }
//
//
// #[test]
// fn sees_duplicate_fn() {
// let sources: HashMap<&str, &str> = HashMap::from([(
@ -213,7 +220,7 @@ pub fn analyze_names(
// )]);
// assert_number_of_diagnostics(sources, &mut SymbolTable::new(), 1);
// }
//
//
// #[test]
// fn use_class_from_other_file() {
// let sources: HashMap<&str, &str> = HashMap::from([
@ -221,7 +228,7 @@ pub fn analyze_names(
// "main.dm",
// indoc! {"
// use greeter::Greeter;
//
//
// fn test(greeter: Greeter) {}
// "},
// ),
@ -229,7 +236,7 @@ pub fn analyze_names(
// "greeter.dm",
// indoc! {"
// ns greeter;
//
//
// class Greeter {}
// "},
// ),
@ -241,7 +248,7 @@ pub fn analyze_names(
// assert_resolved_symbols(cu);
// }
// }
//
//
// #[test]
// fn shadow_import() {
// let sources: HashMap<&str, &str> = HashMap::from([
@ -249,7 +256,7 @@ pub fn analyze_names(
// "main.dm",
// indoc! {"
// use greeter::Greeter;
//
//
// class Greeter {}
// "},
// ),
@ -257,7 +264,7 @@ pub fn analyze_names(
// "greeter.dm",
// indoc! {"
// ns greeter;
//
//
// class Greeter {}
// "},
// ),