Work on better AnalysisContext abstractions.

This commit is contained in:
Jesse Brault 2026-07-12 19:17:51 -05:00
parent 57abc03145
commit c54db2d70d
6 changed files with 499 additions and 537 deletions

View File

@ -1,7 +1,7 @@
mod repl; mod repl;
mod run; mod run;
use crate::repl::{repl, repl_2}; use crate::repl::repl_2;
use crate::run::compile_and_run_script; use crate::run::compile_and_run_script;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::io; use std::io;

View File

@ -2,314 +2,310 @@ use dmc_lib::ast::expression_statement::ExpressionStatement;
use dmc_lib::ast::statement::Statement; use dmc_lib::ast::statement::Statement;
use dmc_lib::compile_statement_to_synthetic_function; use dmc_lib::compile_statement_to_synthetic_function;
use dmc_lib::constants_table::ConstantsTable; use dmc_lib::constants_table::ConstantsTable;
use dmc_lib::diagnostic::{Diagnostic, Diagnostics}; use dmc_lib::diagnostic::Diagnostics;
use dmc_lib::ir::ir_variable::IrVariable;
use dmc_lib::ir::variable_locations::VariableLocations; use dmc_lib::ir::variable_locations::VariableLocations;
use dmc_lib::lexer::Lexer; use dmc_lib::lexer::Lexer;
use dmc_lib::offset_counter::OffsetCounter;
use dmc_lib::parser::{parse_expression, parse_let_statement}; use dmc_lib::parser::{parse_expression, parse_let_statement};
use dmc_lib::semantic_analysis::AnalysisContext; use dmc_lib::semantic_analysis::AnalysisContext;
use dmc_lib::semantic_analysis::scope::{Scope, ScopeId}; use dmc_lib::semantic_analysis::scope::{Scope, ScopeId};
use dmc_lib::symbol::variable_symbol::VariableSymbol;
use dmc_lib::symbol_table::SymbolTable;
use dmc_lib::token::TokenKind; use dmc_lib::token::TokenKind;
use dmc_lib::types_table::TypesTable;
use dvm_lib::vm::constant::{Constant, StringConstant}; use dvm_lib::vm::constant::{Constant, StringConstant};
use dvm_lib::vm::function::Function; use dvm_lib::vm::function::Function;
use dvm_lib::vm::operand::Operand; use dvm_lib::vm::operand::Operand;
use dvm_lib::vm::{CallStack, DvmContext, loop_instructions, prepare_for_instruction_loop}; use dvm_lib::vm::{CallStack, DvmContext, loop_instructions, prepare_for_instruction_loop};
use std::cell::RefCell;
use std::collections::HashMap;
use std::io; use std::io;
use std::io::{BufRead, Write}; use std::io::{BufRead, Write};
use std::rc::Rc; use std::rc::Rc;
//
pub fn repl(read: &mut impl BufRead, register_count: usize) { // pub fn repl(read: &mut impl BufRead, register_count: usize) {
let mut buffer = String::new(); // let mut buffer = String::new();
//
let mut symbol_table = SymbolTable::new(); // let mut symbol_table = SymbolTable::new();
let mut types_table = TypesTable::new(); // let mut types_table = TypesTable::new();
//
let mut repl_fn_body_scope_id: Option<usize> = None; // let mut repl_fn_body_scope_id: Option<usize> = None;
let mut repl_fn_offset_counter = OffsetCounter::new(); // let mut repl_fn_offset_counter = OffsetCounter::new();
let mut repl_fn_local_variables = HashMap::new(); // let mut repl_fn_local_variables = HashMap::new();
//
let mut constants_table = ConstantsTable::new(); // let mut constants_table = ConstantsTable::new();
let mut context = DvmContext::new(); // let mut context = DvmContext::new();
//
let mut repl_fn_stack_locals: Vec<Operand> = vec![]; // let mut repl_fn_stack_locals: Vec<Operand> = vec![];
//
'repl: loop { // 'repl: loop {
print!("> "); // print!("> ");
io::stdout().flush().unwrap(); // io::stdout().flush().unwrap();
read.read_line(&mut buffer).unwrap(); // read.read_line(&mut buffer).unwrap();
let input = buffer.trim(); // let input = buffer.trim();
if input.is_empty() { // if input.is_empty() {
buffer.clear(); // buffer.clear();
continue; // continue;
} // }
//
let mut lexer = Lexer::new(input); // let mut lexer = Lexer::new(input);
let first_token = match lexer.next() { // let first_token = match lexer.next() {
None => { // None => {
continue; // continue;
} // }
Some(result) => match result { // Some(result) => match result {
Ok(first_token) => first_token, // Ok(first_token) => first_token,
Err(lexer_error) => { // Err(lexer_error) => {
eprintln!("{:?}", lexer_error); // eprintln!("{:?}", lexer_error);
buffer.clear(); // buffer.clear();
continue; // continue;
} // }
}, // },
}; // };
//
match first_token.kind() { // match first_token.kind() {
TokenKind::Fn => { // TokenKind::Fn => {
todo!("Parse functions in repl") // todo!("Parse functions in repl")
} // }
TokenKind::Let => { // TokenKind::Let => {
match compile_let_statement( // match compile_let_statement(
input, // input,
register_count, // register_count,
&mut symbol_table, // &mut symbol_table,
&mut repl_fn_body_scope_id, // &mut repl_fn_body_scope_id,
&mut repl_fn_offset_counter, // &mut repl_fn_offset_counter,
&mut repl_fn_local_variables, // &mut repl_fn_local_variables,
&mut types_table, // &mut types_table,
&mut constants_table, // &mut constants_table,
) { // ) {
Ok(function) => { // Ok(function) => {
context // context
.functions_mut() // .functions_mut()
.insert(function.name_owned(), function); // .insert(function.name_owned(), function);
} // }
Err(diagnostics) => { // Err(diagnostics) => {
for diagnostic in diagnostics { // for diagnostic in diagnostics {
eprintln!("{}", diagnostic.message()); // eprintln!("{}", diagnostic.message());
} // }
buffer.clear(); // buffer.clear();
continue 'repl; // continue 'repl;
} // }
} // }
} // }
_ => match compile_expression( // _ => match compile_expression(
input, // input,
register_count, // register_count,
&mut symbol_table, // &mut symbol_table,
&mut types_table, // &mut types_table,
&mut repl_fn_body_scope_id, // &mut repl_fn_body_scope_id,
&mut repl_fn_local_variables, // &mut repl_fn_local_variables,
&mut repl_fn_offset_counter, // &mut repl_fn_offset_counter,
&mut constants_table, // &mut constants_table,
) { // ) {
Ok(function) => { // Ok(function) => {
context // context
.functions_mut() // .functions_mut()
.insert(function.name_owned(), function); // .insert(function.name_owned(), function);
} // }
Err(diagnostics) => { // Err(diagnostics) => {
for diagnostic in &diagnostics { // for diagnostic in &diagnostics {
eprintln!("{}", diagnostic.message()); // eprintln!("{}", diagnostic.message());
} // }
buffer.clear(); // buffer.clear();
continue 'repl; // continue 'repl;
} // }
}, // },
} // }
//
for (name, content) in constants_table.string_constants() { // for (name, content) in constants_table.string_constants() {
context.constants_mut().insert( // context.constants_mut().insert(
name.clone(), // name.clone(),
Constant::String(StringConstant::new(name, content)), // Constant::String(StringConstant::new(name, content)),
); // );
} // }
//
let mut call_stack = CallStack::new(); // let mut call_stack = CallStack::new();
prepare_for_instruction_loop(&context, "__repl", &mut call_stack, &[]); // prepare_for_instruction_loop(&context, "__repl", &mut call_stack, &[]);
//
// copy all old locals to current call frame's stack // // copy all old locals to current call frame's stack
// this has to be done with indexing because the preparation above creates space for them // // this has to be done with indexing because the preparation above creates space for them
for (i, operand) in repl_fn_stack_locals.iter().enumerate() { // for (i, operand) in repl_fn_stack_locals.iter().enumerate() {
let target_index = call_stack.top().fp() + i; // let target_index = call_stack.top().fp() + i;
call_stack.top_mut().stack_mut()[target_index] = operand.clone(); // call_stack.top_mut().stack_mut()[target_index] = operand.clone();
} // }
//
let result = loop_instructions( // let result = loop_instructions(
&context, // &context,
&mut vec![Operand::Null; register_count], // &mut vec![Operand::Null; register_count],
&mut call_stack, // &mut call_stack,
); // );
//
// copy the top frame's stack locals back to OUR stack locals for next iteration // // copy the top frame's stack locals back to OUR stack locals for next iteration
repl_fn_stack_locals = std::mem::take(call_stack.top_mut().stack_mut()); // repl_fn_stack_locals = std::mem::take(call_stack.top_mut().stack_mut());
//
if let Some(value) = result { // if let Some(value) = result {
println!("{}", value); // println!("{}", value);
} // }
//
buffer.clear(); // buffer.clear();
} // }
} // }
//
fn prepare_scopes(symbol_table: &mut SymbolTable, fn_body_scope_id: &mut Option<usize>) -> usize { // fn prepare_scopes(symbol_table: &mut SymbolTable, fn_body_scope_id: &mut Option<usize>) -> usize {
if let Some(scope_id) = fn_body_scope_id { // if let Some(scope_id) = fn_body_scope_id {
symbol_table.change_scope(*scope_id); // symbol_table.change_scope(*scope_id);
*scope_id // *scope_id
} else { // } else {
symbol_table.push_module_scope("__repl_module"); // symbol_table.push_module_scope("__repl_module");
symbol_table.push_function_scope("__repl_fn"); // symbol_table.push_function_scope("__repl_fn");
let container_scope_id = symbol_table.push_block_scope("__repl_fn_body"); // let container_scope_id = symbol_table.push_block_scope("__repl_fn_body");
fn_body_scope_id.replace(container_scope_id); // fn_body_scope_id.replace(container_scope_id);
container_scope_id // container_scope_id
} // }
} // }
//
fn compile_expression( // fn compile_expression(
input: &str, // input: &str,
register_count: usize, // register_count: usize,
symbol_table: &mut SymbolTable, // symbol_table: &mut SymbolTable,
types_table: &mut TypesTable, // types_table: &mut TypesTable,
fn_body_scope_id: &mut Option<usize>, // fn_body_scope_id: &mut Option<usize>,
fn_local_variables: &HashMap<Rc<VariableSymbol>, Rc<RefCell<IrVariable>>>, // fn_local_variables: &HashMap<Rc<VariableSymbol>, Rc<RefCell<IrVariable>>>,
offset_counter: &mut OffsetCounter, // offset_counter: &mut OffsetCounter,
constants_table: &mut ConstantsTable, // constants_table: &mut ConstantsTable,
) -> Result<Function, Vec<Diagnostic>> { // ) -> Result<Function, Vec<Diagnostic>> {
// // parse // // // parse
// let (mut expression, parse_diagnostics) = parse_expression(input); // // let (mut expression, parse_diagnostics) = parse_expression(input);
// if !parse_diagnostics.is_empty() { // // if !parse_diagnostics.is_empty() {
// return Err(parse_diagnostics); // // return Err(parse_diagnostics);
// } // // }
// // //
// // init scopes, if necessary // // // init scopes, if necessary
// let container_scope = prepare_scopes(symbol_table, fn_body_scope_id); // // let container_scope = prepare_scopes(symbol_table, fn_body_scope_id);
// // //
// // inner scopes // // // inner scopes
// expression.init_scopes(symbol_table, container_scope); // // expression.init_scopes(symbol_table, container_scope);
// // //
// // names // // // names
// let diagnostics = expression.check_static_fn_local_names(&symbol_table); // // let diagnostics = expression.check_static_fn_local_names(&symbol_table);
// if !diagnostics.is_empty() { // // if !diagnostics.is_empty() {
// return Err(diagnostics); // // return Err(diagnostics);
// } // // }
// // //
// // type check // // // type check
// expression.type_check(&symbol_table, types_table)?; // // expression.type_check(&symbol_table, types_table)?;
// // //
// // synthesize a function // // // synthesize a function
// // init ir_builder // // // init ir_builder
// let mut ir_builder = IrBuilder::new(); // // let mut ir_builder = IrBuilder::new();
// // //
// // copy all previous declared variables to here so we preserve their stack offsets // // // copy all previous declared variables to here so we preserve their stack offsets
// for (key, value) in fn_local_variables { // // for (key, value) in fn_local_variables {
// ir_builder // // ir_builder
// .local_variables_mut() // // .local_variables_mut()
// .insert(key.clone(), value.clone()); // // .insert(key.clone(), value.clone());
// } // // }
// // //
// let entry_block_id = ir_builder.new_block(); // // let entry_block_id = ir_builder.new_block();
// // //
// let maybe_ir_expression = // // let maybe_ir_expression =
// expression.to_ir_expression(&mut ir_builder, &symbol_table, &types_table); // // expression.to_ir_expression(&mut ir_builder, &symbol_table, &types_table);
// // //
// // if Some, return the value // // // if Some, return the value
// ir_builder // // ir_builder
// .current_block_mut() // // .current_block_mut()
// .add_statement(IrStatement::Return(IrReturn::new(maybe_ir_expression))); // // .add_statement(IrStatement::Return(IrReturn::new(maybe_ir_expression)));
// // //
// ir_builder.finish_block(); // // ir_builder.finish_block();
// let entry_block = ir_builder.get_block(entry_block_id); // // let entry_block = ir_builder.get_block(entry_block_id);
// // //
// let mut ir_function = IrFunction::new( // // let mut ir_function = IrFunction::new(
// "__repl".into(), // // "__repl".into(),
// vec![], // // vec![],
// expression.type_info(&symbol_table, &types_table), // // expression.type_info(&symbol_table, &types_table),
// entry_block.clone(), // // entry_block.clone(),
// ); // // );
// // //
// // spilled registers are put on the stack, so we need to add it to our stack size // // // spilled registers are put on the stack, so we need to add it to our stack size
// ir_function.assign_registers(register_count, offset_counter); // // ir_function.assign_registers(register_count, offset_counter);
// // //
// Ok(ir_function.assemble(offset_counter.get_count(), constants_table)) // // Ok(ir_function.assemble(offset_counter.get_count(), constants_table))
todo!() // todo!()
} // }
//
fn compile_let_statement( // fn compile_let_statement(
input: &str, // input: &str,
register_count: usize, // register_count: usize,
symbol_table: &mut SymbolTable, // symbol_table: &mut SymbolTable,
body_scope_id: &mut Option<usize>, // body_scope_id: &mut Option<usize>,
offset_counter: &mut OffsetCounter, // offset_counter: &mut OffsetCounter,
local_variables: &mut HashMap<Rc<VariableSymbol>, Rc<RefCell<IrVariable>>>, // local_variables: &mut HashMap<Rc<VariableSymbol>, Rc<RefCell<IrVariable>>>,
types_table: &mut TypesTable, // types_table: &mut TypesTable,
constants_table: &mut ConstantsTable, // constants_table: &mut ConstantsTable,
) -> Result<Function, Vec<Diagnostic>> { // ) -> Result<Function, Vec<Diagnostic>> {
// // parse // // // parse
// let (maybe_let_statement, parse_diagnostics) = parse_let_statement(input); // // let (maybe_let_statement, parse_diagnostics) = parse_let_statement(input);
// if !parse_diagnostics.is_empty() { // // if !parse_diagnostics.is_empty() {
// return Err(parse_diagnostics); // // return Err(parse_diagnostics);
// } // // }
// let mut let_statement = maybe_let_statement.unwrap(); // // let mut let_statement = maybe_let_statement.unwrap();
// // //
// // names // // // names
// let container_scope_id = prepare_scopes(symbol_table, body_scope_id); // // let container_scope_id = prepare_scopes(symbol_table, body_scope_id);
// // //
// let_statement.init_scopes(symbol_table, container_scope_id); // // let_statement.init_scopes(symbol_table, container_scope_id);
// // //
// let name_diagnostics = let_statement.analyze_static_fn_local_names(symbol_table); // // let name_diagnostics = let_statement.analyze_static_fn_local_names(symbol_table);
// if !name_diagnostics.is_empty() { // // if !name_diagnostics.is_empty() {
// return Err(name_diagnostics); // // return Err(name_diagnostics);
// } // // }
// // //
// // types // // // types
// let_statement.type_check(&symbol_table, types_table)?; // // let_statement.type_check(&symbol_table, types_table)?;
// // //
// // init the ir builder // // // init the ir builder
// let mut ir_builder = IrBuilder::new(); // // let mut ir_builder = IrBuilder::new();
// // //
// // put previous locals in ir_builder so expressions can find them // // // put previous locals in ir_builder so expressions can find them
// for (k, v) in local_variables.iter() { // // for (k, v) in local_variables.iter() {
// ir_builder // // ir_builder
// .local_variables_mut() // // .local_variables_mut()
// .insert(k.clone(), v.clone()); // // .insert(k.clone(), v.clone());
// } // // }
// // //
// // ir function // // // ir function
// let entry_block_id = ir_builder.new_block(); // // let entry_block_id = ir_builder.new_block();
// // //
// let destination_ir_variable = let_statement.to_repl_ir( // // let destination_ir_variable = let_statement.to_repl_ir(
// &mut ir_builder, // // &mut ir_builder,
// symbol_table, // // symbol_table,
// types_table, // // types_table,
// offset_counter.next() as isize, // // offset_counter.next() as isize,
// ); // put it on top // // ); // put it on top
// // //
// // Now that we've translated to ir, we can add the new local variable in the IrBuilder to our // // // Now that we've translated to ir, we can add the new local variable in the IrBuilder to our
// // record of them to be used for next loop iteration. // // // record of them to be used for next loop iteration.
// let variable_symbol = let_statement.get_destination_symbol(symbol_table); // // let variable_symbol = let_statement.get_destination_symbol(symbol_table);
// local_variables.insert(variable_symbol, destination_ir_variable); // // local_variables.insert(variable_symbol, destination_ir_variable);
// // //
// ir_builder.finish_block(); // // ir_builder.finish_block();
// let entry_block = ir_builder.get_block(entry_block_id); // // let entry_block = ir_builder.get_block(entry_block_id);
// let mut ir_function = IrFunction::new( // // let mut ir_function = IrFunction::new(
// "__repl".into(), // // "__repl".into(),
// vec![], // // vec![],
// &TypeInfo::Void, // // &TypeInfo::Void,
// entry_block.clone(), // // entry_block.clone(),
// ); // // );
// // //
// // By here, the variables should all be assigned to their new or existing stack slots. // // // By here, the variables should all be assigned to their new or existing stack slots.
// // Register allocation should only be required for temp variables. // // // Register allocation should only be required for temp variables.
// ir_function.assign_registers(register_count, offset_counter); // // ir_function.assign_registers(register_count, offset_counter);
// // //
// Ok(ir_function.assemble(offset_counter.get_count(), constants_table)) // // Ok(ir_function.assemble(offset_counter.get_count(), constants_table))
todo!() // todo!()
} // }
pub fn repl_2(read: &mut impl BufRead, register_count: usize) { pub fn repl_2(read: &mut impl BufRead, register_count: usize) {
let mut buffer = String::new(); let mut buffer = String::new();
let mut analysis_context = AnalysisContext::new(); let mut analysis_context = AnalysisContext::new();
analysis_context.push_scope("__repl_root_scope");
let root_scope_id = analysis_context.root_scope_id(); let root_scope_id = analysis_context.root_scope_id();
analysis_context analysis_context
.scopes_mut() .scopes_mut()

View File

@ -85,7 +85,7 @@ pub fn compile_statement_to_synthetic_function(
register_count: usize, register_count: usize,
constants_table: &mut ConstantsTable, constants_table: &mut ConstantsTable,
) -> Result<Function, Diagnostics> { ) -> Result<Function, Diagnostics> {
let diagnostics = analyze_statement(statement, analysis_context, function_scope_id); let diagnostics = analyze_statement(statement, analysis_context);
if !diagnostics.is_empty() { if !diagnostics.is_empty() {
return Err(diagnostics); return Err(diagnostics);
} }

View File

@ -1,4 +1,3 @@
use crate::ast::NodeId;
use crate::ast::assign_statement::AssignStatement; use crate::ast::assign_statement::AssignStatement;
use crate::ast::binary_expression::BinaryExpression; use crate::ast::binary_expression::BinaryExpression;
use crate::ast::call::Call; use crate::ast::call::Call;
@ -11,141 +10,69 @@ use crate::ast::identifier::Identifier;
use crate::ast::let_statement::LetStatement; use crate::ast::let_statement::LetStatement;
use crate::ast::negative_expression::NegativeExpression; use crate::ast::negative_expression::NegativeExpression;
use crate::ast::statement::Statement; use crate::ast::statement::Statement;
use crate::semantic_analysis::scope::{Scope, ScopeId}; use crate::semantic_analysis::AnalysisContext;
use std::collections::HashMap;
struct ScopeCollectionContext<'a> {
scopes: &'a mut Vec<Scope>,
nodes_to_scopes: &'a mut HashMap<NodeId, ScopeId>,
current_scope_id: Option<ScopeId>,
}
impl<'a> ScopeCollectionContext<'a> {
pub fn new(
scopes: &'a mut Vec<Scope>,
nodes_to_scopes: &'a mut HashMap<NodeId, ScopeId>,
current_scope_id: ScopeId,
) -> Self {
Self {
scopes,
nodes_to_scopes,
current_scope_id: Some(current_scope_id),
}
}
pub fn down_scope(&mut self) -> ScopeId {
let scope = Scope::new(self.current_scope_id); // current is parent before push
self.scopes.push(scope);
self.current_scope_id = Some(self.scopes.len() - 1);
self.current_scope_id.unwrap()
}
pub fn up_scope(&mut self) {
let current_scope_id = self
.current_scope_id
.expect("Cannot up_scope() when there is no current_scope");
self.current_scope_id = self.scopes[current_scope_id].parent_id();
}
pub fn current_scope_id(&self) -> Option<ScopeId> {
self.current_scope_id
}
pub fn nodes_to_scopes_mut(&mut self) -> &mut HashMap<NodeId, ScopeId> {
&mut self.nodes_to_scopes
}
}
/// N.b.: the parent_scope_id refers to the **parent** of the compilation unit scope, and must /// N.b.: the parent_scope_id refers to the **parent** of the compilation unit scope, and must
/// be a valid (non-panicking) index of `scopes`. /// be a valid (non-panicking) index of `scopes`.
pub fn collect_scopes_in_compilation_unit( pub fn collect_scopes_compilation_unit(
compilation_unit: &CompilationUnit, compilation_unit: &CompilationUnit,
scopes: &mut Vec<Scope>, ctx: &mut AnalysisContext,
nodes_to_scopes: &mut HashMap<NodeId, ScopeId>,
parent_scope_id: ScopeId,
) { ) {
let mut ctx = ScopeCollectionContext::new(scopes, nodes_to_scopes, parent_scope_id); ctx.push_scope("compilation_unit");
ctx.down_scope(); // compilation unit scope
for function in compilation_unit.functions() { for function in compilation_unit.functions() {
collect_scopes_function(function, &mut ctx); collect_scopes_function(function, ctx);
} }
for extern_function in compilation_unit.extern_functions() { for extern_function in compilation_unit.extern_functions() {
collect_scopes_extern_function(extern_function, &mut ctx); collect_scopes_extern_function(extern_function, ctx);
} }
ctx.up_scope(); // compilation unit scope ctx.pop_scope(); // compilation_unit
} }
/// N.b. the parent_scope_id refers to the scope of the statement and must be a valid fn collect_scopes_function(function: &Function, ctx: &mut AnalysisContext) {
/// (non-panicking) index of `scopes`. // associate function with current scope
pub fn collect_scopes_in_statement( ctx.associate_node_to_current_scope(function.node_id());
statement: &Statement,
scopes: &mut Vec<Scope>,
nodes_to_scopes: &mut HashMap<NodeId, ScopeId>,
parent_scope_id: ScopeId,
) {
let mut ctx = ScopeCollectionContext::new(scopes, nodes_to_scopes, parent_scope_id);
collect_scopes_statement(statement, &mut ctx);
}
fn collect_scopes_function(function: &Function, ctx: &mut ScopeCollectionContext) { // push function params scope
// get containing scope id ctx.push_scope("function_parameters");
let containing_scope_id = ctx.current_scope_id().unwrap(); // guaranteed because functions are in modules
// save function's containing scope id
ctx.nodes_to_scopes_mut()
.insert(function.node_id(), containing_scope_id);
// push function scope
let function_scope_id = ctx.down_scope();
// save return-type and parameters' scope id // save return-type and parameters' scope id
for parameter in function.parameters() { for parameter in function.parameters() {
ctx.nodes_to_scopes_mut() ctx.associate_node_to_current_scope(parameter.node_id());
.insert(parameter.node_id(), function_scope_id);
} }
match function.return_type() { match function.return_type() {
None => { None => {
// no-op // no-op
} }
Some(type_use) => { Some(type_use) => {
ctx.nodes_to_scopes_mut() ctx.associate_node_to_current_scope(type_use.node_id());
.insert(type_use.node_id(), function_scope_id);
} }
} }
// push block scope for body // push block scope for body
ctx.down_scope(); ctx.push_scope("function_body");
for statement in function.statements() { for statement in function.statements() {
collect_scopes_statement(statement, ctx); collect_scopes_statement(statement, ctx);
} }
ctx.up_scope(); // block ctx.pop_scope(); // body
ctx.up_scope(); // function ctx.pop_scope(); // parameters
} }
fn collect_scopes_extern_function( fn collect_scopes_extern_function(extern_function: &ExternFunction, ctx: &mut AnalysisContext) {
extern_function: &ExternFunction, // associate extern_function with current scope
ctx: &mut ScopeCollectionContext, ctx.associate_node_to_current_scope(extern_function.node_id());
) {
let containing_scope_id = ctx.current_scope_id().unwrap();
ctx.nodes_to_scopes_mut()
.insert(extern_function.node_id(), containing_scope_id);
let function_scope_id = ctx.down_scope();
// parameters scope
ctx.push_scope("extern_function_parameters");
for parameter in extern_function.parameters() { for parameter in extern_function.parameters() {
ctx.nodes_to_scopes_mut() ctx.associate_node_to_current_scope(parameter.node_id());
.insert(parameter.node_id(), function_scope_id);
} }
ctx.nodes_to_scopes_mut() ctx.associate_node_to_current_scope(extern_function.return_type().node_id());
.insert(extern_function.return_type().node_id(), function_scope_id); ctx.pop_scope(); // parameters
ctx.up_scope();
} }
fn collect_scopes_statement(statement: &Statement, ctx: &mut ScopeCollectionContext) { pub fn collect_scopes_statement(statement: &Statement, ctx: &mut AnalysisContext) {
match statement { match statement {
Statement::Let(let_statement) => collect_scopes_let_statement(let_statement, ctx), Statement::Let(let_statement) => collect_scopes_let_statement(let_statement, ctx),
Statement::Expression(expression_statement) => { Statement::Expression(expression_statement) => {
@ -157,29 +84,24 @@ fn collect_scopes_statement(statement: &Statement, ctx: &mut ScopeCollectionCont
} }
} }
fn collect_scopes_let_statement(let_statement: &LetStatement, ctx: &mut ScopeCollectionContext) { fn collect_scopes_let_statement(let_statement: &LetStatement, ctx: &mut AnalysisContext) {
collect_scopes_expression(let_statement.initializer(), ctx); collect_scopes_expression(let_statement.initializer(), ctx);
let current_scope_id = ctx.current_scope_id().unwrap(); ctx.associate_node_to_current_scope(let_statement.node_id());
ctx.nodes_to_scopes_mut()
.insert(let_statement.node_id(), current_scope_id);
} }
fn collect_scopes_expression_statement( fn collect_scopes_expression_statement(
expression_statement: &ExpressionStatement, expression_statement: &ExpressionStatement,
ctx: &mut ScopeCollectionContext, ctx: &mut AnalysisContext,
) { ) {
collect_scopes_expression(expression_statement.expression(), ctx); collect_scopes_expression(expression_statement.expression(), ctx);
} }
fn collect_scopes_assign_statement( fn collect_scopes_assign_statement(assign_statement: &AssignStatement, ctx: &mut AnalysisContext) {
assign_statement: &AssignStatement,
ctx: &mut ScopeCollectionContext,
) {
collect_scopes_expression(assign_statement.value(), ctx); collect_scopes_expression(assign_statement.value(), ctx);
collect_scopes_expression(assign_statement.destination(), ctx); collect_scopes_expression(assign_statement.destination(), ctx);
} }
fn collect_scopes_expression(expression: &Expression, ctx: &mut ScopeCollectionContext) { fn collect_scopes_expression(expression: &Expression, ctx: &mut AnalysisContext) {
match expression { match expression {
Expression::Binary(binary_expression) => { Expression::Binary(binary_expression) => {
collect_scopes_binary_expression(binary_expression, ctx); collect_scopes_binary_expression(binary_expression, ctx);
@ -201,7 +123,7 @@ fn collect_scopes_expression(expression: &Expression, ctx: &mut ScopeCollectionC
fn collect_scopes_binary_expression( fn collect_scopes_binary_expression(
binary_expression: &BinaryExpression, binary_expression: &BinaryExpression,
ctx: &mut ScopeCollectionContext, ctx: &mut AnalysisContext,
) { ) {
collect_scopes_expression(binary_expression.lhs(), ctx); collect_scopes_expression(binary_expression.lhs(), ctx);
collect_scopes_expression(binary_expression.rhs(), ctx); collect_scopes_expression(binary_expression.rhs(), ctx);
@ -209,20 +131,18 @@ fn collect_scopes_binary_expression(
fn collect_scopes_negative_expression( fn collect_scopes_negative_expression(
negative_expression: &NegativeExpression, negative_expression: &NegativeExpression,
ctx: &mut ScopeCollectionContext, ctx: &mut AnalysisContext,
) { ) {
collect_scopes_expression(negative_expression.operand(), ctx); collect_scopes_expression(negative_expression.operand(), ctx);
} }
fn collect_scopes_call(call: &Call, ctx: &mut ScopeCollectionContext) { fn collect_scopes_call(call: &Call, ctx: &mut AnalysisContext) {
for argument in call.arguments() { for argument in call.arguments() {
collect_scopes_expression(argument, ctx); collect_scopes_expression(argument, ctx);
} }
collect_scopes_expression(call.callee(), ctx); collect_scopes_expression(call.callee(), ctx);
} }
fn collect_scopes_identifier(identifier: &Identifier, ctx: &mut ScopeCollectionContext) { fn collect_scopes_identifier(identifier: &Identifier, ctx: &mut AnalysisContext) {
let current_scope_id = ctx.current_scope_id().unwrap(); // if this fails, we tried to do this without any context ctx.associate_node_to_current_scope(identifier.node_id());
ctx.nodes_to_scopes_mut()
.insert(identifier.node_id(), current_scope_id);
} }

View File

@ -1,107 +1,36 @@
use crate::ast::NodeId;
use crate::ast::compilation_unit::CompilationUnit; use crate::ast::compilation_unit::CompilationUnit;
use crate::ast::extern_function::ExternFunction; use crate::ast::extern_function::ExternFunction;
use crate::ast::function::Function; use crate::ast::function::Function;
use crate::ast::parameter::Parameter; use crate::ast::parameter::Parameter;
use crate::ast::statement::Statement;
use crate::diagnostic::Diagnostics; use crate::diagnostic::Diagnostics;
use crate::semantic_analysis::diagnostic_helpers::symbol_already_declared; use crate::semantic_analysis::AnalysisContext;
use crate::semantic_analysis::scope::{Scope, ScopeId};
use crate::semantic_analysis::symbol::{FunctionSymbol, ParameterSymbol, Symbol}; use crate::semantic_analysis::symbol::{FunctionSymbol, ParameterSymbol, Symbol};
use std::collections::HashMap;
use std::rc::Rc;
pub struct SymbolCollectionResult(pub Diagnostics); pub struct SymbolCollectionResult(pub Diagnostics);
struct SymbolCollectionContext<'a> { pub fn collect_symbols_compilation_unit(
scopes: &'a mut Vec<Scope>,
nodes_to_scopes: &'a HashMap<NodeId, ScopeId>,
symbols: &'a mut Vec<Symbol>,
fqn_context: Vec<Rc<str>>,
diagnostics: Diagnostics,
}
impl<'a> SymbolCollectionContext<'a> {
pub fn new(
scopes: &'a mut Vec<Scope>,
nodes_to_scopes: &'a HashMap<NodeId, ScopeId>,
symbols: &'a mut Vec<Symbol>,
) -> Self {
Self {
scopes,
nodes_to_scopes,
symbols,
fqn_context: Vec::new(),
diagnostics: Diagnostics::new(),
}
}
pub fn find_symbol_in_scope_for(&self, name: &str, node_id: NodeId) -> Option<&Symbol> {
let scope_id = self.nodes_to_scopes[&node_id];
let scope = &self.scopes[scope_id];
if let Some(symbol_id) = scope.get_symbol_id(name) {
Some(&self.symbols[symbol_id])
} else {
None
}
}
pub fn insert_symbol(&mut self, symbol: Symbol, node_id: NodeId) {
let declared_name = symbol.declared_name_owned();
self.symbols.push(symbol);
let symbol_id = self.symbols.len() - 1;
let scope_id = self.nodes_to_scopes[&node_id];
let scope = &mut self.scopes[scope_id];
scope.insert_symbol(declared_name, symbol_id);
}
pub fn symbols_mut(&mut self) -> &mut Vec<Symbol> {
&mut self.symbols
}
pub fn diagnostics_mut(&mut self) -> &mut Diagnostics {
&mut self.diagnostics
}
fn join_fqn_parts(parts: &[Rc<str>]) -> String {
parts.join("::")
}
pub fn get_fqn_base(&self) -> String {
Self::join_fqn_parts(&self.fqn_context)
}
pub fn resolve_fqn(&self, suffix: &Rc<str>) -> String {
let base = self.get_fqn_base();
if base.is_empty() {
suffix.to_string()
} else {
Self::join_fqn_parts(&[base.into(), suffix.clone()])
}
}
}
pub fn collect_symbols_in_compilation_unit(
compilation_unit: &CompilationUnit, compilation_unit: &CompilationUnit,
scopes: &mut Vec<Scope>, analysis_context: &mut AnalysisContext,
nodes_to_scopes: &HashMap<NodeId, ScopeId>,
symbols: &mut Vec<Symbol>,
) -> SymbolCollectionResult { ) -> SymbolCollectionResult {
let mut ctx = SymbolCollectionContext::new(scopes, nodes_to_scopes, symbols); let mut diagnostics = Diagnostics::new();
for function in compilation_unit.functions() { for function in compilation_unit.functions() {
collect_symbols_function(function, &mut ctx); collect_symbols_function(function, analysis_context, &mut diagnostics);
} }
for extern_function in compilation_unit.extern_functions() { for extern_function in compilation_unit.extern_functions() {
collect_symbols_extern_function(extern_function, &mut ctx); collect_symbols_extern_function(extern_function, analysis_context, &mut diagnostics);
} }
SymbolCollectionResult(ctx.diagnostics) SymbolCollectionResult(diagnostics)
} }
fn collect_symbols_function(function: &Function, ctx: &mut SymbolCollectionContext) { fn collect_symbols_function(
let fqn = ctx.resolve_fqn(&function.declared_name_owned()).into(); function: &Function,
ctx: &mut AnalysisContext,
diagnostics: &mut Diagnostics,
) {
let fqn = ctx.resolve_fqn(&function.declared_name()).into();
// function itself // function itself
let function_symbol = Symbol::Function(FunctionSymbol::new( let function_symbol = Symbol::Function(FunctionSymbol::new(
@ -112,18 +41,15 @@ fn collect_symbols_function(function: &Function, ctx: &mut SymbolCollectionConte
)); ));
// insert // insert
if let Some(already_declared) = if let Err(diagnostic) =
ctx.find_symbol_in_scope_for(function.declared_name(), function.node_id()) ctx.try_insert_associate_symbol_in_node_scope(function_symbol, function.node_id())
{ {
let diagnostic = symbol_already_declared(already_declared, &function_symbol); diagnostics.push(diagnostic);
ctx.diagnostics_mut().push(diagnostic);
} else {
ctx.insert_symbol(function_symbol, function.node_id());
} }
// parameters // parameters
for parameter in function.parameters() { for parameter in function.parameters() {
collect_symbols_parameter(parameter, ctx); collect_symbols_parameter(parameter, ctx, diagnostics);
} }
// n.b. do not do statements yet, because variables are declared and resolved in the resolution pass // n.b. do not do statements yet, because variables are declared and resolved in the resolution pass
@ -131,7 +57,8 @@ fn collect_symbols_function(function: &Function, ctx: &mut SymbolCollectionConte
fn collect_symbols_extern_function( fn collect_symbols_extern_function(
extern_function: &ExternFunction, extern_function: &ExternFunction,
ctx: &mut SymbolCollectionContext, ctx: &mut AnalysisContext,
diagnostics: &mut Diagnostics,
) { ) {
let fqn = ctx let fqn = ctx
.resolve_fqn(&extern_function.declared_name_owned()) .resolve_fqn(&extern_function.declared_name_owned())
@ -146,25 +73,30 @@ fn collect_symbols_extern_function(
)); ));
// insert function symbol // insert function symbol
if let Some(already_declared) = if let Err(diagnostic) =
ctx.find_symbol_in_scope_for(extern_function.declared_name(), extern_function.node_id()) ctx.try_insert_associate_symbol_in_node_scope(function_symbol, extern_function.node_id())
{ {
let diagnostic = symbol_already_declared(already_declared, &function_symbol); diagnostics.push(diagnostic);
ctx.diagnostics_mut().push(diagnostic);
} else {
ctx.insert_symbol(function_symbol, extern_function.node_id());
} }
// parameters // parameters
for parameter in extern_function.parameters() { for parameter in extern_function.parameters() {
collect_symbols_parameter(parameter, ctx); collect_symbols_parameter(parameter, ctx, diagnostics);
} }
} }
fn collect_symbols_parameter(parameter: &Parameter, ctx: &mut SymbolCollectionContext) { fn collect_symbols_parameter(
let parameter_symbol = ParameterSymbol::new( parameter: &Parameter,
ctx: &mut AnalysisContext,
diagnostics: &mut Diagnostics,
) {
let parameter_symbol = Symbol::Parameter(ParameterSymbol::new(
parameter.declared_name_owned(), parameter.declared_name_owned(),
Some(parameter.declared_name_source_range()), Some(parameter.declared_name_source_range()),
); ));
ctx.insert_symbol(Symbol::Parameter(parameter_symbol), parameter.node_id()); if let Err(diagnostic) =
ctx.try_insert_associate_symbol_in_node_scope(parameter_symbol, parameter.node_id())
{
diagnostics.push(diagnostic);
}
} }

View File

@ -1,14 +1,15 @@
use crate::ast::NodeId; use crate::ast::NodeId;
use crate::ast::compilation_unit::CompilationUnit; use crate::ast::compilation_unit::CompilationUnit;
use crate::ast::statement::Statement; use crate::ast::statement::Statement;
use crate::diagnostic::Diagnostics; use crate::diagnostic::{Diagnostic, Diagnostics};
use crate::semantic_analysis::collect_scopes::{ use crate::semantic_analysis::collect_scopes::{
collect_scopes_in_compilation_unit, collect_scopes_in_statement, collect_scopes_compilation_unit, collect_scopes_statement,
}; };
use crate::semantic_analysis::collect_symbols::{ use crate::semantic_analysis::collect_symbols::{
SymbolCollectionResult, collect_symbols_in_compilation_unit, SymbolCollectionResult, collect_symbols_compilation_unit,
}; };
use crate::semantic_analysis::collect_types::collect_types; use crate::semantic_analysis::collect_types::collect_types;
use crate::semantic_analysis::diagnostic_helpers::symbol_already_declared;
use crate::semantic_analysis::resolve_names::{ use crate::semantic_analysis::resolve_names::{
NameResolutionResult, resolve_names_in_compilation_unit, resolve_names_in_statement, NameResolutionResult, resolve_names_in_compilation_unit, resolve_names_in_statement,
}; };
@ -19,6 +20,7 @@ use crate::semantic_analysis::scope::{Scope, ScopeId};
use crate::semantic_analysis::symbol::{Symbol, SymbolId}; use crate::semantic_analysis::symbol::{Symbol, SymbolId};
use crate::semantic_analysis::type_info::{TypeInfo, TypeInfoId}; use crate::semantic_analysis::type_info::{TypeInfo, TypeInfoId};
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc;
mod collect_scopes; mod collect_scopes;
mod collect_symbols; mod collect_symbols;
@ -31,8 +33,10 @@ pub mod symbol;
pub mod type_info; pub mod type_info;
pub struct AnalysisContext { pub struct AnalysisContext {
fqn_stack: Vec<Rc<str>>,
root_scope_id: ScopeId, root_scope_id: ScopeId,
scopes: Vec<Scope>, scopes: Vec<Scope>,
current_scope_id: Option<ScopeId>,
nodes_to_scopes: HashMap<NodeId, ScopeId>, nodes_to_scopes: HashMap<NodeId, ScopeId>,
symbols: Vec<Symbol>, symbols: Vec<Symbol>,
nodes_to_symbols: HashMap<NodeId, SymbolId>, nodes_to_symbols: HashMap<NodeId, SymbolId>,
@ -44,8 +48,10 @@ pub struct AnalysisContext {
impl AnalysisContext { impl AnalysisContext {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
fqn_stack: Vec::new(),
root_scope_id: 0, // pushed in vec! below root_scope_id: 0, // pushed in vec! below
scopes: vec![Scope::new(None)], scopes: vec![Scope::new(None)],
current_scope_id: None,
nodes_to_scopes: HashMap::new(), nodes_to_scopes: HashMap::new(),
symbols: Vec::new(), symbols: Vec::new(),
nodes_to_symbols: HashMap::new(), nodes_to_symbols: HashMap::new(),
@ -55,6 +61,14 @@ impl AnalysisContext {
} }
} }
pub fn commit(&mut self) {
todo!()
}
pub fn rollback(&mut self) {
todo!()
}
pub fn root_scope_id(&self) -> ScopeId { pub fn root_scope_id(&self) -> ScopeId {
self.root_scope_id self.root_scope_id
} }
@ -86,6 +100,125 @@ impl AnalysisContext {
pub fn nodes_to_type_infos(&self) -> &HashMap<NodeId, TypeInfoId> { pub fn nodes_to_type_infos(&self) -> &HashMap<NodeId, TypeInfoId> {
&self.nodes_to_type_infos &self.nodes_to_type_infos
} }
pub fn push_fqn_part(&mut self, part: Rc<str>) {
self.fqn_stack.push(part);
}
pub fn pop_fqn_part(&mut self) {
self.fqn_stack.pop();
}
pub fn resolve_fqn(&self, suffix: &str) -> String {
let mut fqn = self.fqn_stack.clone();
fqn.push(suffix.into());
fqn.join("::")
}
pub fn push_scope(&mut self, _debug_name: &str) {
let scope = Scope::new(self.current_scope_id);
self.scopes.push(scope);
self.current_scope_id = Some(self.scopes.len() - 1);
}
fn get_scope(&self, scope_id: ScopeId) -> &Scope {
self.scopes.get(scope_id).expect(&format!(
"scope_id {} is not a valid index of self.scopes",
scope_id
))
}
fn get_scope_mut(&mut self, scope_id: ScopeId) -> &mut Scope {
self.scopes.get_mut(scope_id).expect(&format!(
"scope_id {} is not a valid index of self.scopes",
scope_id
))
}
fn get_current_scope(&self) -> &Scope {
self.get_scope(self.current_scope_id.expect("current_scope_id is None"))
}
fn get_current_scope_mut(&mut self) -> &mut Scope {
self.get_scope_mut(self.current_scope_id.expect("current_scope_id is None"))
}
pub fn pop_scope(&mut self) {
self.current_scope_id = self.get_current_scope().parent_id();
}
pub fn associate_node_to_current_scope(&mut self, node_id: NodeId) {
self.nodes_to_scopes.insert(
node_id,
self.current_scope_id.expect("current_scope_id is None"),
);
}
fn insert_symbol_in_scope(&mut self, scope_id: ScopeId, symbol: Symbol) -> SymbolId {
// get declared name
let declared_name = symbol.declared_name_owned();
// push the symbol
self.symbols.push(symbol);
let symbol_id = self.symbols.len() - 1;
// put it in scope
self.get_scope_mut(scope_id)
.insert_symbol(declared_name, symbol_id);
symbol_id
}
pub fn associate_node_to_symbol(&mut self, node_id: NodeId, symbol_id: SymbolId) {
self.nodes_to_symbols.insert(node_id, symbol_id);
}
fn get_symbol_in_scope(&self, scope: &Scope, declared_name: &str) -> Option<&Symbol> {
scope.get_symbol_id(declared_name).map(|symbol_id| {
self.symbols.get(symbol_id).expect(&format!(
"symbol_id {} is not a valid index of self.symbols",
symbol_id
))
})
}
/// Use this function only when there is no node associated with the symbol. Otherwise use
/// `try_insert_symbol_in_node_scope`.
pub fn try_insert_symbol_in_scope(
&mut self,
to_insert: Symbol,
scope_id: ScopeId,
) -> Result<(), Diagnostic> {
let scope = self.get_scope(scope_id);
if let Some(already_declared) = self.get_symbol_in_scope(scope, to_insert.declared_name()) {
Err(symbol_already_declared(already_declared, &to_insert))
} else {
self.insert_symbol_in_scope(scope_id, to_insert);
// no associated node, so no need to update self.nodes_to_symbols
Ok(())
}
}
pub fn try_insert_associate_symbol_in_node_scope(
&mut self,
to_insert: Symbol,
owner_node_id: NodeId,
) -> Result<(), Diagnostic> {
let node_scope_id = self.nodes_to_scopes.get(&owner_node_id).expect(&format!(
"owner_node_id {} is not in self.nodes_to_scopes",
owner_node_id
));
let node_scope = self.get_scope(*node_scope_id);
if let Some(already_declared) =
self.get_symbol_in_scope(node_scope, to_insert.declared_name())
{
Err(symbol_already_declared(already_declared, &to_insert))
} else {
let symbol_id = self.insert_symbol_in_scope(*node_scope_id, to_insert);
self.associate_node_to_symbol(owner_node_id, symbol_id);
Ok(())
}
}
} }
pub fn analyze_compilation_unit( pub fn analyze_compilation_unit(
@ -94,20 +227,10 @@ pub fn analyze_compilation_unit(
) -> Diagnostics { ) -> Diagnostics {
let mut diagnostics = Diagnostics::new(); let mut diagnostics = Diagnostics::new();
collect_scopes_in_compilation_unit( collect_scopes_compilation_unit(compilation_unit, ctx);
compilation_unit,
&mut ctx.scopes,
&mut ctx.nodes_to_scopes,
ctx.root_scope_id,
);
let SymbolCollectionResult(mut collect_symbols_diagnostics) = let SymbolCollectionResult(mut collect_symbols_diagnostics) =
collect_symbols_in_compilation_unit( collect_symbols_compilation_unit(compilation_unit, ctx);
compilation_unit,
&mut ctx.scopes,
&ctx.nodes_to_scopes,
&mut ctx.symbols,
);
diagnostics.append(&mut collect_symbols_diagnostics); diagnostics.append(&mut collect_symbols_diagnostics);
let NameResolutionResult(mut resolve_names_diagnostics) = resolve_names_in_compilation_unit( let NameResolutionResult(mut resolve_names_diagnostics) = resolve_names_in_compilation_unit(
@ -138,19 +261,10 @@ pub fn analyze_compilation_unit(
diagnostics diagnostics
} }
pub fn analyze_statement( pub fn analyze_statement(statement: &Statement, ctx: &mut AnalysisContext) -> Diagnostics {
statement: &Statement,
ctx: &mut AnalysisContext,
function_scope_id: ScopeId,
) -> Diagnostics {
let mut diagnostics = Diagnostics::new(); let mut diagnostics = Diagnostics::new();
collect_scopes_in_statement( collect_scopes_statement(statement, ctx);
statement,
&mut ctx.scopes,
&mut ctx.nodes_to_scopes,
function_scope_id,
);
// collect symbols for a statement is currently a no-op, so not needed here // collect symbols for a statement is currently a no-op, so not needed here