mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 08:25:02 +01:00
85 lines
No EOL
1.4 KiB
Text
85 lines
No EOL
1.4 KiB
Text
mod rt;
|
|
mod list;
|
|
mod io;
|
|
|
|
function printlnI32(x: I32) = (
|
|
printI32(x);
|
|
print("\n");
|
|
);
|
|
|
|
function printlnInt(x: Int) = (
|
|
printInt(x);
|
|
print("\n");
|
|
);
|
|
|
|
function printI32(x: I32) = printInt(i32ToInt(x));
|
|
|
|
function printInt(x: Int) = (
|
|
let mag = log10(x);
|
|
|
|
loop (
|
|
if mag == 0 then break;
|
|
let base = pow(10, mag);
|
|
|
|
let digit = x / base;
|
|
print(stringForDigit(digit));
|
|
|
|
x = x % base;
|
|
mag = mag - 1;
|
|
);
|
|
|
|
print(stringForDigit(x % 10));
|
|
);
|
|
|
|
function stringForDigit(x: Int): String =
|
|
if x == 0 then "0"
|
|
else if x == 1 then "1"
|
|
else if x == 2 then "2"
|
|
else if x == 3 then "3"
|
|
else if x == 4 then "4"
|
|
else if x == 5 then "5"
|
|
else if x == 6 then "6"
|
|
else if x == 7 then "7"
|
|
else if x == 8 then "8"
|
|
else if x == 9 then "9"
|
|
else trap();
|
|
|
|
function log10(x: Int): Int = (
|
|
let i = 0;
|
|
loop (
|
|
if x < 10 then break;
|
|
i = i + 1;
|
|
x = x / 10;
|
|
);
|
|
i
|
|
);
|
|
|
|
function pow(base: Int, exp: Int): Int = (
|
|
let acc = 1;
|
|
loop (
|
|
if exp == 0 then break;
|
|
acc = acc * base;
|
|
exp = exp - 1;
|
|
);
|
|
acc
|
|
);
|
|
|
|
function println(s: String) = (
|
|
print(s);
|
|
print("\n");
|
|
);
|
|
|
|
function alignUp(x: I32, align: I32): I32 = (x + (align - 1_I32)) & !(align - 1_I32);
|
|
|
|
function i32ToInt(x: I32): Int = __i32_extend_to_i64_u(x);
|
|
|
|
function abort(message: String) = (
|
|
print("fatal error: ");
|
|
print(message);
|
|
println(".. aborting");
|
|
trap();
|
|
);
|
|
|
|
function main() = (
|
|
std.rt.alloc.test();
|
|
); |