add export block and ability to load multiple files

This commit is contained in:
Michael Sippel 2024-05-18 18:01:41 +02:00
parent f06bf14b52
commit 8d19767c98
Signed by: senvas
GPG key ID: F96CF119C34B64A6
13 changed files with 439 additions and 245 deletions

47
lt-stdlib/color.lt Normal file
View file

@ -0,0 +1,47 @@
export {
let morph-rgb-to-hsv = λ{
{
red: _0,1 ~ _256 ~ machine.UInt64;
green: _0,1 ~ _256 ~ machine.UInt64;
blue: _0,1 ~ _256 ~ machine.UInt64;
} : <Color sRGB>
~ RGB
~ <Vec3 _0,1 ~ _256 ~ machine.UInt64>;
/*
::> Color
~ HSV
~ {
hue: Angle
~ Degrees
~ _0,360
~ _360
~ machine.UInt64 ;
saturation: _0,1 ~ _256 ~ machine.UInt64 ;
value: _0,1 ~ _256 ~ machine.UInt64 ;
}
~ <Vec3 machine.UInt64>
*/
} ↦ {
let channel_max = int-max (int-max red green) blue;
let channel_min = int-min (int-min red green) blue;
/* value */
channel_max;
/* saturation */
i/ (i* 255 (i- channel_max channel_min)) channel_max;
/* hue */
i% (i/ (i* 60
if( int-eq channel_max red ) { i+ (i* 0 255) (i- green blue); }
else {
if( int-eq channel_max green ) { i+ (i* 2 255) (i- blue red); }
else { i+ (i* 4 255) (i- red green); };
}
)
(i- channel_max channel_min)
)
360;
};
}

28
lt-stdlib/euclidean.lt Normal file
View file

@ -0,0 +1,28 @@
/* Integer Math
*/
export {
/* Euclidean Algorithm to calculate greatest common divisor
*/
let gcd = λ{
a : ~ machine.Int64;
b : ~ machine.Int64;
} ↦ {
while( b ) {
let tmp = i% a b;
! a b;
! b tmp;
}
a;
};
/* least common multiple
*/
let lcm = λ{
a : ~ machine.Int64;
b : ~ machine.Int64;
} ↦ i* (int-abs a) (i/ (int-abs b) (gcd a b));
};

18
lt-stdlib/int.lt Normal file
View file

@ -0,0 +1,18 @@
/* Two's complement Signed Integers
*/
export {
let int-sign = λx:~machine.Int64 ↦ bit-and (bit-shr x 63) 1;
let int-neg = λx:~machine.Int64 ↦ i+ (bit-neg x) 1;
let int-abs = λx:~machine.Int64 ↦ if( int-sign x ) { int-neg x; } else { x; };
let int-lt = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ int-sign (i- a b);
let int-gt = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ int-sign (i- b a);
let int-eq = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ if (bit-xor a b) { 0; } else { 1; };
let int-lte = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ bit-or (int-lt a b) (int-eq a b);
let int-gte = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ bit-or (int-gt a b) (int-eq a b);
let int-min = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ if( int-lt a b ) { a; } else { b; };
let int-max = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ if( int-gt a b ) { a; } else { b; };
};

63
lt-stdlib/posint.lt Normal file
View file

@ -0,0 +1,63 @@
/* 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;
}

45
lt-stdlib/ratio.lt Normal file
View file

@ -0,0 +1,45 @@
export {
/* Implementation of Rational Numbers
*/
let ratio-scale = λ{
{p:; q:;} : ~ <Ratio ~machine.UInt64> ;
n : ~ machine.UInt64 ;
} ↦ {
i* q n;
i* p n;
};
let ratio-normalize = λ{
p: ~machine.Int64;
q: ~machine.Int64;
} : ~ <Ratio ~machine.Int64>
↦ {
let s = gcd p q;
i/ q s;
i/ p s;
};
let ratio-add = λ{
{ap:; aq:;}: ~ <Ratio ~ _2^64 ~ machine.UInt64> ;
{bp:; bq:;}: ~ <Ratio ~ _2^64 ~ machine.UInt64> ;
} ↦ {
let l = lcm aq bq;
let as = i/ l aq;
let bs = i/ l bq;
i* aq as;
i+ (i* ap as) (i* bp bs);
};
let ratio-mul = λ{
{ap:; aq:;}: ~ <Ratio ~ _2^64 ~ machine.Int64> ;
{bp:; bq:;}: ~ <Ratio ~ _2^64 ~ machine.Int64> ;
} ↦ ratio-normalize (i* ap bp) (i* aq bq);
let fmt-ratio = λ{ p:; q:; }: ~<Ratio > ↦ {
fmt-int q;':';fmt-int p;
};
}

12
lt-stdlib/stdio.lt Normal file
View file

@ -0,0 +1,12 @@
export {
/* output nullterminated string directly from datastack
*/
let print-nullterm =
λ{} : < Seq Char~Ascii~machine.Word >
~ < NullTerminatedArray machine.Word >
↦ {
while(dup) { emit; }
drop;
};
}

23
lt-stdlib/vec3i.lt Normal file
View file

@ -0,0 +1,23 @@
export {
/* Vec3i
*/
let vec3i-add = λ{
{ ax:_2^64; ay:_2^64; az:_2^64; } : <Vec3 _2^64~machine.Int64>;
{ bx:_2^64; by:_2^64; bz:_2^64; } : <Vec3 _2^64~machine.Int64>;
} ↦ {
i+ az bz;
i+ ay by;
i+ ax bx;
};
let fmt-vec3i =
λ{ x:_2^64; y:_2^64; z:_2^64; } : <Vec3 _2^64~machine.Int64>
↦ {
'}';
fmt-int z; '='; 'z'; ' '; ';';
fmt-int y; '='; 'y'; ' '; ';';
fmt-int x; '='; 'x'; '{';
};
}