From 8c1d56dc1a3f1822b908e0afc6d24326188d7432 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 2 Mar 2026 20:15:36 -0600 Subject: [PATCH] Remove ir structs and fix misc. warnings. --- dm-std-lib/src/lib.rs | 4 +- dmc-lib/src/ast/call.rs | 16 ------ dmc-lib/src/ast/expression.rs | 11 ---- dmc-lib/src/ast/expression_statement.rs | 21 ------- dmc-lib/src/ast/extern_function.rs | 13 +---- dmc-lib/src/ast/function.rs | 64 --------------------- dmc-lib/src/ast/identifier.rs | 7 --- dmc-lib/src/ast/integer_literal.rs | 6 -- dmc-lib/src/ast/let_statement.rs | 11 ---- dmc-lib/src/ast/module_level_declaration.rs | 4 +- dmc-lib/src/ast/statement.rs | 12 ---- dmc-lib/src/ast/string_literal.rs | 13 ----- dmc-lib/src/ir/assemble_context.rs | 7 --- dmc-lib/src/ir/ir_assign.rs | 51 ---------------- dmc-lib/src/ir/ir_call.rs | 53 ----------------- dmc-lib/src/ir/ir_constant.rs | 29 ---------- dmc-lib/src/ir/ir_expression.rs | 11 ---- dmc-lib/src/ir/ir_function.rs | 28 --------- dmc-lib/src/ir/ir_statement.rs | 19 ------ dmc-lib/src/ir/ir_variable.rs | 22 ------- dmc-lib/src/ir/mod.rs | 17 ------ dmc-lib/src/lib.rs | 1 - 22 files changed, 6 insertions(+), 414 deletions(-) delete mode 100644 dmc-lib/src/ir/assemble_context.rs delete mode 100644 dmc-lib/src/ir/ir_assign.rs delete mode 100644 dmc-lib/src/ir/ir_call.rs delete mode 100644 dmc-lib/src/ir/ir_constant.rs delete mode 100644 dmc-lib/src/ir/ir_expression.rs delete mode 100644 dmc-lib/src/ir/ir_function.rs delete mode 100644 dmc-lib/src/ir/ir_statement.rs delete mode 100644 dmc-lib/src/ir/ir_variable.rs delete mode 100644 dmc-lib/src/ir/mod.rs diff --git a/dm-std-lib/src/lib.rs b/dm-std-lib/src/lib.rs index f5aa64f..49b300e 100644 --- a/dm-std-lib/src/lib.rs +++ b/dm-std-lib/src/lib.rs @@ -36,8 +36,8 @@ impl Display for StdCoreError { impl Error for StdCoreError {} pub fn std_core_println( - context: &DvmContext, - state: &DvmState, + _context: &DvmContext, + _state: &DvmState, args: &[Value], ) -> Result> { let maybe_to_print = args.get(0); diff --git a/dmc-lib/src/ast/call.rs b/dmc-lib/src/ast/call.rs index ac5244c..9c6f698 100644 --- a/dmc-lib/src/ast/call.rs +++ b/dmc-lib/src/ast/call.rs @@ -3,11 +3,8 @@ use crate::asm::asm_instruction::{ }; use crate::ast::assemble_context::AssembleContext; use crate::ast::expression::Expression; -use crate::ast::function::FunctionLoweringContext; use crate::constants_table::ConstantsTable; use crate::diagnostic::Diagnostic; -use crate::ir::ir_call::IrCall; -use crate::ir::ir_expression::IrExpression; use crate::source_range::SourceRange; use crate::symbol::ExpressibleSymbol; use crate::symbol_table::SymbolTable; @@ -124,19 +121,6 @@ impl Call { } } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) -> IrExpression { - let function_name = match self.callee() { - Expression::Identifier(identifier) => identifier.name(), - _ => panic!("Calling things other than identifiers not yet supported."), - }; - let arguments = self - .arguments() - .iter() - .map(|arg| arg.lower_to_ir(context)) - .collect(); - IrExpression::Call(IrCall::new(function_name, arguments)) - } - pub fn assemble( &self, context: &mut AssembleContext, diff --git a/dmc-lib/src/ast/expression.rs b/dmc-lib/src/ast/expression.rs index 8695c95..8d9dbba 100644 --- a/dmc-lib/src/ast/expression.rs +++ b/dmc-lib/src/ast/expression.rs @@ -1,10 +1,8 @@ use crate::ast::call::Call; -use crate::ast::function::FunctionLoweringContext; use crate::ast::identifier::Identifier; use crate::ast::integer_literal::IntegerLiteral; use crate::ast::string_literal::StringLiteral; use crate::diagnostic::Diagnostic; -use crate::ir::ir_expression::IrExpression; use crate::source_range::SourceRange; use crate::symbol_table::SymbolTable; use crate::type_info::TypeInfo; @@ -57,13 +55,4 @@ impl Expression { Expression::Identifier(identifier) => identifier.source_range(), } } - - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) -> IrExpression { - match self { - Expression::Call(call) => call.lower_to_ir(context), - Expression::IntegerLiteral(integer_literal) => integer_literal.lower_to_ir(context), - Expression::String(string_literal) => string_literal.lower_to_ir(context), - Expression::Identifier(identifier) => identifier.lower_to_ir(context), - } - } } diff --git a/dmc-lib/src/ast/expression_statement.rs b/dmc-lib/src/ast/expression_statement.rs index ea6120e..35b4cd5 100644 --- a/dmc-lib/src/ast/expression_statement.rs +++ b/dmc-lib/src/ast/expression_statement.rs @@ -1,10 +1,7 @@ use crate::ast::assemble_context::AssembleContext; use crate::ast::expression::Expression; -use crate::ast::function::FunctionLoweringContext; use crate::constants_table::ConstantsTable; use crate::diagnostic::Diagnostic; -use crate::ir::ir_expression::IrExpression; -use crate::ir::ir_statement::IrStatement; use crate::symbol_table::SymbolTable; pub struct ExpressionStatement { @@ -34,24 +31,6 @@ impl ExpressionStatement { self.expression.type_check(symbol_table) } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) { - let ir_expression = self.expression.lower_to_ir(context); - match ir_expression { - IrExpression::Call(ir_call) => { - context.add_statement(IrStatement::Call(ir_call)); - } - IrExpression::Constant(ir_constant) => { - unimplemented!() - } - IrExpression::IntegerLiteral(i) => { - unimplemented!() - } - IrExpression::Variable(ir_variable) => { - unimplemented!() - } - } - } - pub fn assemble( &self, context: &mut AssembleContext, diff --git a/dmc-lib/src/ast/extern_function.rs b/dmc-lib/src/ast/extern_function.rs index f038863..948fc4d 100644 --- a/dmc-lib/src/ast/extern_function.rs +++ b/dmc-lib/src/ast/extern_function.rs @@ -80,22 +80,13 @@ impl ExternFunction { diagnostics } - pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec { + pub fn check_name_usages(&mut self, _symbol_table: &SymbolTable) -> Vec { // no-op (for now) vec![] } - pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec { + pub fn type_check(&mut self, _symbol_table: &SymbolTable) -> Vec { // no-op (for now) vec![] } - - pub fn assemble( - &self, - context: &mut AssembleContext, - symbol_table: &SymbolTable, - constants_table: &mut ConstantsTable, - ) { - // no-op - } } diff --git a/dmc-lib/src/ast/function.rs b/dmc-lib/src/ast/function.rs index 8da140b..59ad6bf 100644 --- a/dmc-lib/src/ast/function.rs +++ b/dmc-lib/src/ast/function.rs @@ -2,10 +2,6 @@ use crate::ast::assemble_context::AssembleContext; use crate::ast::statement::Statement; use crate::constants_table::ConstantsTable; use crate::diagnostic::Diagnostic; -use crate::ir::Ir; -use crate::ir::ir_constant::IrConstant; -use crate::ir::ir_function::IrFunction; -use crate::ir::ir_statement::IrStatement; use crate::source_range::SourceRange; use crate::symbol::FunctionSymbol; use crate::symbol_table::{SymbolInsertError, SymbolTable}; @@ -83,20 +79,6 @@ impl Function { diagnostics } - pub fn lower_to_ir(&self) -> Vec { - let mut context = FunctionLoweringContext::new(); - for statement in &self.statements { - statement.lower_to_ir(&mut context); - } - let mut irs = vec![]; - for constant in context.take_constants() { - irs.push(Ir::Constant(constant)); - } - let ir_function = IrFunction::new(&self.declared_name, context.take_statements()); - irs.push(Ir::Function(ir_function)); - irs - } - pub fn assemble( &self, context: &mut AssembleContext, @@ -111,49 +93,3 @@ impl Function { context.complete_function(); } } - -pub struct FunctionLoweringContext { - temp_variable_counter: usize, - constant_counter: usize, - constants: Vec, - statements: Vec, -} - -impl FunctionLoweringContext { - pub fn new() -> Self { - Self { - temp_variable_counter: 0, - constant_counter: 0, - constants: vec![], - statements: vec![], - } - } - - pub fn next_temp_variable(&mut self) -> String { - let temp_variable = format!("t_{}", self.temp_variable_counter); - self.temp_variable_counter += 1; - temp_variable - } - - pub fn next_constant_name(&mut self) -> String { - let constant_name = format!("%const_{}", self.constant_counter); - self.constant_counter += 1; - constant_name - } - - pub fn add_constant(&mut self, constant: IrConstant) { - self.constants.push(constant); - } - - pub fn take_constants(&mut self) -> Vec { - std::mem::take(&mut self.constants) - } - - pub fn add_statement(&mut self, statement: IrStatement) { - self.statements.push(statement); - } - - pub fn take_statements(&mut self) -> Vec { - std::mem::take(&mut self.statements) - } -} diff --git a/dmc-lib/src/ast/identifier.rs b/dmc-lib/src/ast/identifier.rs index b35e67c..37e1cb7 100644 --- a/dmc-lib/src/ast/identifier.rs +++ b/dmc-lib/src/ast/identifier.rs @@ -1,7 +1,4 @@ -use crate::ast::function::FunctionLoweringContext; use crate::diagnostic::Diagnostic; -use crate::ir::ir_expression::IrExpression; -use crate::ir::ir_variable::IrVariable; use crate::source_range::SourceRange; use crate::symbol::ExpressibleSymbol; use crate::symbol_table::SymbolTable; @@ -69,10 +66,6 @@ impl Identifier { self.expressible_symbol.as_ref().unwrap() } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) -> IrExpression { - IrExpression::Variable(IrVariable::new(self.name())) - } - pub fn source_range(&self) -> &SourceRange { &self.source_range } diff --git a/dmc-lib/src/ast/integer_literal.rs b/dmc-lib/src/ast/integer_literal.rs index 7a3ed2d..5dd1559 100644 --- a/dmc-lib/src/ast/integer_literal.rs +++ b/dmc-lib/src/ast/integer_literal.rs @@ -1,5 +1,3 @@ -use crate::ast::function::FunctionLoweringContext; -use crate::ir::ir_expression::IrExpression; use crate::source_range::SourceRange; pub struct IntegerLiteral { @@ -19,10 +17,6 @@ impl IntegerLiteral { self.value } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) -> IrExpression { - IrExpression::IntegerLiteral(self.value) - } - pub fn source_range(&self) -> &SourceRange { &self.source_range } diff --git a/dmc-lib/src/ast/let_statement.rs b/dmc-lib/src/ast/let_statement.rs index 1521a5e..9c18b3c 100644 --- a/dmc-lib/src/ast/let_statement.rs +++ b/dmc-lib/src/ast/let_statement.rs @@ -1,12 +1,8 @@ use crate::asm::asm_instruction::{AsmInstruction, LoadConstant, Move, Operand, Pop}; use crate::ast::assemble_context::AssembleContext; use crate::ast::expression::Expression; -use crate::ast::function::FunctionLoweringContext; use crate::constants_table::ConstantsTable; use crate::diagnostic::Diagnostic; -use crate::ir::ir_assign::IrAssign; -use crate::ir::ir_statement::IrStatement; -use crate::ir::ir_variable::IrVariable; use crate::source_range::SourceRange; use crate::symbol::{ExpressibleSymbol, VariableSymbol}; use crate::symbol_table::{SymbolInsertError, SymbolTable}; @@ -79,13 +75,6 @@ impl LetStatement { diagnostics } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) { - let data = self.initializer.lower_to_ir(context); - let destination = IrVariable::new(self.declared_name()); - let assign_statement = IrAssign::new(destination, data); - context.add_statement(IrStatement::Assign(assign_statement)); - } - pub fn assemble( &self, context: &mut AssembleContext, diff --git a/dmc-lib/src/ast/module_level_declaration.rs b/dmc-lib/src/ast/module_level_declaration.rs index e7da720..30f010a 100644 --- a/dmc-lib/src/ast/module_level_declaration.rs +++ b/dmc-lib/src/ast/module_level_declaration.rs @@ -50,8 +50,8 @@ impl ModuleLevelDeclaration { ModuleLevelDeclaration::Function(function) => { function.assemble(context, symbol_table, constants_table) } - ModuleLevelDeclaration::ExternFunction(extern_function) => { - extern_function.assemble(context, symbol_table, constants_table) + ModuleLevelDeclaration::ExternFunction(_) => { + // no-op } } } diff --git a/dmc-lib/src/ast/statement.rs b/dmc-lib/src/ast/statement.rs index 2196cd0..bbff3af 100644 --- a/dmc-lib/src/ast/statement.rs +++ b/dmc-lib/src/ast/statement.rs @@ -1,6 +1,5 @@ use crate::ast::assemble_context::AssembleContext; use crate::ast::expression_statement::ExpressionStatement; -use crate::ast::function::FunctionLoweringContext; use crate::ast::let_statement::LetStatement; use crate::constants_table::ConstantsTable; use crate::diagnostic::Diagnostic; @@ -39,17 +38,6 @@ impl Statement { } } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) { - match self { - Statement::Let(let_statement) => { - let_statement.lower_to_ir(context); - } - Statement::Expression(expression) => { - expression.lower_to_ir(context); - } - } - } - pub fn assemble( &self, context: &mut AssembleContext, diff --git a/dmc-lib/src/ast/string_literal.rs b/dmc-lib/src/ast/string_literal.rs index 14f12c9..c9c920f 100644 --- a/dmc-lib/src/ast/string_literal.rs +++ b/dmc-lib/src/ast/string_literal.rs @@ -1,8 +1,4 @@ -use crate::ast::function::FunctionLoweringContext; -use crate::ir::ir_constant::{IrConstant, IrStringConstant}; -use crate::ir::ir_expression::IrExpression; use crate::source_range::SourceRange; -use std::rc::Rc; pub struct StringLiteral { content: String, @@ -21,15 +17,6 @@ impl StringLiteral { &self.content } - pub fn lower_to_ir(&self, context: &mut FunctionLoweringContext) -> IrExpression { - let ir_string_constant = Rc::new(IrStringConstant::new( - self.content(), - &context.next_constant_name(), - )); - context.add_constant(IrConstant::String(ir_string_constant.clone())); - IrExpression::Constant(IrConstant::String(ir_string_constant)) - } - pub fn source_range(&self) -> &SourceRange { &self.source_range } diff --git a/dmc-lib/src/ir/assemble_context.rs b/dmc-lib/src/ir/assemble_context.rs deleted file mode 100644 index 8cbca97..0000000 --- a/dmc-lib/src/ir/assemble_context.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub struct AssembleContext {} - -impl AssembleContext { - pub fn new() -> Self { - Self {} - } -} diff --git a/dmc-lib/src/ir/ir_assign.rs b/dmc-lib/src/ir/ir_assign.rs deleted file mode 100644 index 2d33641..0000000 --- a/dmc-lib/src/ir/ir_assign.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::asm::asm_instruction::{AsmInstruction, LoadConstant, Move, Operand, Pop}; -use crate::ir::assemble_context::AssembleContext; -use crate::ir::ir_constant::IrConstant; -use crate::ir::ir_expression::IrExpression; -use crate::ir::ir_variable::IrVariable; - -#[derive(Debug)] -pub struct IrAssign { - destination: Box, - value: Box, -} - -impl IrAssign { - pub fn new(destination: IrVariable, value: IrExpression) -> Self { - Self { - destination: destination.into(), - value: value.into(), - } - } - - pub fn assemble(&self, context: &mut AssembleContext) -> Vec { - let mut instructions = vec![]; - match self.value.as_ref() { - IrExpression::Call(ir_call) => { - instructions.append(&mut ir_call.assemble(context)); - instructions.push(AsmInstruction::Pop(Pop::new(self.destination.register()))); - } - IrExpression::Constant(ir_constant) => match ir_constant { - IrConstant::String(ir_string_constant) => { - instructions.push(AsmInstruction::LoadConstant(LoadConstant::new( - ir_string_constant.name(), - self.destination.register(), - ))); - } - }, - IrExpression::IntegerLiteral(i) => { - instructions.push(AsmInstruction::Move(Move::new( - Operand::IntegerLiteral(*i), - self.destination.register(), - ))); - } - IrExpression::Variable(ir_variable) => { - instructions.push(AsmInstruction::Move(Move::new( - Operand::Register(ir_variable.register()), - self.destination.register(), - ))) - } - }; - instructions - } -} diff --git a/dmc-lib/src/ir/ir_call.rs b/dmc-lib/src/ir/ir_call.rs deleted file mode 100644 index 27bf7ef..0000000 --- a/dmc-lib/src/ir/ir_call.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::asm::asm_instruction::{ - AsmInstruction, InvokePlatformStatic, LoadConstant, Operand, Push, -}; -use crate::ir::assemble_context::AssembleContext; -use crate::ir::ir_constant::IrConstant; -use crate::ir::ir_expression::IrExpression; - -#[derive(Debug)] -pub struct IrCall { - name: String, - arguments: Vec, -} - -impl IrCall { - pub fn new(name: &str, arguments: Vec) -> Self { - Self { - name: name.into(), - arguments, - } - } - - pub fn assemble(&self, context: &mut AssembleContext) -> Vec { - let mut instructions = vec![]; - for argument in &self.arguments { - match argument { - IrExpression::Call(ir_call) => { - instructions.append(&mut ir_call.assemble(context)); - } - IrExpression::Constant(ir_constant) => { - match ir_constant { - IrConstant::String(string_constant) => { - instructions.push(AsmInstruction::LoadConstant(LoadConstant::new( - string_constant.name(), - 0, - ))); - } - } - instructions.push(AsmInstruction::Push(Push::new(Operand::Register(0)))) - } - IrExpression::IntegerLiteral(i) => { - instructions.push(AsmInstruction::Push(Push::new(Operand::IntegerLiteral(*i)))); - } - IrExpression::Variable(ir_variable) => instructions.push(AsmInstruction::Push( - Push::new(Operand::Register(ir_variable.register())), - )), - } - } - instructions.push(AsmInstruction::InvokePlatformStatic( - InvokePlatformStatic::new(&self.name, todo!()), - )); - instructions - } -} diff --git a/dmc-lib/src/ir/ir_constant.rs b/dmc-lib/src/ir/ir_constant.rs deleted file mode 100644 index 2fdb575..0000000 --- a/dmc-lib/src/ir/ir_constant.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::rc::Rc; - -#[derive(Debug)] -pub enum IrConstant { - String(Rc), -} - -#[derive(Debug)] -pub struct IrStringConstant { - value: String, - name: String, -} - -impl IrStringConstant { - pub fn new(value: &str, name: &str) -> Self { - Self { - value: value.into(), - name: name.into(), - } - } - - pub fn value(&self) -> &str { - &self.value - } - - pub fn name(&self) -> &str { - &self.name - } -} diff --git a/dmc-lib/src/ir/ir_expression.rs b/dmc-lib/src/ir/ir_expression.rs deleted file mode 100644 index eb24c62..0000000 --- a/dmc-lib/src/ir/ir_expression.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::ir::ir_call::IrCall; -use crate::ir::ir_constant::IrConstant; -use crate::ir::ir_variable::IrVariable; - -#[derive(Debug)] -pub enum IrExpression { - Call(IrCall), - Constant(IrConstant), - IntegerLiteral(i32), - Variable(IrVariable), -} diff --git a/dmc-lib/src/ir/ir_function.rs b/dmc-lib/src/ir/ir_function.rs deleted file mode 100644 index f006f20..0000000 --- a/dmc-lib/src/ir/ir_function.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::asm::asm_block::AsmBlock; -use crate::asm::asm_function::AsmFunction; -use crate::ir::assemble_context::AssembleContext; -use crate::ir::ir_statement::IrStatement; - -#[derive(Debug)] -pub struct IrFunction { - name: String, - statements: Vec, -} - -impl IrFunction { - pub fn new(name: &str, statements: Vec) -> Self { - Self { - name: name.into(), - statements, - } - } - - pub fn assemble(&self, context: &mut AssembleContext) -> AsmFunction { - let mut instructions = vec![]; - for statement in &self.statements { - instructions.append(&mut statement.assemble(context)); - } - let blocks = vec![AsmBlock::new("oops", instructions)]; - AsmFunction::new(&self.name, blocks) - } -} diff --git a/dmc-lib/src/ir/ir_statement.rs b/dmc-lib/src/ir/ir_statement.rs deleted file mode 100644 index 9976d13..0000000 --- a/dmc-lib/src/ir/ir_statement.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::asm::asm_instruction::AsmInstruction; -use crate::ir::assemble_context::AssembleContext; -use crate::ir::ir_assign::IrAssign; -use crate::ir::ir_call::IrCall; - -#[derive(Debug)] -pub enum IrStatement { - Assign(IrAssign), - Call(IrCall), -} - -impl IrStatement { - pub fn assemble(&self, context: &mut AssembleContext) -> Vec { - match self { - IrStatement::Assign(ir_assign) => ir_assign.assemble(context), - IrStatement::Call(ir_call) => ir_call.assemble(context), - } - } -} diff --git a/dmc-lib/src/ir/ir_variable.rs b/dmc-lib/src/ir/ir_variable.rs deleted file mode 100644 index b445c1d..0000000 --- a/dmc-lib/src/ir/ir_variable.rs +++ /dev/null @@ -1,22 +0,0 @@ -#[derive(Debug)] -pub struct IrVariable { - name: String, - register: Option, -} - -impl IrVariable { - pub fn new(name: &str) -> Self { - Self { - name: name.into(), - register: None, - } - } - - pub fn set_register(&mut self, register: usize) { - self.register = Some(register); - } - - pub fn register(&self) -> usize { - 0 - } -} diff --git a/dmc-lib/src/ir/mod.rs b/dmc-lib/src/ir/mod.rs deleted file mode 100644 index cc789ae..0000000 --- a/dmc-lib/src/ir/mod.rs +++ /dev/null @@ -1,17 +0,0 @@ -use crate::ir::ir_constant::IrConstant; -use crate::ir::ir_function::IrFunction; - -pub mod assemble_context; -pub mod ir_assign; -pub mod ir_call; -pub mod ir_constant; -pub mod ir_expression; -pub mod ir_function; -pub mod ir_statement; -pub mod ir_variable; - -#[derive(Debug)] -pub enum Ir { - Function(IrFunction), - Constant(IrConstant), -} diff --git a/dmc-lib/src/lib.rs b/dmc-lib/src/lib.rs index 5578421..2b90830 100644 --- a/dmc-lib/src/lib.rs +++ b/dmc-lib/src/lib.rs @@ -2,7 +2,6 @@ pub mod asm; pub mod ast; pub mod constants_table; pub mod diagnostic; -pub mod ir; pub mod lexer; pub mod parser; pub mod scope;