-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored internal expression conforming code to be generic over whe…
…ther the operation should be performed or not
- Loading branch information
1 parent
ecadbd6
commit 7ba13f9
Showing
14 changed files
with
330 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
Oops, something went wrong.