From 34480a0870030ead60b969f5e29bba4531a07b83 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 11 May 2026 13:22:27 -0500 Subject: [PATCH] Filling various code holes. WIP. --- dmc-lib/src/ast/assign_statement.rs | 57 +++++++++++++++++++++++++ dmc-lib/src/ast/call.rs | 3 +- dmc-lib/src/ast/class.rs | 2 +- dmc-lib/src/ast/constructor.rs | 2 +- dmc-lib/src/ast/expression_statement.rs | 21 +++++++++ dmc-lib/src/ast/function.rs | 2 +- dmc-lib/src/ast/statement.rs | 21 +++++++-- 7 files changed, 100 insertions(+), 8 deletions(-) diff --git a/dmc-lib/src/ast/assign_statement.rs b/dmc-lib/src/ast/assign_statement.rs index 17a094e..54dd62e 100644 --- a/dmc-lib/src/ast/assign_statement.rs +++ b/dmc-lib/src/ast/assign_statement.rs @@ -12,6 +12,7 @@ use crate::ir::ir_assign::IrAssign; use crate::ir::ir_set_field::IrSetField; use crate::ir::ir_statement::IrStatement; use crate::source_range::SourceRange; +use crate::symbol::Symbol; use crate::symbol::class_symbol::ClassSymbol; use crate::symbol::expressible_symbol::ExpressibleSymbol; use crate::symbol_table::SymbolTable; @@ -226,6 +227,7 @@ impl AssignStatement { (nodes_to_types, diagnostics) } + #[deprecated] pub fn type_check( &mut self, symbol_table: &SymbolTable, @@ -358,6 +360,7 @@ impl AssignStatement { } } + #[deprecated] pub fn to_ir( &self, builder: &mut IrBuilder, @@ -399,6 +402,60 @@ impl AssignStatement { builder.current_block_mut().add_statement(ir_statement); } + + pub fn lower_to_ir( + &self, + builder: &mut IrBuilder, + nodes_to_symbols: &NodesToSymbols, + symbols_to_types: &SymbolsToTypes, + nodes_to_types: &NodesToTypes, + ) { + let destination_expressible_symbol = match self.destination.as_ref() { + Expression::Identifier(identifier) => nodes_to_symbols + .get(&identifier.scope_id()) + .unwrap() + .unwrap_expressible_symbol(), + _ => unreachable!("Destination must be a mutable L value"), + }; + + let ir_statement = match destination_expressible_symbol { + ExpressibleSymbol::Field(field_symbol) => { + let field_type = symbols_to_types + .get(&Symbol::Field(field_symbol.clone())) + .unwrap(); + let mut_field_pointer_variable = + get_or_init_mut_field_pointer_variable(builder, &field_symbol, field_type) + .clone(); + + let ir_set_field = IrSetField::new( + &mut_field_pointer_variable, + self.value.lower_to_ir_expression( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ), + ); + IrStatement::SetField(ir_set_field) + } + ExpressibleSymbol::Variable(variable_symbol) => { + let ir_variable = builder.local_variables().get(&variable_symbol).unwrap(); + let ir_assign = IrAssign::new( + ir_variable.clone(), + self.value.lower_to_ir_operation( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ), + ); + IrStatement::Assign(ir_assign) + } + _ => unreachable!("Destination must be a mutable L value"), + }; + + builder.current_block_mut().add_statement(ir_statement); + } } #[cfg(test)] diff --git a/dmc-lib/src/ast/call.rs b/dmc-lib/src/ast/call.rs index 5d3a5dd..506edfa 100644 --- a/dmc-lib/src/ast/call.rs +++ b/dmc-lib/src/ast/call.rs @@ -10,14 +10,13 @@ use crate::diagnostic_factories::{ use crate::ir::ir_call::IrCall; use crate::ir::ir_expression::IrExpression; use crate::source_range::SourceRange; -use crate::symbol::Symbol; use crate::symbol::callable_symbol::CallableSymbol; use crate::symbol::class_symbol::ClassSymbol; use crate::symbol::expressible_symbol::ExpressibleSymbol; +use crate::symbol::Symbol; use crate::symbol_table::SymbolTable; use crate::type_info::TypeInfo; use crate::types_table::TypesTable; -use std::thread::Builder; pub struct Call { node_id: NodeId, diff --git a/dmc-lib/src/ast/class.rs b/dmc-lib/src/ast/class.rs index 0aa14e6..576abca 100644 --- a/dmc-lib/src/ast/class.rs +++ b/dmc-lib/src/ast/class.rs @@ -17,9 +17,9 @@ use crate::error_codes::{FIELD_MULTIPLE_INIT, FIELD_UNINIT}; use crate::ir::ir_class::{IrClass, IrField}; use crate::ir::ir_function::IrFunction; use crate::source_range::SourceRange; +use crate::symbol::Symbol; use crate::symbol::class_symbol::ClassSymbol; use crate::symbol::constructor_symbol::ConstructorSymbol; -use crate::symbol::Symbol; use crate::symbol_table::SymbolTable; use crate::type_info::TypeInfo; use crate::types_table::TypesTable; diff --git a/dmc-lib/src/ast/constructor.rs b/dmc-lib/src/ast/constructor.rs index d41c676..bdf24a7 100644 --- a/dmc-lib/src/ast/constructor.rs +++ b/dmc-lib/src/ast/constructor.rs @@ -425,7 +425,7 @@ impl Constructor { // PART 3. Constructor statements for statement in &self.statements { - statement.lower( + statement.lower_to_ir( &mut ir_builder, nodes_to_symbols, symbols_to_types, diff --git a/dmc-lib/src/ast/expression_statement.rs b/dmc-lib/src/ast/expression_statement.rs index 58a083c..f5e9bff 100644 --- a/dmc-lib/src/ast/expression_statement.rs +++ b/dmc-lib/src/ast/expression_statement.rs @@ -140,4 +140,25 @@ impl ExpressionStatement { .add_statement(IrStatement::Return(IrReturn::new(ir_expression))); } } + + pub fn lower_to_ir( + &self, + builder: &mut IrBuilder, + nodes_to_symbols: &NodesToSymbols, + symbols_to_types: &SymbolsToTypes, + nodes_to_types: &NodesToTypes, + is_return_statement: bool, + ) { + let ir_expression = self.expression.lower_to_ir_expression( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ); + if is_return_statement { + builder + .current_block_mut() + .add_statement(IrStatement::Return(IrReturn::new(Some(ir_expression)))); + } + } } diff --git a/dmc-lib/src/ast/function.rs b/dmc-lib/src/ast/function.rs index cb1cf54..dce4d17 100644 --- a/dmc-lib/src/ast/function.rs +++ b/dmc-lib/src/ast/function.rs @@ -492,7 +492,7 @@ impl Function { let should_return_value = !matches!(return_type_info, TypeInfo::Void); for (i, statement) in self.statements.iter().enumerate() { let is_last = i == self.statements.len() - 1; - statement.lower( + statement.lower_to_ir( &mut builder, nodes_to_symbols, symbols_to_types, diff --git a/dmc-lib/src/ast/statement.rs b/dmc-lib/src/ast/statement.rs index 1c3ae97..b4a0610 100644 --- a/dmc-lib/src/ast/statement.rs +++ b/dmc-lib/src/ast/statement.rs @@ -199,7 +199,7 @@ impl Statement { } } - pub fn lower( + pub fn lower_to_ir( &self, builder: &mut IrBuilder, nodes_to_symbols: &NodesToSymbols, @@ -211,8 +211,23 @@ impl Statement { Statement::Let(let_statement) => { let_statement.lower(builder, nodes_to_symbols, symbols_to_types, nodes_to_types); } - Statement::Expression(expression_statement) => {} - Statement::Assign(assign_statement) => {} + Statement::Expression(expression_statement) => { + expression_statement.lower_to_ir( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + is_return_statement, + ); + } + Statement::Assign(assign_statement) => { + assign_statement.lower_to_ir( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ); + } } } }