Skip to content

Commit

Permalink
Refactored internal expression conforming code to be generic over whe…
Browse files Browse the repository at this point in the history
…ther the operation should be performed or not
  • Loading branch information
IsaacShelton committed Sep 28, 2024
1 parent ecadbd6 commit 7ba13f9
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 229 deletions.
86 changes: 46 additions & 40 deletions src/resolve/conform/from_c_integer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::ConformMode;
use super::{ConformMode, Objective, ObjectiveResult};
use crate::{
ast::{CInteger, IntegerBits, OptionIntegerSignExt},
logic::implies,
Expand All @@ -9,17 +9,17 @@ use crate::{
source_files::Source,
};

pub fn from_c_integer(
pub fn from_c_integer<O: Objective>(
expr: &TypedExpr,
mode: ConformMode,
from_c_integer: CInteger,
from_sign: Option<IntegerSign>,
to_type: &Type,
source: Source,
) -> Option<TypedExpr> {
) -> ObjectiveResult<O> {
match &to_type.kind {
TypeKind::Boolean => from_c_integer_to_bool(expr, mode, source),
TypeKind::Integer(to_bits, to_sign) => from_c_integer_to_integer(
TypeKind::Boolean => from_c_integer_to_bool::<O>(expr, mode, source),
TypeKind::Integer(to_bits, to_sign) => from_c_integer_to_integer::<O>(
expr,
mode,
from_c_integer,
Expand All @@ -28,7 +28,7 @@ pub fn from_c_integer(
*to_sign,
source,
),
TypeKind::CInteger(to_c_integer, to_sign) => from_c_integer_to_c_integer(
TypeKind::CInteger(to_c_integer, to_sign) => from_c_integer_to_c_integer::<O>(
expr,
mode,
from_c_integer,
Expand All @@ -37,38 +37,40 @@ pub fn from_c_integer(
*to_sign,
source,
),
_ => None,
_ => O::fail(),
}
}

fn from_c_integer_to_bool(
fn from_c_integer_to_bool<O: Objective>(
expr: &TypedExpr,
mode: ConformMode,
source: Source,
) -> Option<TypedExpr> {
) -> ObjectiveResult<O> {
if !mode.allow_lossy_integer() {
return None;
return O::fail();
}

Some(TypedExpr::new(
TypeKind::Boolean.at(source),
ExprKind::UnaryMathOperation(Box::new(UnaryMathOperation {
operator: UnaryMathOperator::IsNonZero,
inner: expr.clone(),
}))
.at(source),
))
O::success(|| {
TypedExpr::new(
TypeKind::Boolean.at(source),
ExprKind::UnaryMathOperation(Box::new(UnaryMathOperation {
operator: UnaryMathOperator::IsNonZero,
inner: expr.clone(),
}))
.at(source),
)
})
}

pub fn from_c_integer_to_c_integer(
pub fn from_c_integer_to_c_integer<O: Objective>(
expr: &TypedExpr,
mode: ConformMode,
from_c_integer: CInteger,
from_sign: Option<IntegerSign>,
to_c_integer: CInteger,
to_sign: Option<IntegerSign>,
source: Source,
) -> Option<TypedExpr> {
) -> ObjectiveResult<O> {
let target_type = TypeKind::CInteger(to_c_integer, to_sign).at(source);

let is_smaller_likeness = from_sign == to_sign && from_c_integer <= to_c_integer;
Expand All @@ -79,40 +81,44 @@ pub fn from_c_integer_to_c_integer(
let is_lossless = is_smaller_likeness || is_smaller_and_can_preserve_sign;

if mode.allow_lossy_integer() || is_lossless {
return Some(TypedExpr::new(
target_type.clone(),
ExprKind::IntegerCast(Box::new(CastFrom {
cast: Cast::new(target_type, expr.expr.clone()),
from_type: TypeKind::CInteger(from_c_integer, from_sign).at(source),
}))
.at(source),
));
return O::success(|| {
TypedExpr::new(
target_type.clone(),
ExprKind::IntegerCast(Box::new(CastFrom {
cast: Cast::new(target_type, expr.expr.clone()),
from_type: TypeKind::CInteger(from_c_integer, from_sign).at(source),
}))
.at(source),
)
});
}

None
O::fail()
}

fn from_c_integer_to_integer(
fn from_c_integer_to_integer<O: Objective>(
expr: &TypedExpr,
mode: ConformMode,
from_c_integer: CInteger,
from_sign: Option<IntegerSign>,
to_bits: IntegerBits,
to_sign: IntegerSign,
source: Source,
) -> Option<TypedExpr> {
) -> ObjectiveResult<O> {
if !mode.allow_lossy_integer() {
return None;
return O::fail();
}

let target_type = TypeKind::Integer(to_bits, to_sign).at(source);

Some(TypedExpr::new(
target_type.clone(),
ExprKind::IntegerCast(Box::new(CastFrom {
cast: Cast::new(target_type, expr.expr.clone()),
from_type: TypeKind::CInteger(from_c_integer, from_sign).at(source),
}))
.at(source),
))
O::success(|| {
TypedExpr::new(
target_type.clone(),
ExprKind::IntegerCast(Box::new(CastFrom {
cast: Cast::new(target_type, expr.expr.clone()),
from_type: TypeKind::CInteger(from_c_integer, from_sign).at(source),
}))
.at(source),
)
})
}
30 changes: 19 additions & 11 deletions src/resolve/conform/from_float.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
use super::{Objective, ObjectiveResult};
use crate::{
ast::FloatSize,
resolved::{Cast, Expr, ExprKind, Type, TypeKind, TypedExpr},
source_files::Source,
};

pub fn from_float(expr: &TypedExpr, from_size: FloatSize, to_type: &Type) -> Option<TypedExpr> {
pub fn from_float<O: Objective>(
expr: &TypedExpr,
from_size: FloatSize,
to_type: &Type,
) -> ObjectiveResult<O> {
match &to_type.kind {
TypeKind::Floating(to_size) => {
from_float_to_float(&expr.expr, from_size, *to_size, to_type.source)
from_float_to_float::<O>(&expr.expr, from_size, *to_size, to_type.source)
}
_ => None,
_ => O::fail(),
}
}

fn from_float_to_float(
fn from_float_to_float<O: Objective>(
expr: &Expr,
from_size: FloatSize,
to_size: FloatSize,
type_source: Source,
) -> Option<TypedExpr> {
) -> ObjectiveResult<O> {
let target_type = TypeKind::Floating(to_size).at(type_source);
let from_bits = from_size.bits();
let to_bits = to_size.bits();

if from_bits == to_bits {
return Some(TypedExpr::new(target_type, expr.clone()));
return O::success(|| TypedExpr::new(target_type, expr.clone()));
}

if from_bits < to_bits {
return Some(TypedExpr::new(
target_type.clone(),
ExprKind::FloatExtend(Box::new(Cast::new(target_type, expr.clone()))).at(expr.source),
));
return O::success(|| {
TypedExpr::new(
target_type.clone(),
ExprKind::FloatExtend(Box::new(Cast::new(target_type, expr.clone())))
.at(expr.source),
)
});
}

None
O::fail()
}
19 changes: 13 additions & 6 deletions src/resolve/conform/from_float_literal.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use super::{Objective, ObjectiveResult};
use crate::{
resolved::{Expr, ExprKind, Type, TypeKind, TypedExpr},
source_files::Source,
};

pub fn from_float_literal(from: f64, to_type: &Type, source: Source) -> Option<TypedExpr> {
pub fn from_float_literal<O: Objective>(
from: f64,
to_type: &Type,
source: Source,
) -> ObjectiveResult<O> {
match &to_type.kind {
TypeKind::Floating(to_size) => Some(TypedExpr::new(
TypeKind::Floating(*to_size).at(to_type.source),
Expr::new(ExprKind::FloatingLiteral(*to_size, from), source),
)),
_ => None,
TypeKind::Floating(to_size) => O::success(|| {
TypedExpr::new(
TypeKind::Floating(*to_size).at(to_type.source),
Expr::new(ExprKind::FloatingLiteral(*to_size, from), source),
)
}),
_ => O::fail(),
}
}
Loading

0 comments on commit 7ba13f9

Please sign in to comment.