82 lines
2.3 KiB
Nix
82 lines
2.3 KiB
Nix
{ debug, expression, float, intrinsics, list, string, type, ... }:
|
|
let
|
|
inherit(intrinsics) toString;
|
|
|
|
from#: int | float | string -> int
|
|
= value:
|
|
type.matchPrimitiveOrDefault value
|
|
{
|
|
float = float.round' value;
|
|
int = value;
|
|
string
|
|
= let
|
|
result = toInteger value;
|
|
in
|
|
if result != null
|
|
then
|
|
result
|
|
else
|
|
debug.panic "from" "Could not convert string ${value} to int!";
|
|
}
|
|
(
|
|
debug.panic
|
|
"from"
|
|
{
|
|
text = "Could not convert type ${type.get value} to int!";
|
|
data = value;
|
|
}
|
|
);
|
|
|
|
isInstanceOf = intrinsics.isInt or (value: type.getPrimitive value == "int");
|
|
|
|
divmod
|
|
= value:
|
|
modulus:
|
|
let
|
|
value' = value / modulus;
|
|
in
|
|
{
|
|
value = value';
|
|
rest = value - value' * modulus;
|
|
};
|
|
|
|
orNull
|
|
= value:
|
|
isInstanceOf value || value == null;
|
|
|
|
toInteger#: string -> int | null
|
|
= value:
|
|
let
|
|
value' = string.match "([+-])?0*(.+)" value;
|
|
result = expression.fromJSON ( list.get value' 1);
|
|
sign = list.head value';
|
|
in
|
|
if isInstanceOf result
|
|
then
|
|
if sign == "-"
|
|
then
|
|
( 0 - result )
|
|
else
|
|
result
|
|
else
|
|
null;
|
|
in
|
|
type "integer"
|
|
{
|
|
isPrimitive = true;
|
|
|
|
signed
|
|
= type "SignedInteger"
|
|
{
|
|
#isInstanceOf = value: isInstanceOf value;
|
|
};
|
|
|
|
unsigned
|
|
= type "UnsignedInteger"
|
|
{
|
|
#isInstanceOf = value: isInstanceOf value && value >= 0;
|
|
};
|
|
|
|
inherit divmod from isInstanceOf orNull toInteger toString;
|
|
}
|