63 lines
1.3 KiB
Text
63 lines
1.3 KiB
Text
|
||
|
||
/* Positional Integer
|
||
*/
|
||
export {
|
||
let fmt-uint-radix = λ{
|
||
radix : ℕ ~ ℤ_2^64 ~ machine.UInt64;
|
||
x : ℕ ~ ℤ_2^64 ~ machine.UInt64;
|
||
} ↦ {
|
||
if( x ) {
|
||
while( x ) {
|
||
let digit = (i% x radix);
|
||
|
||
if( int-lt digit 10 ) {
|
||
i+ '0' digit;
|
||
} else {
|
||
i+ (i- 'a' 10) digit;
|
||
};
|
||
! x (i/ x radix);
|
||
}
|
||
} else {
|
||
'0';
|
||
};
|
||
};
|
||
|
||
let uint-machine-to-posint =
|
||
λ{
|
||
radix: ℕ ~ ℤ_2^64 ~ machine.UInt64;
|
||
value: ℕ ~ ℤ_2^64 ~ machine.UInt64;
|
||
}
|
||
/*
|
||
::> ℕ
|
||
~ <PosInt radix BigEndian>
|
||
~ <Seq <Digit radix>
|
||
~ ℤ_radix
|
||
~ ℤ_2^64
|
||
~ machine.UInt64>
|
||
~ <LengthPrefixedArray machine.UInt64>
|
||
*/
|
||
↦ {
|
||
let len = 0;
|
||
while( value ) {
|
||
/* push digit to sequence on stack */
|
||
i% value radix;
|
||
! value (i/ value radix);
|
||
! len (i+ len 1);
|
||
}
|
||
/* push length of sequence to stack */
|
||
len;
|
||
};
|
||
|
||
let fmt-int-radix = λ{
|
||
radix: ℕ ~ ℤ_2^64 ~ machine.UInt64;
|
||
x : ℤ ~ machine.Int64;
|
||
} ↦ {
|
||
fmt-uint-radix radix (int-abs x);
|
||
if( int-sign x ) { '-'; };
|
||
};
|
||
|
||
let fmt-uint = λx:ℕ ↦ fmt-uint-radix 10 x;
|
||
let fmt-int = λx:ℤ ↦ fmt-int-radix 10 x;
|
||
}
|
||
|