Remove ir structs and fix misc. warnings.

This commit is contained in:
Jesse Brault 2026-03-02 20:15:36 -06:00
parent 68cb200494
commit 8c1d56dc1a
22 changed files with 6 additions and 414 deletions

View File

@ -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<Value, Box<dyn Error>> {
let maybe_to_print = args.get(0);

View File

@ -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,

View File

@ -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),
}
}
}

View File

@ -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,

View File

@ -80,22 +80,13 @@ impl ExternFunction {
diagnostics
}
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> {
pub fn check_name_usages(&mut self, _symbol_table: &SymbolTable) -> Vec<Diagnostic> {
// no-op (for now)
vec![]
}
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> {
pub fn type_check(&mut self, _symbol_table: &SymbolTable) -> Vec<Diagnostic> {
// no-op (for now)
vec![]
}
pub fn assemble(
&self,
context: &mut AssembleContext,
symbol_table: &SymbolTable,
constants_table: &mut ConstantsTable,
) {
// no-op
}
}

View File

@ -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<Ir> {
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<IrConstant>,
statements: Vec<IrStatement>,
}
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<IrConstant> {
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<IrStatement> {
std::mem::take(&mut self.statements)
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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,

View File

@ -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
}
}
}

View File

@ -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,

View File

@ -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
}

View File

@ -1,7 +0,0 @@
pub struct AssembleContext {}
impl AssembleContext {
pub fn new() -> Self {
Self {}
}
}

View File

@ -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<IrVariable>,
value: Box<IrExpression>,
}
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<AsmInstruction> {
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
}
}

View File

@ -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<IrExpression>,
}
impl IrCall {
pub fn new(name: &str, arguments: Vec<IrExpression>) -> Self {
Self {
name: name.into(),
arguments,
}
}
pub fn assemble(&self, context: &mut AssembleContext) -> Vec<AsmInstruction> {
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
}
}

View File

@ -1,29 +0,0 @@
use std::rc::Rc;
#[derive(Debug)]
pub enum IrConstant {
String(Rc<IrStringConstant>),
}
#[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
}
}

View File

@ -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),
}

View File

@ -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<IrStatement>,
}
impl IrFunction {
pub fn new(name: &str, statements: Vec<IrStatement>) -> 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)
}
}

View File

@ -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<AsmInstruction> {
match self {
IrStatement::Assign(ir_assign) => ir_assign.assemble(context),
IrStatement::Call(ir_call) => ir_call.assemble(context),
}
}
}

View File

@ -1,22 +0,0 @@
#[derive(Debug)]
pub struct IrVariable {
name: String,
register: Option<usize>,
}
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
}
}

View File

@ -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),
}

View File

@ -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;