WIP fleshing out of block and expression name analysis.

This commit is contained in:
Jesse Brault 2025-10-31 13:04:22 -05:00
parent 12a3a61156
commit 13330300c1
9 changed files with 413 additions and 43 deletions

View File

@ -116,8 +116,7 @@ fn generate_node_file(build_specs: &[BuildSpec]) -> AstGeneratedFile {
use std::range::Range;
use std::rc::Rc;
use std::cell::RefCell;
use crate::name_analysis::symbol::type_symbol::*;
use crate::name_analysis::symbol::use_symbol::*;
use crate::name_analysis::symbol::*;
#(#types)*
};

View File

@ -14,11 +14,12 @@ use crate::name_analysis::symbol::primitive_type_symbol::PrimitiveTypeSymbol;
use crate::name_analysis::symbol::source_definition::SourceDefinition;
use crate::name_analysis::symbol::type_symbol::TypeSymbol;
use crate::name_analysis::symbol::use_symbol::{ConcreteUseSymbol, StarUseSymbol};
use crate::name_analysis::symbol_table::{SymbolInsertError, SymbolTable};
use crate::name_analysis::symbol_table::SymbolTable;
use crate::name_analysis::util::{
format_fqn, handle_insert_error, handle_lookup_error, join_fqn_parts,
};
use std::cell::RefCell;
use std::fmt::format;
use std::rc::Rc;
pub fn na_p1_compilation_unit(
@ -250,9 +251,7 @@ fn na_p1_function(
Some(SourceDefinition::from_identifier(function.identifier())),
);
let function_symbol = match symbol_table.insert_function_symbol(to_insert) {
Ok(function_symbol) => {
Some(function_symbol)
}
Ok(function_symbol) => Some(function_symbol),
Err(symbol_insert_error) => {
handle_insert_error(
symbol_insert_error,
@ -282,15 +281,21 @@ fn na_p1_function(
));
// return type
let return_type =
na_p1_return_type(function.return_type_mut(), symbol_table, diagnostics);
let return_type = na_p1_return_type(function.return_type_mut(), symbol_table, diagnostics);
if let Some(type_symbol) = return_type {
as_ref_mut.set_return_type(type_symbol);
}
symbol_table.push_scope(&format!("FunctionBodyScope {}", function.identifier().name()));
symbol_table.push_scope(&format!(
"FunctionBodyScope {}",
function.identifier().name()
));
na_p1_function_body(function.function_body_mut(), symbol_table, diagnostics);
na_p1_function_body(
function.function_body_mut(),
symbol_table,
diagnostics
);
symbol_table.pop_scope();
symbol_table.pop_scope();
@ -324,9 +329,7 @@ fn na_p1_parameter(
parameter_type_symbol,
);
match symbol_table.insert_parameter_symbol(to_insert) {
Ok(parameter_symbol) => {
Some(parameter_symbol)
}
Ok(parameter_symbol) => Some(parameter_symbol),
Err(symbol_insert_error) => {
handle_insert_error(
symbol_insert_error,
@ -473,10 +476,12 @@ fn na_p1_function_body(
) {
match function_body {
FunctionBody::FunctionAliasBody(alias_body) => {
// no-op, resolve in pass 2
// set scope id for pass 2; see below
alias_body.set_scope_id(symbol_table.current_scope_id());
}
FunctionBody::FunctionEqualsBody(equals_body) => {
// see below
equals_body.set_scope_id(symbol_table.current_scope_id());
}
FunctionBody::FunctionBlockBody(block_body) => {
// we need to do all insertion/resolution in pass 2, because we
@ -484,6 +489,7 @@ fn na_p1_function_body(
// unit which haven't been defined yet. So the strategy is to set
// the scope id for the body and then in pass 2, set the symbol
// table's current scope to that id.
block_body.set_scope_id(symbol_table.current_scope_id());
}
}
}

View File

@ -52,12 +52,16 @@ pub fn analyze_names<
na_p1_compilation_unit(file_name, compilation_unit, symbol_table, &mut diagnostics);
}
if !diagnostics.is_empty() {
return diagnostics;
}
// resolve symbols
for compilation_unit in compilation_units {
na_p2_compilation_unit(compilation_unit, symbol_table, &mut diagnostics);
}
diagnostics.into()
diagnostics
}
#[cfg(test)]

View File

@ -1,28 +1,27 @@
use crate::ast::node::{
CompilationUnit, ConcreteUseStatement, ConcreteUseStatementSuffix, Identifier,
StarUseStatement, UseStatement, UseStatementIdentifier, UseStatementPrefix,
};
use crate::ast::node::{AssignmentStatement, CompilationUnit, ConcreteUseStatement, ConcreteUseStatementSuffix, Expression, ExpressionStatement, Function, FunctionAliasBody, FunctionBlockBody, FunctionBody, Identifier, LValue, ModuleLevelDeclaration, StarUseStatement, Statement, TypeUse, UseStatement, UseStatementIdentifier, UseStatementPrefix, VariableDeclaration, VariableUse};
use crate::diagnostic::DmDiagnostic;
use crate::name_analysis::symbol::source_definition::SourceDefinition;
use crate::name_analysis::symbol::variable_symbol::VariableSymbol;
use crate::name_analysis::symbol_table::{SymbolLookupError, SymbolTable};
use crate::name_analysis::util::{handle_lookup_error, join_fqn_parts};
use crate::name_analysis::util::{handle_insert_error, handle_lookup_error, join_fqn_parts};
use std::rc::Rc;
pub fn na_p2_compilation_unit(
compilation_unit: &mut CompilationUnit,
symbol_table: &SymbolTable,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
// TODO: check namespace for proper file name
for use_statement in compilation_unit.use_statements_mut() {
na_p2_use_statement(use_statement, symbol_table, diagnostics);
}
// TODO: declarations
for declaration in compilation_unit.module_level_declarations_mut() {
na_p2_module_level_declaration(declaration, symbol_table, diagnostics);
}
}
fn na_p2_use_statement(
use_statement: &mut UseStatement,
symbol_table: &SymbolTable,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match use_statement {
@ -37,7 +36,7 @@ fn na_p2_use_statement(
fn na_p2_concrete_use_statement(
concrete_use_statement: &mut ConcreteUseStatement,
symbol_table: &SymbolTable,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
let base_fqn_parts = concrete_use_statement
@ -72,7 +71,7 @@ fn na_p2_concrete_use_statement(
fn handle_concrete_use_statement_identifier(
base_fqn_parts: &[Rc<str>],
use_statement_identifier: &mut UseStatementIdentifier,
symbol_table: &SymbolTable,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
let fqn_parts = {
@ -101,7 +100,7 @@ fn handle_concrete_use_statement_identifier(
fn na_p2_star_use_statement(
star_use_statement: &mut StarUseStatement,
symbol_table: &SymbolTable,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
let mut symbol_ref_mut = star_use_statement.symbol().unwrap().borrow_mut();
@ -120,3 +119,276 @@ fn na_p2_star_use_statement(
}
}
}
fn na_p2_module_level_declaration(
module_level_declaration: &mut ModuleLevelDeclaration,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match module_level_declaration {
ModuleLevelDeclaration::Module(module_declaration) => {
todo!()
}
ModuleLevelDeclaration::Interface(interface) => {
todo!()
}
ModuleLevelDeclaration::Class(class) => {
todo!()
}
ModuleLevelDeclaration::Function(function) => {
na_p2_function(function, symbol_table, diagnostics);
}
ModuleLevelDeclaration::PlatformFunction(platform_function) => {
todo!()
}
}
}
fn na_p2_function(
function: &mut Function,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
na_p2_function_body(function.function_body_mut(), symbol_table, diagnostics);
}
fn na_p2_function_body(
function_body: &mut FunctionBody,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match function_body {
FunctionBody::FunctionAliasBody(alias_body) => {
na_p2_function_alias_body(alias_body, symbol_table, diagnostics);
}
FunctionBody::FunctionEqualsBody(equals_body) => {}
FunctionBody::FunctionBlockBody(block_body) => {
na_p2_function_block_body(block_body, symbol_table, diagnostics);
}
}
}
fn na_p2_function_alias_body(
function_alias_body: &mut FunctionAliasBody,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
let maybe_function_symbol = symbol_table.lookup_function_symbol(
function_alias_body.identifier().name(),
*function_alias_body.scope_id().unwrap(),
);
match maybe_function_symbol {
Ok(function_symbol) => {
function_alias_body.set_resolved_function_symbol(function_symbol);
}
Err(symbol_lookup_error) => {
handle_lookup_error(
symbol_lookup_error,
function_alias_body.identifier().name(),
function_alias_body.identifier().file_id(),
function_alias_body.identifier().range(),
diagnostics,
);
}
}
}
fn na_p2_function_block_body(
function_block_body: &mut FunctionBlockBody,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
symbol_table.set_current_scope(*function_block_body.scope_id().unwrap());
for statement in function_block_body.statements_mut() {
na_p2_statement(statement, symbol_table, diagnostics);
}
}
fn na_p2_statement(
statement: &mut Statement,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match statement {
Statement::VariableDeclaration(variable_declaration) => {
na_p2_variable_declaration(variable_declaration, symbol_table, diagnostics);
}
Statement::AssignmentStatement(assignment_statement) => {
na_p2_assignment_statement(assignment_statement, symbol_table, diagnostics);
}
Statement::ExpressionStatement(expression_statement) => {
na_p2_expression_statement(expression_statement, symbol_table, diagnostics);
}
Statement::UseStatement(use_statement) => {
todo!()
}
Statement::IfStatement(if_statement) => {
todo!()
}
Statement::WhileStatement(while_statement) => {
todo!()
}
Statement::ForStatement(for_statement) => {
todo!()
}
}
}
fn na_p2_variable_declaration(
variable_declaration: &mut VariableDeclaration,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
// handle variable itself
let to_insert = VariableSymbol::new(
variable_declaration.identifier().name(),
variable_declaration.is_mut(),
Some(SourceDefinition::from_identifier(
variable_declaration.identifier(),
)),
);
match symbol_table.insert_variable_symbol(to_insert) {
Ok(variable_symbol) => {
variable_declaration.set_variable_symbol(variable_symbol);
}
Err(symbol_insert_error) => {
handle_insert_error(
symbol_insert_error,
variable_declaration.identifier().name(),
variable_declaration.identifier().file_id(),
variable_declaration.identifier().range(),
diagnostics,
);
}
}
// type-use
if let Some(type_use) = variable_declaration.type_use_mut() {
na_p2_type_use(type_use, symbol_table, diagnostics);
}
// initializer
if let Some(expression) = variable_declaration.expression_mut() {
na_p2_expression(expression, symbol_table, diagnostics);
}
}
fn na_p2_assignment_statement(
assignment_statement: &mut AssignmentStatement,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
na_p2_l_value(assignment_statement.l_value_mut(), symbol_table, diagnostics);
na_p2_expression(assignment_statement.expression_mut(), symbol_table, diagnostics);
}
fn na_p2_l_value(
l_value: &mut LValue,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
na_p2_variable_use(l_value.variable_use_mut(), symbol_table, diagnostics);
// TODO: suffixes
}
fn na_p2_expression_statement(
expression_statement: &mut ExpressionStatement,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
na_p2_expression(expression_statement.expression_mut(), symbol_table, diagnostics);
}
fn na_p2_variable_use(
variable_use: &mut VariableUse,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match symbol_table.lookup_lv_symbol(variable_use.identifier().name()) {
Ok(lv_symbol) => {
variable_use.set_lv_symbol(lv_symbol);
}
Err(symbol_lookup_error) => {
handle_lookup_error(
symbol_lookup_error,
variable_use.identifier().name(),
variable_use.identifier().file_id(),
variable_use.identifier().range(),
diagnostics,
);
}
}
}
fn na_p2_expression(
expression: &mut Expression,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match expression {
Expression::Ternary(ternary) => {
todo!()
}
Expression::Or(or) => {
todo!()
}
Expression::And(and) => {
todo!()
}
Expression::Comparison(comparison) => {
todo!()
}
Expression::Shift(shift) => {
todo!()
}
Expression::Additive(additive) => {
todo!()
}
Expression::Multiplicative(multiplicative) => {
todo!()
}
Expression::Prefix(prefix) => {
todo!()
}
Expression::Suffix(suffix) => {
todo!()
}
Expression::Literal(literal) => {
todo!()
}
Expression::VariableUse(variable_use) => {
na_p2_variable_use(variable_use, symbol_table, diagnostics);
}
Expression::Fqn(fqn) => {
todo!()
}
Expression::Closure(closure) => {
todo!()
}
Expression::List(list) => {
todo!()
}
}
}
fn na_p2_type_use(
type_use: &mut TypeUse,
symbol_table: &mut SymbolTable,
diagnostics: &mut Vec<DmDiagnostic>,
) {
match type_use {
TypeUse::PrimitiveType(primitive_type_use) => {
todo!()
}
TypeUse::InterfaceOrClassTypeUse(interface_or_class_type_use) => {
todo!()
}
TypeUse::TupleTypeUse(tuple_type_use) => {
todo!()
}
TypeUse::FunctionTypeUse(function_type_use) => {
todo!()
}
}
}

View File

@ -17,6 +17,13 @@ pub mod variable_symbol;
use crate::name_analysis::symbol::source_definition::SourceDefinition;
use std::fmt::Debug;
pub use self::{
class_member_symbol::*, class_symbol::*, function_symbol::*, function_symbol::*,
generic_type_symbol::*, interface_symbol::*, lv_symbol::*, module_level_symbol::*,
module_symbol::*, parameter_symbol::*, primitive_type_symbol::*, type_symbol::*,
usable_symbol::*, use_symbol::*, variable_symbol::*,
};
pub trait Symbol: Debug {
fn source_definition(&self) -> Option<&SourceDefinition>;
}

View File

@ -1,10 +1,11 @@
use crate::name_analysis::symbol::source_definition::SourceDefinition;
use crate::name_analysis::symbol::Symbol;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
#[derive(Clone)]
pub struct VariableSymbol {
declared_name: String,
declared_name: Rc<str>,
is_mutable: bool,
source_definition: Option<SourceDefinition>,
}
@ -16,7 +17,7 @@ impl VariableSymbol {
source_definition: Option<SourceDefinition>,
) -> Self {
VariableSymbol {
declared_name: declared_name.to_string(),
declared_name: Rc::from(declared_name),
is_mutable,
source_definition,
}
@ -26,6 +27,10 @@ impl VariableSymbol {
&self.declared_name
}
pub fn declared_name_owned(&self) -> Rc<str> {
self.declared_name.clone()
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}

View File

@ -5,7 +5,7 @@ use crate::name_analysis::symbol::parameter_symbol::ParameterSymbol;
use crate::name_analysis::symbol::type_symbol::TypeSymbol;
use crate::name_analysis::symbol::usable_symbol::UsableSymbol;
use crate::name_analysis::symbol::use_symbol::{ConcreteUseSymbol, StarUseSymbol};
use crate::name_analysis::symbol::Symbol;
use crate::name_analysis::symbol::{LVSymbol, Symbol};
use crate::name_analysis::symbol_table::fqn_context::FqnContext;
use crate::name_analysis::symbol_table::symbol_tree::SymbolTree;
use crate::name_analysis::symbol_table::SymbolInsertError::SymbolAlreadyDefined;
@ -14,6 +14,7 @@ use std::cell::RefCell;
use std::fmt::Display;
use std::ops::Deref;
use std::rc::Rc;
use crate::name_analysis::symbol::variable_symbol::VariableSymbol;
pub(self) mod fqn_context;
mod scope;
@ -68,6 +69,10 @@ impl SymbolTable {
}
}
pub fn set_current_scope(&mut self, id: usize) {
self.current_scope_id = id;
}
pub fn set_current_fqn(&mut self, names: &[&str]) {
self.fqn_context = Box::new(FqnContext::new());
for name in names {
@ -227,6 +232,23 @@ impl SymbolTable {
}
}
pub fn insert_variable_symbol(
&mut self,
variable_symbol: VariableSymbol,
) -> Result<Rc<RefCell<VariableSymbol>>, SymbolInsertError> {
if let Some(defined_symbol) = self
.current_scope()
.find_lv_symbol(variable_symbol.declared_name())
{
Err(SymbolAlreadyDefined(defined_symbol.to_symbol()))
} else {
let inserted = self
.current_scope_mut()
.insert_variable_symbol(variable_symbol);
Ok(inserted)
}
}
pub fn lookup_type(&self, declared_name: &str) -> Result<TypeSymbol, SymbolLookupError> {
let mut current_scope: Option<&Scope> = Some(self.current_scope());
while let Some(scope) = current_scope.take() {
@ -244,6 +266,34 @@ impl SymbolTable {
pub fn lookup_type_by_fqn(&self, fqn_parts: &[&str]) -> Result<TypeSymbol, SymbolLookupError> {
todo!()
}
pub fn lookup_function_symbol(&self, declared_name: &str, scope_id: usize) -> Result<Rc<RefCell<FunctionSymbol>>, SymbolLookupError> {
let mut current_scope: Option<&Scope> = self.scopes.get(scope_id);
while let Some(scope) = current_scope.take() {
if let Some(function_symbol) = scope.find_function_symbol(declared_name) {
return Ok(function_symbol.clone());
} else {
current_scope = scope
.parent()
.and_then(|parent_id| self.scopes.get(parent_id));
}
}
Err(SymbolLookupError::NoDefinition)
}
pub fn lookup_lv_symbol(&self, declared_name: &str) -> Result<LVSymbol, SymbolLookupError> {
let mut current_scope: Option<&Scope> = Some(self.current_scope());
while let Some(scope) = current_scope.take() {
if let Some(lv_symbol) = scope.find_lv_symbol(declared_name) {
return Ok(lv_symbol)
} else {
current_scope = scope
.parent()
.and_then(|parent_id| self.scopes.get(parent_id));
}
}
Err(SymbolLookupError::NoDefinition)
}
}
impl Display for SymbolTable {

View File

@ -143,6 +143,14 @@ impl Scope {
insert_symbol!(self.parameter_symbols, symbol, key)
}
pub fn insert_variable_symbol(
&mut self,
symbol: VariableSymbol,
) -> Rc<RefCell<VariableSymbol>> {
let key = symbol.declared_name_owned();
insert_symbol!(self.variable_symbols, symbol, key)
}
pub fn find_module_level_symbol(&self, declared_name: &str) -> Option<ModuleLevelSymbol> {
self.module_symbols
.get(declared_name)
@ -196,6 +204,10 @@ impl Scope {
})
}
pub fn find_function_symbol(&self, declared_name: &str) -> Option<&Rc<RefCell<FunctionSymbol>>> {
self.function_symbols.get(declared_name)
}
pub fn debug_name(&self) -> &str {
&self.debug_name
}

View File

@ -609,6 +609,9 @@ FunctionEqualsBody:
struct:
children:
- expression
fields:
- scope_id:
kind: usize
FunctionAliasBody:
struct:
children:
@ -616,6 +619,12 @@ FunctionAliasBody:
skip:
rule: Alias
- identifier
fields:
- scope_id:
kind: usize
- resolved_function_symbol:
kind: FunctionSymbol
wrap: rc_ref_cell
FunctionBlockBody:
struct:
children:
@ -625,6 +634,9 @@ FunctionBlockBody:
- end_kw:
skip:
rule: End
fields:
- scope_id:
kind: usize
# Class constructs
ClassConstructor:
@ -681,6 +693,10 @@ VariableDeclaration:
- expression:
member:
optional: true
fields:
- variable_symbol:
kind: VariableSymbol
wrap: rc_ref_cell
AssignmentStatement:
struct:
children:
@ -786,10 +802,9 @@ VariableUse:
struct:
children:
- identifier
derive:
- PartialEq
- Eq
- Hash
fields:
- lv_symbol:
kind: LVSymbol
# Expressions
Expression: