Various gathering and scope table.

This commit is contained in:
Jesse Brault 2025-10-13 10:51:22 -05:00
parent b47dea9136
commit 6b206605c1
5 changed files with 848 additions and 176 deletions

File diff suppressed because it is too large Load Diff

View File

@ -19,19 +19,23 @@ The resolve phase has one main responsibility: resolve all references based on t
`scope_id` property. `scope_id` property.
*/ */
use crate::ast::node::{CompilationUnit, VariableUse}; // use crate::name_analysis::resolve::resolve_compilation_unit;
use crate::ast::ast_node::AstNode;
use crate::ast::node::CompilationUnit;
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::symbol_table::SymbolTable; use crate::name_analysis::symbol_table::SymbolTable;
use codespan_reporting::files::Files; use codespan_reporting::files::Files;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::Hash;
use crate::name_analysis::scope_table::ScopeTable;
pub(self) mod fqn_context; pub(self) 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;
mod scope_table;
pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>( pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
compilation_units: &[Box<CompilationUnit>], compilation_units: &[Box<CompilationUnit>],
@ -39,7 +43,7 @@ pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
) -> Vec<DmDiagnostic> { ) -> Vec<DmDiagnostic> {
let mut diagnostics = vec![]; let mut diagnostics = vec![];
let mut scope_ids: HashMap<&VariableUse, usize> = HashMap::new(); let mut scope_table = ScopeTable::new();
// gather symbols // gather symbols
for compilation_unit in compilation_units { for compilation_unit in compilation_units {
@ -48,7 +52,7 @@ pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
compilation_unit, compilation_unit,
&file_name, &file_name,
symbol_table, symbol_table,
&mut scope_ids, &mut scope_table,
&mut diagnostics, &mut diagnostics,
); );
} }

View File

@ -0,0 +1,32 @@
use crate::ast::node::{FullyQualifiedName, VariableUse};
use std::collections::HashMap;
pub struct ScopeTable<'a> {
variable_use_scopes: HashMap<&'a VariableUse, usize>,
fqn_scopes: HashMap<&'a FullyQualifiedName, usize>,
}
impl<'a> ScopeTable<'a> {
pub fn new() -> Self {
Self {
variable_use_scopes: HashMap::new(),
fqn_scopes: HashMap::new(),
}
}
pub fn insert_variable_use_scope(&mut self, variable_use: &'a VariableUse, scope_id: usize) {
self.variable_use_scopes.insert(variable_use, scope_id);
}
pub fn insert_fqn_scope(&mut self, fqn: &'a FullyQualifiedName, scope_id: usize) {
self.fqn_scopes.insert(fqn, scope_id);
}
pub fn variable_use_scope(&self, variable_use: &'a VariableUse) -> Option<usize> {
self.variable_use_scopes.get(&variable_use).copied()
}
pub fn fqn_scope(&self, fqn: &'a FullyQualifiedName) -> Option<usize> {
self.fqn_scopes.get(&fqn).copied()
}
}

View File

@ -50,7 +50,6 @@ Identifier:
- range: - range:
kind: range kind: range
derive: derive:
- Clone
- PartialEq - PartialEq
- Eq - Eq
- Hash - Hash
@ -60,6 +59,10 @@ FullyQualifiedName:
- identifiers: - identifiers:
vec: vec:
rule: Identifier rule: Identifier
derive:
- PartialEq
- Eq
- Hash
# Lists # Lists
TypeUseList: TypeUseList:
@ -74,7 +77,7 @@ IdentifierList:
- identifiers: - identifiers:
vec: vec:
rule: Identifier rule: Identifier
ParenthesesOptionalTypeUseList: ParenthesesTypeUseList:
struct: struct:
children: children:
- type_use_list: - type_use_list:
@ -90,19 +93,29 @@ TypeUse:
- TupleTypeUse - TupleTypeUse
- FunctionTypeUse - FunctionTypeUse
PrimitiveType: PrimitiveType:
leaf_enum: tree_enum:
rules: rules:
- Byte - Byte:
- Short child: false
- Char - Short:
- Int child: false
- Long - Char:
- Double child: false
- Bool - Int:
- String child: false
- Long:
child: false
- Double:
child: false
- Bool:
child: false
- String:
child: false
- TypedArray - TypedArray
- Any - Any:
- Void child: false
- Void:
child: false
TypedArray: TypedArray:
struct: struct:
children: children:
@ -160,7 +173,7 @@ GenericParameters:
TupleArguments: TupleArguments:
struct: struct:
children: children:
- parentheses_optional_type_use_list - parentheses_type_use_list
# Implements List # Implements List
ImplementsList: ImplementsList:
@ -656,7 +669,7 @@ IfClause:
skip: skip:
rule: Then rule: Then
- statements: - statements:
skip: vec:
rule: Statement rule: Statement
IfElseIf: IfElseIf:
struct: struct:
@ -704,7 +717,7 @@ ForStatement:
- do_kw: - do_kw:
skip: skip:
rule: Do rule: Do
- statement: - statements:
vec: vec:
rule: Statement rule: Statement
- end_kw: - end_kw:
@ -731,7 +744,6 @@ VariableUse:
children: children:
- identifier - identifier
derive: derive:
- Clone
- PartialEq - PartialEq
- Eq - Eq
- Hash - Hash

View File

@ -199,7 +199,7 @@ IdentifierList = {
~ ( "," ~ Identifier )* ~ ( "," ~ Identifier )*
} }
ParenthesesOptionalTypeUseList = { ParenthesesTypeUseList = {
"(" "("
~ TypeUseList? ~ TypeUseList?
~ ")" ~ ")"
@ -272,7 +272,7 @@ GenericParameters = {
// Tuple Arguments // Tuple Arguments
TupleArguments = { TupleArguments = {
ParenthesesOptionalTypeUseList ParenthesesTypeUseList
} }
// Implements list // Implements list