Add Num.withoutDecimalPoint

This commit is contained in:
Fabian Schmalzried 2024-03-14 23:59:06 +01:00
parent 62cc19c64b
commit 9c664172dd
No known key found for this signature in database
GPG key ID: D691D5DA4CEF42E7
9 changed files with 25 additions and 4 deletions

View file

@ -150,6 +150,8 @@ interface Num
toF32Checked,
toF64,
toF64Checked,
withoutDecimalPoint,
# withDecimalPoint
]
imports [
Bool.{ Bool },
@ -1406,3 +1408,10 @@ toU64Checked : Int * -> Result U64 [OutOfBounds]
toU128Checked : Int * -> Result U128 [OutOfBounds]
toF32Checked : Num * -> Result F32 [OutOfBounds]
toF64Checked : Num * -> Result F64 [OutOfBounds]
## Turns a [Dec] into its [I128] representation, effectifely removing the decimal point.
withoutDecimalPoint : Dec -> I128
## Turns a [I128] into the coresponding [Dec], effectifely addoing the decimal point.
#withDecimalPoint : I128 -> Dec

View file

@ -205,6 +205,7 @@ map_symbol_to_lowlevel_and_arity! {
NumCountTrailingZeroBits; NUM_COUNT_TRAILING_ZERO_BITS; 1,
NumCountOneBits; NUM_COUNT_ONE_BITS; 1,
I128OfDec; I128_OF_DEC; 1,
DecWithoutDecimalPoint; NUM_DEC_WITHOUT_DECIMAL_POINT; 1,
Eq; BOOL_STRUCTURAL_EQ; 2,
NotEq; BOOL_STRUCTURAL_NOT_EQ; 2,

View file

@ -2029,6 +2029,11 @@ trait Backend<'a> {
}
}
LowLevel::DecWithoutDecimalPoint => {
let intrinsic = bitcode::DEC_TO_I128.to_string();
self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout)
}
x => todo!("low level, {:?}", x),
}
}

View file

@ -1172,7 +1172,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
// which could be useful to look at when implementing this.
todo!("implement checked float conversion");
}
I128OfDec => {
I128OfDec | DecWithoutDecimalPoint => {
arguments!(dec);
dec_unary_op(env, bitcode::DEC_TO_I128, dec)
}

View file

@ -2017,7 +2017,9 @@ impl<'a> LowLevelCall<'a> {
NumToFloatChecked => {
todo!("implement toF32Checked and toF64Checked");
}
I128OfDec => self.load_args_and_call_zig(backend, bitcode::DEC_TO_I128),
I128OfDec | DecWithoutDecimalPoint => {
self.load_args_and_call_zig(backend, bitcode::DEC_TO_I128)
}
And => {
self.load_args(backend);
backend.code_builder.i32_and();

View file

@ -103,6 +103,7 @@ pub enum LowLevel {
NumCountTrailingZeroBits,
NumCountOneBits,
I128OfDec,
DecWithoutDecimalPoint,
Eq,
NotEq,
And,
@ -337,6 +338,7 @@ map_symbol_to_lowlevel! {
NumCountTrailingZeroBits <= NUM_COUNT_TRAILING_ZERO_BITS;
NumCountOneBits <= NUM_COUNT_ONE_BITS;
I128OfDec <= I128_OF_DEC;
DecWithoutDecimalPoint <= I128_OF_DEC;
Eq <= BOOL_STRUCTURAL_EQ;
NotEq <= BOOL_STRUCTURAL_NOT_EQ;
And <= BOOL_AND;

View file

@ -1268,6 +1268,7 @@ define_builtins! {
156 NUM_BYTES_TO_U128_LOWLEVEL: "bytesToU128Lowlevel"
157 NUM_DIV_TRUNC_UNCHECKED: "divTruncUnchecked" // traps on division by zero
158 NUM_REM_UNCHECKED: "remUnchecked" // traps on division by zero
159 NUM_DEC_WITHOUT_DECIMAL_POINT: "withoutDecimalPoint"
}
4 BOOL: "Bool" => {
0 BOOL_BOOL: "Bool" exposed_type=true // the Bool.Bool type alias

View file

@ -1593,7 +1593,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC {
| NumCountLeadingZeroBits
| NumCountTrailingZeroBits
| NumCountOneBits => RC::NoRc,
I128OfDec => RC::NoRc,
I128OfDec | DecWithoutDecimalPoint => RC::NoRc,
DictPseudoSeed => RC::NoRc,
StrStartsWith | StrEndsWith => RC::NoRc,
StrFromUtf8 => RC::Rc,

View file

@ -1345,7 +1345,8 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] {
| NumCountLeadingZeroBits
| NumCountTrailingZeroBits
| NumCountOneBits
| I128OfDec => &[IRRELEVANT],
| I128OfDec
| DecWithoutDecimalPoint => &[IRRELEVANT],
StrStartsWith | StrEndsWith => &[BORROWED, BORROWED],
StrFromUtf8 => &[OWNED],
StrToUtf8 => &[OWNED],