More progress on getting repl let statements to work.

This commit is contained in:
Jesse Brault 2026-08-06 15:17:15 -05:00
parent 2175d5f20a
commit 6b51545114
7 changed files with 138 additions and 99 deletions

View File

@ -3,15 +3,20 @@ 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::Diagnostics; use dmc_lib::diagnostic::Diagnostics;
use dmc_lib::ir::ir_parameter::{IrParameter, IrParameterId};
use dmc_lib::ir::ir_variable::{IrVariable, IrVariableId};
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::lowering::SyntheticFunctionLoweringContext;
use dmc_lib::parser::{parse_expression, parse_let_statement}; use dmc_lib::parser::{parse_expression, parse_let_statement};
use dmc_lib::semantic_analysis::analysis_context::AnalysisContext; use dmc_lib::semantic_analysis::analysis_context::AnalysisContext;
use dmc_lib::semantic_analysis::symbol::SymbolId;
use dmc_lib::token::TokenKind; use dmc_lib::token::TokenKind;
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::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;
@ -308,6 +313,12 @@ pub fn repl_2(read: &mut impl BufRead, register_count: usize) {
analysis_context.push_scope("__repl_body_scope"); analysis_context.push_scope("__repl_body_scope");
let fqn: Rc<str> = Rc::from("__repl"); let fqn: Rc<str> = Rc::from("__repl");
let mut ir_variables: Vec<IrVariable> = Vec::new();
let mut ir_parameters: Vec<IrParameter> = Vec::new();
let mut symbols_to_variables: HashMap<SymbolId, IrVariableId> = HashMap::new();
let mut symbols_to_parameters: HashMap<SymbolId, IrParameterId> = HashMap::new();
let mut variable_locations = VariableLocations::new(); let mut variable_locations = VariableLocations::new();
let mut constants_table = ConstantsTable::new(); let mut constants_table = ConstantsTable::new();
@ -345,6 +356,12 @@ pub fn repl_2(read: &mut impl BufRead, register_count: usize) {
input, input,
&mut analysis_context, &mut analysis_context,
&fqn, &fqn,
SyntheticFunctionLoweringContext {
ir_variables: &mut ir_variables,
symbols_to_variables: &mut symbols_to_variables,
ir_parameters: &mut ir_parameters,
symbols_to_parameters: &mut symbols_to_parameters,
},
&mut variable_locations, &mut variable_locations,
register_count, register_count,
&mut constants_table, &mut constants_table,
@ -369,6 +386,12 @@ pub fn repl_2(read: &mut impl BufRead, register_count: usize) {
input, input,
&mut analysis_context, &mut analysis_context,
&fqn, &fqn,
SyntheticFunctionLoweringContext {
ir_variables: &mut ir_variables,
symbols_to_variables: &mut symbols_to_variables,
ir_parameters: &mut ir_parameters,
symbols_to_parameters: &mut symbols_to_parameters,
},
&mut variable_locations, &mut variable_locations,
register_count, register_count,
&mut constants_table, &mut constants_table,
@ -428,6 +451,7 @@ fn compile_expression_2(
input: &str, input: &str,
analysis_context: &mut AnalysisContext, analysis_context: &mut AnalysisContext,
fqn: &Rc<str>, fqn: &Rc<str>,
synthetic_function_lowering_context: SyntheticFunctionLoweringContext,
variable_locations: &mut VariableLocations, variable_locations: &mut VariableLocations,
register_count: usize, register_count: usize,
constants_table: &mut ConstantsTable, constants_table: &mut ConstantsTable,
@ -443,6 +467,7 @@ fn compile_expression_2(
&statement, &statement,
analysis_context, analysis_context,
fqn.clone(), fqn.clone(),
synthetic_function_lowering_context,
variable_locations, variable_locations,
register_count, register_count,
constants_table, constants_table,
@ -453,6 +478,7 @@ fn compile_let_statement_2(
input: &str, input: &str,
analysis_context: &mut AnalysisContext, analysis_context: &mut AnalysisContext,
fqn: &Rc<str>, fqn: &Rc<str>,
synthetic_function_lowering_context: SyntheticFunctionLoweringContext,
variable_locations: &mut VariableLocations, variable_locations: &mut VariableLocations,
register_count: usize, register_count: usize,
constants_table: &mut ConstantsTable, constants_table: &mut ConstantsTable,
@ -465,6 +491,7 @@ fn compile_let_statement_2(
&Statement::Let(maybe_let_statement.unwrap()), &Statement::Let(maybe_let_statement.unwrap()),
analysis_context, analysis_context,
fqn.clone(), fqn.clone(),
synthetic_function_lowering_context,
variable_locations, variable_locations,
register_count, register_count,
constants_table, constants_table,

View File

@ -5,7 +5,7 @@ use std::rc::Rc;
pub type IrParameterId = usize; pub type IrParameterId = usize;
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct IrParameter { pub struct IrParameter {
name: Rc<str>, name: Rc<str>,
type_info: IrTypeInfo, type_info: IrTypeInfo,

View File

@ -2,7 +2,7 @@ use std::fmt::{Display, Formatter};
pub type IrTypeInfoId = usize; pub type IrTypeInfoId = usize;
#[derive(Debug)] #[derive(Clone, Debug)]
pub enum IrTypeInfo { pub enum IrTypeInfo {
String, String,
Int, Int,

View File

@ -4,7 +4,7 @@ use std::rc::Rc;
pub type IrVariableId = usize; pub type IrVariableId = usize;
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct IrVariable { pub struct IrVariable {
name: Rc<str>, name: Rc<str>,
type_info: IrTypeInfo, type_info: IrTypeInfo,

View File

@ -3,7 +3,9 @@ use crate::constants_table::ConstantsTable;
use crate::diagnostic::Diagnostics; use crate::diagnostic::Diagnostics;
use crate::ir::compile_dvm_function; use crate::ir::compile_dvm_function;
use crate::ir::variable_locations::VariableLocations; use crate::ir::variable_locations::VariableLocations;
use crate::lowering::{lower_to_ir_compilation_unit, lower_to_ir_synthetic_function}; use crate::lowering::{
SyntheticFunctionLoweringContext, lower_to_ir_compilation_unit, lower_to_ir_synthetic_function,
};
use crate::parser::parse_compilation_unit; use crate::parser::parse_compilation_unit;
use crate::semantic_analysis::{analyze_compilation_unit, analyze_statement}; use crate::semantic_analysis::{analyze_compilation_unit, analyze_statement};
use dvm_lib::vm::function::Function; use dvm_lib::vm::function::Function;
@ -79,6 +81,7 @@ pub fn compile_statement_to_synthetic_function(
statement: &Statement, statement: &Statement,
analysis_context: &mut AnalysisContext, analysis_context: &mut AnalysisContext,
fqn: Rc<str>, fqn: Rc<str>,
synthetic_function_lowering_context: SyntheticFunctionLoweringContext,
variable_locations: &mut VariableLocations, variable_locations: &mut VariableLocations,
register_count: usize, register_count: usize,
constants_table: &mut ConstantsTable, constants_table: &mut ConstantsTable,
@ -87,7 +90,12 @@ pub fn compile_statement_to_synthetic_function(
if !diagnostics.is_empty() { if !diagnostics.is_empty() {
return Err(diagnostics); return Err(diagnostics);
} }
let ir_function = lower_to_ir_synthetic_function(statement, analysis_context, fqn); let ir_function = lower_to_ir_synthetic_function(
statement,
analysis_context,
fqn,
synthetic_function_lowering_context,
);
Ok(compile_dvm_function( Ok(compile_dvm_function(
&ir_function, &ir_function,
register_count, register_count,

View File

@ -1,6 +1,5 @@
mod util; mod util;
use crate::ast::NodeId;
use crate::ast::assign_statement::AssignStatement; use crate::ast::assign_statement::AssignStatement;
use crate::ast::binary_expression::BinaryOperation; use crate::ast::binary_expression::BinaryOperation;
use crate::ast::call::Call; use crate::ast::call::Call;
@ -25,7 +24,7 @@ use crate::ir::ir_variable::{IrVariable, IrVariableId};
use crate::lowering::util::{return_type_info_to_ir_type_info, to_ir_type_info}; use crate::lowering::util::{return_type_info_to_ir_type_info, to_ir_type_info};
use crate::semantic_analysis::analysis_context::AnalysisContext; use crate::semantic_analysis::analysis_context::AnalysisContext;
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;
use std::collections::HashMap; use std::collections::HashMap;
use std::ops::Neg; use std::ops::Neg;
use std::rc::Rc; use std::rc::Rc;
@ -34,55 +33,40 @@ pub struct LowerToIrResult {
pub functions: Vec<IrFunction>, pub functions: Vec<IrFunction>,
} }
#[derive(Debug)]
struct LowerToIrContext<'a> {
symbols: &'a [Symbol],
nodes_to_symbols: &'a HashMap<NodeId, SymbolId>,
type_infos: &'a [TypeInfo],
symbols_to_type_infos: &'a HashMap<SymbolId, TypeInfoId>,
nodes_to_type_infos: &'a HashMap<NodeId, TypeInfoId>,
ir_functions: Vec<IrFunction>,
}
pub fn lower_to_ir_compilation_unit( pub fn lower_to_ir_compilation_unit(
compilation_unit: &CompilationUnit, compilation_unit: &CompilationUnit,
analysis_context: &AnalysisContext, analysis_context: &AnalysisContext,
) -> LowerToIrResult { ) -> LowerToIrResult {
let mut ctx = LowerToIrContext {
symbols: analysis_context.symbols(),
nodes_to_symbols: analysis_context.nodes_to_symbols(),
type_infos: analysis_context.type_infos(),
symbols_to_type_infos: analysis_context.symbols_to_type_infos(),
nodes_to_type_infos: analysis_context.nodes_to_type_infos(),
ir_functions: Vec::new(),
};
for function in compilation_unit.functions() {
lower_to_ir_function(function, &mut ctx);
}
LowerToIrResult { LowerToIrResult {
functions: ctx.ir_functions, functions: compilation_unit
.functions()
.iter()
.map(|function| lower_to_ir_function(function, analysis_context))
.collect(),
} }
} }
pub struct SyntheticFunctionLoweringContext<'a> {
pub ir_variables: &'a mut Vec<IrVariable>,
pub symbols_to_variables: &'a mut HashMap<SymbolId, IrVariableId>,
pub ir_parameters: &'a mut Vec<IrParameter>,
pub symbols_to_parameters: &'a mut HashMap<SymbolId, IrParameterId>,
}
pub fn lower_to_ir_synthetic_function( pub fn lower_to_ir_synthetic_function(
statement: &Statement, statement: &Statement,
analysis_ctx: &AnalysisContext, analysis_ctx: &AnalysisContext,
fqn: Rc<str>, fqn: Rc<str>,
synthetic_function_lowering_context: SyntheticFunctionLoweringContext,
) -> IrFunction { ) -> IrFunction {
let mut ctx = LowerToIrContext { let mut fn_ctx = LowerToIrFunctionContext::with_variables_and_parameters(
symbols: analysis_ctx.symbols(), synthetic_function_lowering_context.ir_variables,
nodes_to_symbols: analysis_ctx.nodes_to_symbols(), synthetic_function_lowering_context.symbols_to_variables,
type_infos: analysis_ctx.type_infos(), synthetic_function_lowering_context.ir_parameters,
symbols_to_type_infos: analysis_ctx.symbols_to_type_infos(), synthetic_function_lowering_context.symbols_to_parameters,
nodes_to_type_infos: analysis_ctx.nodes_to_type_infos(), );
ir_functions: Vec::new(),
};
let mut fn_ctx = LowerToIrFunctionContext::new(); lower_to_ir_statement(statement, analysis_ctx, &mut fn_ctx, true);
lower_to_ir_statement(statement, &mut ctx, &mut fn_ctx, true);
fn_ctx.finish_block(); fn_ctx.finish_block();
// infer return type from statement // infer return type from statement
@ -90,40 +74,45 @@ pub fn lower_to_ir_synthetic_function(
Statement::Let(_) | Statement::Assign(_) => None, Statement::Let(_) | Statement::Assign(_) => None,
Statement::Expression(expression_statement) => { Statement::Expression(expression_statement) => {
let type_info_id = let type_info_id =
ctx.nodes_to_type_infos[&expression_statement.expression().node_id()]; analysis_ctx.nodes_to_type_infos()[&expression_statement.expression().node_id()];
let type_info = &ctx.type_infos[type_info_id]; let type_info = &analysis_ctx.type_infos()[type_info_id];
return_type_info_to_ir_type_info(type_info) return_type_info_to_ir_type_info(type_info)
} }
}; };
IrFunction::new( IrFunction::new(
fqn, fqn,
fn_ctx.ir_parameters, fn_ctx.ir_parameters.clone(), // provide just a snapshot of those
fn_ctx.ir_variables, fn_ctx.ir_variables.clone(),
maybe_return_ir_type_info, maybe_return_ir_type_info,
fn_ctx.blocks, fn_ctx.blocks,
) )
} }
#[derive(Debug)] #[derive(Debug)]
struct LowerToIrFunctionContext { struct LowerToIrFunctionContext<'a> {
ir_variables: &'a mut Vec<IrVariable>,
symbols_to_variables: &'a mut HashMap<SymbolId, IrVariableId>,
ir_parameters: &'a mut Vec<IrParameter>,
symbols_to_parameters: &'a mut HashMap<SymbolId, IrParameterId>,
blocks: Vec<IrBlock>, blocks: Vec<IrBlock>,
ir_variables: Vec<IrVariable>,
symbols_to_variables: HashMap<SymbolId, IrVariableId>,
ir_parameters: Vec<IrParameter>,
symbols_to_parameters: HashMap<SymbolId, IrParameterId>,
current_block_statements: Vec<IrStatement>, current_block_statements: Vec<IrStatement>,
t_var_counter: usize, t_var_counter: usize,
} }
impl LowerToIrFunctionContext { impl<'a> LowerToIrFunctionContext<'a> {
fn new() -> Self { fn with_variables_and_parameters(
ir_variables: &'a mut Vec<IrVariable>,
symbols_to_variables: &'a mut HashMap<SymbolId, IrVariableId>,
ir_parameters: &'a mut Vec<IrParameter>,
symbols_to_parameters: &'a mut HashMap<SymbolId, IrParameterId>,
) -> Self {
Self { Self {
ir_variables,
symbols_to_variables,
ir_parameters,
symbols_to_parameters,
blocks: Vec::new(), blocks: Vec::new(),
ir_variables: Vec::new(),
symbols_to_variables: HashMap::new(),
ir_parameters: Vec::new(),
symbols_to_parameters: HashMap::new(),
current_block_statements: Vec::new(), current_block_statements: Vec::new(),
t_var_counter: 0, t_var_counter: 0,
} }
@ -179,23 +168,33 @@ impl LowerToIrFunctionContext {
} }
} }
fn lower_to_ir_function(function: &Function, ctx: &mut LowerToIrContext) { fn lower_to_ir_function(function: &Function, ctx: &AnalysisContext) -> IrFunction {
let mut fn_ctx = LowerToIrFunctionContext::new(); let mut ir_variables = Vec::new();
let mut symbols_to_variables = HashMap::new();
let mut ir_parameters = Vec::new();
let mut symbols_to_parameters = HashMap::new();
let mut fn_ctx = LowerToIrFunctionContext::with_variables_and_parameters(
&mut ir_variables,
&mut symbols_to_variables,
&mut ir_parameters,
&mut symbols_to_parameters,
);
lower_to_ir_parameters(function, ctx, &mut fn_ctx); lower_to_ir_parameters(function, ctx, &mut fn_ctx);
// get various function info // get various function info
let function_symbol_id = ctx.nodes_to_symbols[&function.node_id()]; let function_symbol_id = ctx.nodes_to_symbols()[&function.node_id()];
let function_symbol = match &ctx.symbols[function_symbol_id] { let function_symbol = match &ctx.symbols()[function_symbol_id] {
Symbol::Function(function_symbol) => function_symbol, Symbol::Function(function_symbol) => function_symbol,
_ => panic!("Expected FunctionSymbol"), _ => panic!("Expected FunctionSymbol"),
}; };
let function_type_info_id = ctx.symbols_to_type_infos[&function_symbol_id]; let function_type_info_id = ctx.symbols_to_type_infos()[&function_symbol_id];
let function_type_info = match &ctx.type_infos[function_type_info_id] { let function_type_info = match &ctx.type_infos()[function_type_info_id] {
TypeInfo::Function(function_type_info) => function_type_info, TypeInfo::Function(function_type_info) => function_type_info,
_ => panic!("Expected FunctionTypeInfo"), _ => panic!("Expected FunctionTypeInfo"),
}; };
let return_type_info = &ctx.type_infos[function_type_info.return_type_id()]; let return_type_info = &ctx.type_infos()[function_type_info.return_type_id()];
let is_void_function = match return_type_info { let is_void_function = match return_type_info {
TypeInfo::Void => true, TypeInfo::Void => true,
_ => false, _ => false,
@ -209,26 +208,25 @@ fn lower_to_ir_function(function: &Function, ctx: &mut LowerToIrContext) {
} }
fn_ctx.finish_block(); fn_ctx.finish_block();
let ir_function = IrFunction::new( let blocks = fn_ctx.blocks; // must move it out beforehand to satisfy borrow checker
IrFunction::new(
function_symbol.fqn_owned(), function_symbol.fqn_owned(),
fn_ctx.ir_parameters, ir_parameters,
fn_ctx.ir_variables, ir_variables,
return_type_info_to_ir_type_info(return_type_info), return_type_info_to_ir_type_info(return_type_info),
fn_ctx.blocks, blocks,
); )
ctx.ir_functions.push(ir_function);
} }
fn lower_to_ir_parameters( fn lower_to_ir_parameters(
function: &Function, function: &Function,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
) { ) {
let n_parameters = function.parameters().len() as isize; let n_parameters = function.parameters().len() as isize;
for (i, parameter) in function.parameters().iter().enumerate() { for (i, parameter) in function.parameters().iter().enumerate() {
let parameter_type_info_id = ctx.nodes_to_type_infos[&parameter.node_id()]; let parameter_type_info_id = ctx.nodes_to_type_infos()[&parameter.node_id()];
let parameter_type_info = &ctx.type_infos[parameter_type_info_id]; let parameter_type_info = &ctx.type_infos()[parameter_type_info_id];
let ir_parameter = IrParameter::new( let ir_parameter = IrParameter::new(
parameter.declared_name(), parameter.declared_name(),
to_ir_type_info(parameter_type_info), to_ir_type_info(parameter_type_info),
@ -236,14 +234,14 @@ fn lower_to_ir_parameters(
); );
fn_ctx.insert_ir_parameter( fn_ctx.insert_ir_parameter(
ir_parameter, ir_parameter,
Some(ctx.nodes_to_symbols[&parameter.node_id()]), Some(ctx.nodes_to_symbols()[&parameter.node_id()]),
); );
} }
} }
fn lower_to_ir_statement( fn lower_to_ir_statement(
statement: &Statement, statement: &Statement,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
can_return_value: bool, can_return_value: bool,
) { ) {
@ -277,13 +275,13 @@ fn lower_binary_operator(binary_operation: &BinaryOperation) -> IrBinaryOperator
fn lower_to_ir_let_statement( fn lower_to_ir_let_statement(
let_statement: &LetStatement, let_statement: &LetStatement,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
) { ) {
let symbol_id = ctx.nodes_to_symbols[&let_statement.node_id()]; let symbol_id = ctx.nodes_to_symbols()[&let_statement.node_id()];
let type_info_id = ctx.nodes_to_type_infos[&let_statement.node_id()]; let type_info_id = ctx.nodes_to_type_infos()[&let_statement.node_id()];
let type_info = &ctx.type_infos[type_info_id]; let type_info = &ctx.type_infos()[type_info_id];
let ir_variable = IrVariable::new(let_statement.declared_name(), to_ir_type_info(type_info)); let ir_variable = IrVariable::new(let_statement.declared_name(), to_ir_type_info(type_info));
let ir_variable_id = fn_ctx.insert_ir_variable(ir_variable, Some(symbol_id)); let ir_variable_id = fn_ctx.insert_ir_variable(ir_variable, Some(symbol_id));
@ -298,7 +296,7 @@ fn lower_to_ir_let_statement(
fn lower_to_ir_expression_statement( fn lower_to_ir_expression_statement(
expression_statement: &ExpressionStatement, expression_statement: &ExpressionStatement,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
returns_value: bool, returns_value: bool,
) { ) {
@ -314,8 +312,8 @@ fn lower_to_ir_expression_statement(
lower_expression_to_ir_operation(expression_statement.expression(), ctx, fn_ctx); lower_expression_to_ir_operation(expression_statement.expression(), ctx, fn_ctx);
let result_type_info_id = let result_type_info_id =
ctx.nodes_to_type_infos[&expression_statement.expression().node_id()]; ctx.nodes_to_type_infos()[&expression_statement.expression().node_id()];
let result_type_info = &ctx.type_infos[result_type_info_id]; let result_type_info = &ctx.type_infos()[result_type_info_id];
let t_var_ir_variable_id = fn_ctx.make_t_var(to_ir_type_info(result_type_info)); let t_var_ir_variable_id = fn_ctx.make_t_var(to_ir_type_info(result_type_info));
let ir_statement = IrStatement::Assign(IrAssign::new(t_var_ir_variable_id, ir_operation)); let ir_statement = IrStatement::Assign(IrAssign::new(t_var_ir_variable_id, ir_operation));
@ -325,12 +323,12 @@ fn lower_to_ir_expression_statement(
fn lower_to_ir_assign_statement( fn lower_to_ir_assign_statement(
assign_statement: &AssignStatement, assign_statement: &AssignStatement,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
) { ) {
match assign_statement.destination() { match assign_statement.destination() {
Expression::Identifier(identifier) => { Expression::Identifier(identifier) => {
let destination_symbol_id = ctx.nodes_to_symbols[&identifier.node_id()]; let destination_symbol_id = ctx.nodes_to_symbols()[&identifier.node_id()];
let destination_ir_variable_id = fn_ctx.symbols_to_variables[&destination_symbol_id]; let destination_ir_variable_id = fn_ctx.symbols_to_variables[&destination_symbol_id];
let ir_operation = let ir_operation =
@ -345,7 +343,7 @@ fn lower_to_ir_assign_statement(
fn lower_expression_to_ir_operation( fn lower_expression_to_ir_operation(
expression: &Expression, expression: &Expression,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
) -> IrOperation { ) -> IrOperation {
match expression { match expression {
@ -369,7 +367,7 @@ fn lower_expression_to_ir_operation(
} }
Expression::Call(call) => IrOperation::Call(lower_to_ir_call(call, ctx, fn_ctx)), Expression::Call(call) => IrOperation::Call(lower_to_ir_call(call, ctx, fn_ctx)),
Expression::Identifier(identifier) => { Expression::Identifier(identifier) => {
let identifier_symbol_id = ctx.nodes_to_symbols[&identifier.node_id()]; let identifier_symbol_id = ctx.nodes_to_symbols()[&identifier.node_id()];
let identifier_ir_variable_id = fn_ctx.symbols_to_variables[&identifier_symbol_id]; let identifier_ir_variable_id = fn_ctx.symbols_to_variables[&identifier_symbol_id];
let ir_expression = IrExpression::Variable(identifier_ir_variable_id); let ir_expression = IrExpression::Variable(identifier_ir_variable_id);
IrOperation::Load(ir_expression) IrOperation::Load(ir_expression)
@ -388,7 +386,7 @@ fn lower_expression_to_ir_operation(
fn lower_expression_to_ir_expression( fn lower_expression_to_ir_expression(
expression: &Expression, expression: &Expression,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
) -> IrExpression { ) -> IrExpression {
match expression { match expression {
@ -403,8 +401,8 @@ fn lower_expression_to_ir_expression(
)); ));
// make destination temp var // make destination temp var
let result_type_info_id = ctx.nodes_to_type_infos[&binary_expression.node_id()]; let result_type_info_id = ctx.nodes_to_type_infos()[&binary_expression.node_id()];
let result_type_info = &ctx.type_infos[result_type_info_id]; let result_type_info = &ctx.type_infos()[result_type_info_id];
let destination_ir_variable_id = fn_ctx.make_t_var(to_ir_type_info(result_type_info)); let destination_ir_variable_id = fn_ctx.make_t_var(to_ir_type_info(result_type_info));
// make assign statement to destination temp var // make assign statement to destination temp var
@ -425,8 +423,8 @@ fn lower_expression_to_ir_expression(
negative_one, negative_one,
IrBinaryOperator::Multiply, IrBinaryOperator::Multiply,
)); ));
let result_type_info_id = ctx.nodes_to_type_infos[&negative_expression.node_id()]; let result_type_info_id = ctx.nodes_to_type_infos()[&negative_expression.node_id()];
let result_type_info = &ctx.type_infos[result_type_info_id]; let result_type_info = &ctx.type_infos()[result_type_info_id];
let destination_ir_variable_id = fn_ctx.make_t_var(to_ir_type_info(result_type_info)); let destination_ir_variable_id = fn_ctx.make_t_var(to_ir_type_info(result_type_info));
let ir_assign = IrAssign::new(destination_ir_variable_id, ir_operation); let ir_assign = IrAssign::new(destination_ir_variable_id, ir_operation);
@ -441,8 +439,8 @@ fn lower_expression_to_ir_expression(
let ir_call = lower_to_ir_call(call, ctx, fn_ctx); let ir_call = lower_to_ir_call(call, ctx, fn_ctx);
// make temp var // make temp var
let return_type_info_id = ctx.nodes_to_type_infos[&call.node_id()]; let return_type_info_id = ctx.nodes_to_type_infos()[&call.node_id()];
let return_type_info = &ctx.type_infos[return_type_info_id]; let return_type_info = &ctx.type_infos()[return_type_info_id];
let return_ir_type_info = to_ir_type_info(return_type_info); let return_ir_type_info = to_ir_type_info(return_type_info);
let t_var_ir_variable_id = fn_ctx.make_t_var(return_ir_type_info); let t_var_ir_variable_id = fn_ctx.make_t_var(return_ir_type_info);
@ -457,7 +455,7 @@ fn lower_expression_to_ir_expression(
IrExpression::Variable(t_var_ir_variable_id) IrExpression::Variable(t_var_ir_variable_id)
} }
Expression::Identifier(identifier) => { Expression::Identifier(identifier) => {
let rhs_symbol_id = ctx.nodes_to_symbols[&identifier.node_id()]; let rhs_symbol_id = ctx.nodes_to_symbols()[&identifier.node_id()];
if let Some(rhs_ir_variable_id) = fn_ctx.symbols_to_variables.get(&rhs_symbol_id) { if let Some(rhs_ir_variable_id) = fn_ctx.symbols_to_variables.get(&rhs_symbol_id) {
IrExpression::Variable(*rhs_ir_variable_id) IrExpression::Variable(*rhs_ir_variable_id)
} else if let Some(rhs_ir_parameter_id) = } else if let Some(rhs_ir_parameter_id) =
@ -465,7 +463,6 @@ fn lower_expression_to_ir_expression(
{ {
IrExpression::Parameter(*rhs_ir_parameter_id) IrExpression::Parameter(*rhs_ir_parameter_id)
} else { } else {
println!("Dump:\n{:#?}\n{:#?}", ctx, fn_ctx);
panic!( panic!(
"Could not find parameter or variable for symbol_id {}", "Could not find parameter or variable for symbol_id {}",
rhs_symbol_id rhs_symbol_id
@ -480,11 +477,11 @@ fn lower_expression_to_ir_expression(
fn lower_to_ir_call( fn lower_to_ir_call(
call: &Call, call: &Call,
ctx: &LowerToIrContext, ctx: &AnalysisContext,
fn_ctx: &mut LowerToIrFunctionContext, fn_ctx: &mut LowerToIrFunctionContext,
) -> IrCall { ) -> IrCall {
let callee_symbol_id = ctx.nodes_to_symbols[&call.callee().node_id()]; let callee_symbol_id = ctx.nodes_to_symbols()[&call.callee().node_id()];
let callee_symbol = &ctx.symbols[callee_symbol_id]; let callee_symbol = &ctx.symbols()[callee_symbol_id];
match callee_symbol { match callee_symbol {
Symbol::Function(function_symbol) => { Symbol::Function(function_symbol) => {
let arguments = call let arguments = call

View File

@ -252,6 +252,12 @@ pub fn loop_instructions<'a>(
{ {
let instruction = &call_stack.top().instructions()[call_stack.top().ip()]; let instruction = &call_stack.top().instructions()[call_stack.top().ip()];
if debug { if debug {
if let Some(top) = call_stack.maybe_top() {
println!("-- before instruction --");
println!(" stack: {:?}", top.stack());
println!(" registers: {:?}", registers);
println!(" rv: {:?}", top.return_value());
}
println!("{}", instruction); println!("{}", instruction);
} }
@ -717,6 +723,7 @@ pub fn loop_instructions<'a>(
if debug { if debug {
if let Some(top) = call_stack.maybe_top() { if let Some(top) = call_stack.maybe_top() {
println!("-- after instruction -- ");
println!(" stack: {:?}", top.stack()); println!(" stack: {:?}", top.stack());
println!(" registers: {:?}", registers); println!(" registers: {:?}", registers);
println!(" rv: {:?}", top.return_value()); println!(" rv: {:?}", top.return_value());