lt-core/lt-stdlib/posint.lt

64 lines
1.3 KiB
Text
Raw Normal View History

/* 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;
}