wraith-lang/generic.w

59 lines
1.2 KiB
OpenEdge ABL
Raw Normal View History

struct Foo {
static Func2<U>(u: U) : U {
return u;
}
static Func<T>(t: T): T {
foo: T = t;
return Foo.Func2(foo);
}
}
2021-06-03 00:26:26 +00:00
struct MemoryBlock<T>
{
start: MemoryAddress;
capacity: uint;
2021-06-04 00:48:16 +00:00
AddressOf(index: uint): MemoryAddress
2021-06-03 00:26:26 +00:00
{
2021-06-04 00:48:16 +00:00
return start + (index * @sizeof<T>());
}
Get(index: uint): T
{
2021-06-04 07:41:30 +00:00
return @dereference<T>(AddressOf(index));
}
Set(index: uint, value: T): void
{
@memcpy(AddressOf(index), @addr(value), @sizeof<T>());
2021-06-04 00:48:16 +00:00
}
Free(): void
{
@free(start);
2021-06-03 00:26:26 +00:00
}
}
struct Program {
static Main(): int {
x: int = 4;
y: int = Foo.Func(x);
2021-06-04 07:41:30 +00:00
block: MemoryBlock<int>;
2021-06-03 22:08:01 +00:00
block.capacity = y;
block.start = @malloc(y * @sizeof<int>());
z: MemoryAddress = block.AddressOf(2);
Console.PrintLine("%p", block.start);
2021-06-04 07:41:30 +00:00
block.Set(0, 5);
block.Set(1, 3);
block.Set(2, 9);
block.Set(3, 100);
Console.PrintLine("%i", block.Get(0));
Console.PrintLine("%i", block.Get(1));
Console.PrintLine("%i", block.Get(2));
Console.PrintLine("%i", block.Get(3));
2021-06-04 00:48:16 +00:00
block.Free();
2021-06-03 22:08:01 +00:00
return 0;
}
}