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