Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jul 31, 2024
1 parent 9025401 commit 0b33555
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/root/builtin/core/referencing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::root::compiler::assembly::utils::{copy_from_indirect, copy_from_indirect_fixed_offset};
use crate::root::errors::evaluable_errors::EvalErrs::{ExpectedDifferentType, OpWrongReturnType};
use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse::Location;
Expand All @@ -18,7 +18,7 @@ pub fn set_reference(
.with_indirection(to_ref.type_ref().indirection().0 + 1);
if new_type != *into.type_ref() {
return WErr::ne(
OpWrongReturnType(
EvalErrs::OpWrongReturnType(
global_table.get_type_name(into.type_ref()),
global_table.get_type_name(&new_type),
),
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn set_deref(
let expected = into.type_ref().plus_one_indirect();
if to_deref.type_ref() != &expected {
return WErr::ne(
ExpectedDifferentType(
EvalErrs::ExpectedDifferentType(
global_table.get_type_name(&expected),
global_table.get_type_name(to_deref.type_ref()),
),
Expand Down
6 changes: 3 additions & 3 deletions src/root/builtin/types/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::root::builtin::types::int::printi::PrintI;
use crate::root::builtin::types::int::sub::IntSub;
use crate::root::builtin::{f_id, t_id, BuiltinInlineFunction, InlineFunctionGenerator};
use crate::root::compiler::assembly::utils::write_64bit_int;
use crate::root::compiler::compiler_errors::CErrs;
use crate::root::compiler::compiler_errors::CompErrs;
use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
Expand Down Expand Up @@ -102,13 +102,13 @@ impl Type for IntType {
LiteralTokens::Int(value) => {
if *value > i64::MAX as i128 {
return WErr::ne(
CErrs::IntLiteralExceedsMax(*value, i64::MAX as i128),
CompErrs::IntLiteralExceedsMax(*value, i64::MAX as i128),
literal.location().clone(),
);
}
if *value < i64::MIN as i128 {
return WErr::ne(
CErrs::IntLiteralBelowMin(*value, i64::MAX as i128),
CompErrs::IntLiteralBelowMin(*value, i64::MAX as i128),
literal.location().clone(),
);
}
Expand Down
15 changes: 7 additions & 8 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::root::assembler::assembly_builder::AssemblyBuilder;
use crate::root::builtin::types::bool::BoolType;
use crate::root::builtin::types::int::IntType;
use crate::root::compiler::compiler_errors::CErrs::{
CannotBreak, ExpectedNoReturn, ExpectedReturn, ExpectedSomeReturn,
};
use crate::root::compiler::evaluation::into::compile_evaluable_into;
use crate::root::compiler::evaluation::reference::compile_evaluable_reference;
use crate::root::compiler::global_tracker::GlobalTracker;
Expand All @@ -16,6 +13,8 @@ use crate::root::shared::common::AddressedTypeRef;
use crate::root::shared::common::{FunctionID, Indirection, LocalAddress, TypeRef};
use crate::root::utils::warn;
use color_print::cprintln;
use crate::root::compiler::compiler_errors::CompErrs;
use crate::root::errors::evaluable_errors::EvalErrs;

/// Compiles a given function into assembly
pub fn compile_function(
Expand Down Expand Up @@ -73,7 +72,7 @@ pub fn compile_function(
.map(|x| x.type_ref().clone())
.unwrap_or_else(|| IntType::id().immediate());
return WErr::ne(
ExpectedReturn(global_table.get_type_name(&type_ref)),
CompErrs::ExpectedReturn(global_table.get_type_name(&type_ref)),
end_location,
);
}
Expand Down Expand Up @@ -258,7 +257,7 @@ fn recursively_compile_lines(
if fid.is_main() {
if rt.return_value().is_none() {
return WErr::ne(
ExpectedSomeReturn(
CompErrs::ExpectedSomeReturn(
global_table.get_type_name(&IntType::id().immediate()),
),
rt.location().clone(),
Expand All @@ -280,7 +279,7 @@ fn recursively_compile_lines(
contents.line(&format!("mov rax, qword {}", address.local_address()));
} else if let Some(return_value) = rt.return_value() {
if return_variable.is_none() {
return WErr::ne(ExpectedNoReturn, return_value.location().clone());
return WErr::ne(CompErrs::ExpectedNoReturn, return_value.location().clone());
}

contents.other(&compile_evaluable_into(
Expand All @@ -293,7 +292,7 @@ fn recursively_compile_lines(
)?);
} else if return_variable.is_some() {
return WErr::ne(
ExpectedSomeReturn(
CompErrs::ExpectedSomeReturn(
global_table
.get_type_name(return_variable.as_ref().unwrap().type_ref()),
),
Expand All @@ -313,7 +312,7 @@ fn recursively_compile_lines(
if let Some(break_tag) = break_tag {
contents.line(&format!("jmp {break_tag}"));
} else {
return WErr::ne(CannotBreak, bt.location().clone());
return WErr::ne(CompErrs::CannotBreak, bt.location().clone());
}
}
LineTokens::NoOp(et) => {
Expand Down
16 changes: 8 additions & 8 deletions src/root/compiler/compile_function_call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::root::assembler::assembly_builder::AssemblyBuilder;
use crate::root::compiler::assembly::utils::{align_16_bytes, align_16_bytes_plus_8, copy};
use crate::root::compiler::compiler_errors::CErrs::{
BadFunctionArgCount, BadFunctionReturn, ExpectedFunctionReturn, ExpectedSomeReturn,
};
use crate::root::compiler::evaluation::coerce_self::coerce_self;
use crate::root::compiler::evaluation::into::compile_evaluable_into;
use crate::root::compiler::evaluation::reference::compile_evaluable_reference;
Expand All @@ -16,6 +13,9 @@ use crate::root::parser::parse_parameters::SelfType;
use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID};
use either::Either;
use itertools::Itertools;
use crate::root::compiler::compiler_errors::CompErrs;
use crate::root::errors::evaluable_errors::EvalErrs;

// TODO: Cleanup code
/// Calls a given function with arguments
pub fn call_function(
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn call_function(
if let Some(return_address) = return_address {
if return_address.type_ref() != &expected_return {
return WErr::ne(
BadFunctionReturn(
EvalErrs::BadFunctionReturn(
global_table.get_type_name(return_address.type_ref()),
global_table.get_type_name(&expected_return),
),
Expand All @@ -66,7 +66,7 @@ pub fn call_function(
} else {
if let Some(return_address) = return_address {
return WErr::ne(
ExpectedSomeReturn(global_table.get_type_name(return_address.type_ref())),
CompErrs::ExpectedSomeReturn(global_table.get_type_name(return_address.type_ref())),
location.clone(),
);
}
Expand All @@ -83,7 +83,7 @@ pub fn call_function(

if signature_args.len() != arguments.len() {
return WErr::ne(
BadFunctionArgCount(name.to_string(), signature_args.len(), arguments.len()),
EvalErrs::BadFunctionArgCount(name.to_string(), signature_args.len(), arguments.len()),
location.clone(),
);
}
Expand Down Expand Up @@ -177,7 +177,7 @@ pub fn call_function(

if signature_args.len() != arguments.len() {
return WErr::ne(
BadFunctionArgCount(name.to_string(), signature_args.len(), arguments.len()),
EvalErrs::BadFunctionArgCount(name.to_string(), signature_args.len(), arguments.len()),
location.clone(),
);
}
Expand Down Expand Up @@ -301,7 +301,7 @@ pub fn call_function(
let return_addr = if let Some(return_address) = return_address {
if return_addr.is_none() {
return WErr::ne(
ExpectedFunctionReturn(global_table.get_type_name(return_address.type_ref())),
EvalErrs::ExpectedFunctionReturn(global_table.get_type_name(return_address.type_ref())),
location.clone(),
);
}
Expand Down
12 changes: 1 addition & 11 deletions src/root/compiler/compiler_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use thiserror::Error;

/// An error in the compiler step, excluding errors covered in `EvalErrs`
#[derive(Error, Debug)]
pub enum CErrs {
pub enum CompErrs {
#[error("Int literal ({0}) exceeds maximum value ({1}) for type")]
IntLiteralExceedsMax(i128, i128),
#[error("Int literal ({0}) below minimum value ({1}) for type")]
Expand All @@ -17,14 +17,4 @@ pub enum CErrs {
ExpectedNoReturn,
#[error("Cannot break - not in a loop")]
CannotBreak,
#[error("Expected type ({0}) but function returns ({1})")]
BadFunctionReturn(String, String),
#[error("Expected type ({0}) but function doesn't return a value")]
ExpectedFunctionReturn(String),
#[error("Function ({0}) expects ({1}) arguments but found ({2})")]
BadFunctionArgCount(String, usize, usize),
#[error("Type ({0}) does not have attributes")]
TypeDoesntHaveAttributes(String),
#[error("Type ({0}) cannot be initialised")]
TypeCannotBeInitialised(String)
}
23 changes: 10 additions & 13 deletions src/root/compiler/evaluation/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use crate::root::compiler::evaluation::{function_only, reference, type_only};
use crate::root::compiler::global_tracker::GlobalTracker;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::errors::evaluable_errors::EvalErrs::{
ExpectedDifferentType, ExpectedNotNone, ExpectedType, OpNoReturn, OpWrongReturnType,
};
use crate::root::errors::name_resolver_errors::NRErrs;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResult};
Expand All @@ -24,7 +21,7 @@ use crate::root::shared::types::Type;
use either::{Left, Right};
use itertools::Itertools;
use std::any::Any;
use crate::root::compiler::compiler_errors::CErrs;
use crate::root::compiler::compiler_errors::CompErrs;

/// Evaluates `et` putting the result into `target`
pub fn compile_evaluable_into(
Expand Down Expand Up @@ -55,7 +52,7 @@ pub fn compile_evaluable_into(
NameResult::Variable(address) => {
if address.type_ref() != target.type_ref() {
return WErr::ne(
ExpectedDifferentType(
EvalErrs::ExpectedDifferentType(
global_table.get_type_name(target.type_ref()),
global_table.get_type_name(address.type_ref()),
),
Expand Down Expand Up @@ -85,7 +82,7 @@ pub fn compile_evaluable_into(
match op.operator() {
OperatorTokens::Assign => {
return WErr::ne(
ExpectedType(global_table.get_type_name(target.type_ref())),
EvalErrs::ExpectedType(global_table.get_type_name(target.type_ref())),
et.location().clone(),
);
}
Expand Down Expand Up @@ -127,14 +124,14 @@ pub fn compile_evaluable_into(
match signature.return_type() {
None => {
return WErr::ne(
OpNoReturn(global_table.get_type_name(target.type_ref())),
EvalErrs::OpNoReturn(global_table.get_type_name(target.type_ref())),
op.location().clone(),
)
}
Some(rt) => {
if rt != target.type_ref() {
return WErr::ne(
OpWrongReturnType(
EvalErrs::OpWrongReturnType(
global_table.get_type_name(target.type_ref()),
global_table.get_type_name(rt),
),
Expand Down Expand Up @@ -180,7 +177,7 @@ pub fn compile_evaluable_into(
global_tracker,
)?;
let Some(val) = val else {
return WErr::ne(ExpectedNotNone, lhs.location().clone());
return WErr::ne(EvalErrs::ExpectedNotNone, lhs.location().clone());
};

if *val.type_ref() != lhs_type {
Expand All @@ -198,7 +195,7 @@ pub fn compile_evaluable_into(
global_tracker,
)?;
let Some(val) = val else {
return WErr::ne(ExpectedNotNone, lhs.location().clone());
return WErr::ne(EvalErrs::ExpectedNotNone, lhs.location().clone());
};

c += &set_deref(lhs.location(), val, target, global_table)?;
Expand Down Expand Up @@ -234,14 +231,14 @@ pub fn compile_evaluable_into(
match signature.return_type() {
None => {
return WErr::ne(
OpNoReturn(global_table.get_type_name(target.type_ref())),
EvalErrs::OpNoReturn(global_table.get_type_name(target.type_ref())),
op.location().clone(),
)
}
Some(rt) => {
if rt != target.type_ref() {
return WErr::ne(
OpWrongReturnType(
EvalErrs::OpWrongReturnType(
global_table.get_type_name(target.type_ref()),
global_table.get_type_name(rt),
),
Expand Down Expand Up @@ -414,7 +411,7 @@ pub fn compile_evaluable_into(

let tt = global_table.get_type(t.type_id().clone());
let attributes = tt.get_attributes(struct_init.location())
.map_err(|_| WErr::n(CErrs::TypeCannotBeInitialised(tt.name().to_string()), struct_init.location().clone()))?
.map_err(|_| WErr::n(EvalErrs::TypeCannotBeInitialised(tt.name().to_string()), struct_init.location().clone()))?
.iter().map(|x| x.clone()).collect_vec();
let give_attrs = struct_init.contents();

Expand Down
15 changes: 7 additions & 8 deletions src/root/compiler/evaluation/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use crate::root::builtin::core::referencing::{set_deref, set_reference};
use crate::root::compiler::assembly::heap::heap_alloc;
use crate::root::compiler::assembly::utils::{copy, copy_to_indirect};
use crate::root::compiler::compile_function_call::call_function;
use crate::root::compiler::compiler_errors::CErrs;
use crate::root::compiler::compiler_errors::CompErrs;
use crate::root::compiler::evaluation::reference::compile_evaluable_reference;
use crate::root::compiler::evaluation::{function_only, into, reference, type_only};
use crate::root::compiler::global_tracker::GlobalTracker;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::errors::evaluable_errors::EvalErrs::{ExpectedNotNone, ExpectedReference};
use crate::root::errors::name_resolver_errors::NRErrs;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResult};
Expand Down Expand Up @@ -90,11 +89,11 @@ pub fn compile_evaluable_new(
)?;

let Some(into) = into else {
return WErr::ne(ExpectedNotNone, lhs.location().clone());
return WErr::ne(EvalErrs::ExpectedNotNone, lhs.location().clone());
};
if !into.type_ref().indirection().has_indirection() {
return WErr::ne(
ExpectedReference(global_table.get_type_name(into.type_ref())),
EvalErrs::ExpectedReference(global_table.get_type_name(into.type_ref())),
lhs.location().clone(),
);
}
Expand Down Expand Up @@ -197,7 +196,7 @@ pub fn compile_evaluable_new(
global_tracker,
)?;
let Some(val) = val else {
return WErr::ne(ExpectedNotNone, lhs.location().clone());
return WErr::ne(EvalErrs::ExpectedNotNone, lhs.location().clone());
};

if *val.type_ref() != lhs_type {
Expand All @@ -219,11 +218,11 @@ pub fn compile_evaluable_new(
global_tracker,
)?;
let Some(val) = val else {
return WErr::ne(ExpectedNotNone, lhs.location().clone());
return WErr::ne(EvalErrs::ExpectedNotNone, lhs.location().clone());
};
if !val.type_ref().indirection().has_indirection() {
return WErr::ne(
ExpectedReference(global_table.get_type_name(val.type_ref())),
EvalErrs::ExpectedReference(global_table.get_type_name(val.type_ref())),
lhs.location().clone(),
);
}
Expand Down Expand Up @@ -426,7 +425,7 @@ pub fn compile_evaluable_new(

let tt = global_table.get_type(t.type_id().clone());
let attributes = tt.get_attributes(struct_init.location())
.map_err(|_| WErr::n(CErrs::TypeCannotBeInitialised(tt.name().to_string()), struct_init.location().clone()))?
.map_err(|_| WErr::n(EvalErrs::TypeCannotBeInitialised(tt.name().to_string()), struct_init.location().clone()))?
.iter().map(|x| x.clone()).collect_vec();
let give_attrs = struct_init.contents();

Expand Down
10 changes: 10 additions & 0 deletions src/root/errors/evaluable_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ pub enum EvalErrs {
ExpectedFunctionName,
#[error("Expected a reference type but found ({0})")]
ExpectedReference(String),
#[error("Expected type ({0}) but function returns ({1})")]
BadFunctionReturn(String, String),
#[error("Expected type ({0}) but function doesn't return a value")]
ExpectedFunctionReturn(String),
#[error("Function ({0}) expects ({1}) arguments but found ({2})")]
BadFunctionArgCount(String, usize, usize),
#[error("Type ({0}) does not have attributes")]
TypeDoesntHaveAttributes(String),
#[error("Type ({0}) cannot be initialised")]
TypeCannotBeInitialised(String)
}

// return Err(WErr::n(OpWrongReturnType(global_table.get_type_name(into.type_ref()), global_table.get_type_name(&new_type)), location.clone()));
5 changes: 3 additions & 2 deletions src/root/shared/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::root::compiler::compiler_errors::CErrs;
use crate::root::compiler::compiler_errors::CompErrs;
use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::errors::WErr;
use crate::root::parser::parse::Location;
use crate::root::parser::parse_function::parse_literal::LiteralToken;
Expand All @@ -14,7 +15,7 @@ pub trait Type {
fn name(&self) -> &str;

fn get_attributes(&self, location: &Location) -> Result<&[(ByteSize, SimpleNameToken, TypeRef)], WErr> {
WErr::ne(CErrs::TypeDoesntHaveAttributes(self.name().to_string()), location.clone())
WErr::ne(EvalErrs::TypeDoesntHaveAttributes(self.name().to_string()), location.clone())
}

fn instantiate_from_literal(
Expand Down

0 comments on commit 0b33555

Please sign in to comment.