wraith-lang/generic.w

85 lines
1.6 KiB
OpenEdge ABL

struct Foo {
static Func2<U>(u: U) : U {
return u;
}
static Func<T>(t: T): T {
foo: T = t;
return Foo.Func2(foo);
}
}
struct MemoryBlock<T>
{
start: MemoryAddress;
capacity: uint;
AddressOf(index: uint): MemoryAddress
{
return start + (index * @sizeof<T>());
}
Get(index: uint): T
{
return @dereference<T>(AddressOf(index));
}
Set(index: uint, value: T): void
{
@memcpy(AddressOf(index), @addr(value), @sizeof<T>());
}
Free(): void
{
@free(start);
}
}
struct Array<T>
{
memoryBlock: MemoryBlock<T>;
static Init(capacity: uint): Array<T>
{
return Array<T>
{
memoryBlock: MemoryBlock<T> { capacity: capacity, start: @malloc(capacity * @sizeof<T>()) }
};
}
Get(index: uint): T
{
return memoryBlock.Get(index);
}
}
interface Iterable<T>
{
HasNext(): bool;
Next(): T;
}
struct Program {
static Main(): int {
array: Array<int> = Array<int>.Init(4);
x: int = 4;
y: int = Foo.Func(x);
block: MemoryBlock<int> = MemoryBlock<int>
{
capacity: y,
start: @malloc(y * @sizeof<int>())
};
block.Set(0, 5);
block.Set(1, 3);
block.Set(2, 9);
block.Set(3, 100);
Console.PrintLine("%p", block.start);
Console.PrintLine("%i", block.Get(0));
Console.PrintLine("%i", block.Get(1));
Console.PrintLine("%i", block.Get(2));
Console.PrintLine("%i", block.Get(3));
block.Free();
return 0;
}
}