Code cleanup

This commit is contained in:
Mia
2026-03-06 15:28:30 +01:00
parent bd0b619127
commit 92c02c6ca2
9 changed files with 63 additions and 68 deletions
+22 -25
View File
@@ -5,7 +5,7 @@ use leaf_assembly::{
Function,
ir::{Cmp, FunctionBodyBuilder},
},
types::{IntT, Type, derivations::PtrT},
types::{Type, derivations::PtrT},
values::{AnyConst, Int, Value, ValueFlags},
};
use leaf_parser::{
@@ -118,7 +118,7 @@ impl<'l> Scope<'l> {
.map(|f| Value::Constant(AnyConst::Function(f)))
.map_err(|err| CompilationError {
kind: Kind::FunctionCompilationFailed,
message: format!("Could not compile function."),
message: "Could not compile function.".to_string(),
location: Location::Range {
file: self.source.clone(),
range: func.text.range(),
@@ -135,7 +135,7 @@ impl<'l> Scope<'l> {
match n.text.split_at_checked(2) {
Some(("0b", value)) => <$ty>::from_str_radix(value, 2),
Some(("0x", value)) => <$ty>::from_str_radix(value, 16),
_ => <$ty>::from_str_radix(&n.text, 10),
_ => n.text.parse::<$ty>(),
}
.map(|v| Value::Constant(AnyConst::Int(Int::$id(v))))
.map_err(|_| CompilationError {
@@ -163,17 +163,15 @@ impl<'l> Scope<'l> {
Some("u64") => parse_number!(u64, U64),
Some("u128") => parse_number!(u128, U128),
Some("usize") => parse_number!(u128, USize),
Some(ty) => {
return Err(CompilationError {
kind: Kind::InvalidIntegerType,
message: format!("`{ty}` is not a valid integer type."),
location: Location::Range {
file: self.source.clone(),
range: n.text.range(),
},
cause: None,
});
}
Some(ty) => Err(CompilationError {
kind: Kind::InvalidIntegerType,
message: format!("`{ty}` is not a valid integer type."),
location: Location::Range {
file: self.source.clone(),
range: n.text.range(),
},
cause: None,
}),
}
}
@@ -247,14 +245,14 @@ impl<'l> Scope<'l> {
let Value::Constant(AnyConst::Type(dst_ty)) = rhs else {
return Err(CompilationError {
kind: Kind::NotAType,
message: format!("Cannot perform cast."),
message: "Cannot perform cast.".to_string(),
location: Location::Range {
file: self.source.clone(),
range: expr.range(),
},
cause: Some(Box::new(CompilationError {
kind: Kind::NotAType,
message: format!("Cast target is not a type."),
message: "Cast target is not a type.".to_string(),
location: Location::Range {
file: self.source.clone(),
range: rhs_expr.range(),
@@ -266,7 +264,7 @@ impl<'l> Scope<'l> {
match (src_ty, dst_ty) {
(Type::Int(src_ty), Type::Int(dst_ty)) => {
if dst_ty.precision < src_ty.precision {
return Ok(builder.trunc(lhs, dst_ty).unwrap().into());
return Ok(builder.trunc(lhs, dst_ty).unwrap());
}
todo!("{src_ty} as {dst_ty}");
}
@@ -373,8 +371,7 @@ impl<'l> Scope<'l> {
unsafe {
Ok(builder
.reinterpret(Value::Instruction(inst), ptr, flags)
.unwrap()
.into())
.unwrap())
}
}
v => todo!("{v:?}"),
@@ -509,10 +506,10 @@ impl<'l> Scope<'l> {
match func.ty.ret_t {
Type::Void => builder.ret(None).unwrap(),
_ => {
if let Some(expr) = last_expr.as_mut() {
if expr.flags().contains(ValueFlags::LValue) {
*expr = builder.load(*expr).unwrap();
}
if let Some(expr) = last_expr.as_mut()
&& expr.flags().contains(ValueFlags::LValue)
{
*expr = builder.load(*expr).unwrap();
}
builder.ret(last_expr).unwrap()
}
@@ -533,7 +530,7 @@ impl<'l> Scope<'l> {
) -> Result<Value<'l>, CompilationError> {
let mut sub_ctx = ExpressionContext {
decl_names: Some(names),
builder: ctx.builder.as_mut().map(|v| &mut **v),
builder: ctx.builder.as_deref_mut(),
};
let mut value = self.compile_expression(value, &mut sub_ctx)?;
if mutable {
@@ -558,7 +555,7 @@ impl<'l> Scope<'l> {
Value::Constant(AnyConst::Type(ty)) => Ok(ty),
_ => Err(CompilationError {
kind: Kind::NotAType,
message: format!("Value is not a type."),
message: "Value is not a type.".to_string(),
location: Location::None,
cause: None,
}),