From 11d368ab1a6f18233a28b0e38e750c254450f901 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Fri, 1 May 2026 14:19:01 -0500 Subject: [PATCH] Fix compile errors WIP. --- dmc-lib/src/ast/binary_expression.rs | 62 ++++++++++++++++++++++++++ dmc-lib/src/ast/expression.rs | 7 ++- dmc-lib/src/ast/let_statement.rs | 9 ++-- dmc-lib/src/ast/negative_expression.rs | 31 +++++++++++-- 4 files changed, 102 insertions(+), 7 deletions(-) diff --git a/dmc-lib/src/ast/binary_expression.rs b/dmc-lib/src/ast/binary_expression.rs index d6e7d4d..d7ce8e7 100644 --- a/dmc-lib/src/ast/binary_expression.rs +++ b/dmc-lib/src/ast/binary_expression.rs @@ -516,6 +516,42 @@ impl BinaryExpression { diagnostics_result!(diagnostics) } + pub fn lower_to_ir_operation( + &self, + builder: &mut IrBuilder, + nodes_to_symbols: &NodesToSymbols, + symbols_to_types: &SymbolsToTypes, + nodes_to_types: &NodesToTypes, + ) -> IrOperation { + let lhs = self.lhs.lower_to_ir_expression( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ); + let rhs = self.rhs.lower_to_ir_expression( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ); + + let ir_binary_operator = match &self.op { + BinaryOperation::Multiply => IrBinaryOperator::Multiply, + BinaryOperation::Divide => IrBinaryOperator::Divide, + BinaryOperation::Modulo => IrBinaryOperator::Modulo, + BinaryOperation::Add => IrBinaryOperator::Add, + BinaryOperation::Subtract => IrBinaryOperator::Subtract, + BinaryOperation::LeftShift => IrBinaryOperator::LeftShift, + BinaryOperation::RightShift => IrBinaryOperator::RightShift, + BinaryOperation::BitwiseAnd => IrBinaryOperator::BitwiseAnd, + BinaryOperation::BitwiseXor => IrBinaryOperator::BitwiseXor, + BinaryOperation::BitwiseOr => IrBinaryOperator::BitwiseOr, + }; + IrOperation::Binary(IrBinaryOperation::new(lhs, rhs, ir_binary_operator)) + } + + #[deprecated] pub fn to_ir_operation( &self, builder: &mut IrBuilder, @@ -559,6 +595,7 @@ impl BinaryExpression { IrOperation::Binary(ir_binary_operation) } + #[deprecated] pub fn to_ir_expression( &self, builder: &mut IrBuilder, @@ -578,4 +615,29 @@ impl BinaryExpression { .add_statement(IrStatement::Assign(ir_assign)); IrExpression::Variable(as_rc) } + + pub fn lower_to_ir_expression( + &self, + builder: &mut IrBuilder, + nodes_to_symbols: &NodesToSymbols, + symbols_to_types: &SymbolsToTypes, + nodes_to_types: &NodesToTypes, + ) -> IrExpression { + let ir_operation = + self.lower_to_ir_operation(builder, nodes_to_symbols, symbols_to_types, nodes_to_types); + + let type_info = nodes_to_types.get(&self.node_id).unwrap(); + + let t_var = Rc::new(RefCell::new(IrVariable::new_vr( + builder.new_t_var().into(), + builder.current_block().id(), + type_info, + ))); + + let ir_assign = IrAssign::new(t_var.clone(), ir_operation); + builder + .current_block_mut() + .add_statement(IrStatement::Assign(ir_assign)); + IrExpression::Variable(t_var) + } } diff --git a/dmc-lib/src/ast/expression.rs b/dmc-lib/src/ast/expression.rs index ad439c2..4f741ae 100644 --- a/dmc-lib/src/ast/expression.rs +++ b/dmc-lib/src/ast/expression.rs @@ -379,7 +379,12 @@ impl Expression { nodes_to_types: &NodesToTypes, ) -> IrOperation { match self { - Expression::Binary(binary_expression) => {} + Expression::Binary(binary_expression) => binary_expression.lower_to_ir_operation( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ), Expression::Negative(negative_expression) => { IrOperation::Load(negative_expression.lower_to_ir_expression( builder, diff --git a/dmc-lib/src/ast/let_statement.rs b/dmc-lib/src/ast/let_statement.rs index ad267bd..98a757e 100644 --- a/dmc-lib/src/ast/let_statement.rs +++ b/dmc-lib/src/ast/let_statement.rs @@ -329,9 +329,12 @@ impl LetStatement { symbols_to_types: &SymbolsToTypes, nodes_to_types: &NodesToTypes, ) { - let init_operation = self - .initializer - .lower_to_ir_operation(builder, nodes_to_types); + let init_operation = self.initializer.lower_to_ir_operation( + builder, + nodes_to_symbols, + symbols_to_types, + nodes_to_types, + ); let destination_symbol = nodes_to_symbols.get(&self.node_id).unwrap(); let destination_type_info = symbols_to_types.get(destination_symbol).unwrap(); diff --git a/dmc-lib/src/ast/negative_expression.rs b/dmc-lib/src/ast/negative_expression.rs index 35e05aa..c19d00d 100644 --- a/dmc-lib/src/ast/negative_expression.rs +++ b/dmc-lib/src/ast/negative_expression.rs @@ -209,15 +209,40 @@ impl NegativeExpression { IrExpression::Variable(destination) } - IrExpression::Variable(ir_variable) => {} - IrExpression::Int(i) => {} - IrExpression::Double(d) => {} + IrExpression::Variable(ir_variable) => { + let destination = Rc::new(RefCell::new(IrVariable::new_vr( + builder.new_t_var().into(), + builder.current_block().id(), + ir_variable.borrow().type_info(), + ))); + + let rhs = match ir_variable.borrow().type_info() { + TypeInfo::Integer => IrExpression::Int(-1), + TypeInfo::Double => IrExpression::Double(-1.0), + _ => panic!(), + }; + + let operation = IrOperation::Binary(IrBinaryOperation::new( + IrExpression::Variable(ir_variable), + rhs, + IrBinaryOperator::Multiply, + )); + + let ir_assign = IrAssign::new(destination.clone(), operation); + builder + .current_block_mut() + .add_statement(IrStatement::Assign(ir_assign)); + IrExpression::Variable(destination) + } + IrExpression::Int(i) => IrExpression::Int(i * -1), + IrExpression::Double(d) => IrExpression::Double(d * -1.0), IrExpression::String(_) => { panic!(); } } } + #[deprecated] pub fn to_ir( &self, builder: &mut IrBuilder,