2021-06-01 19:58:46 +00:00
|
|
|
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-09-07 19:46:47 +00:00
|
|
|
static Init(capacity: uint): MemoryBlock<T>
|
|
|
|
{
|
|
|
|
return MemoryBlock<T>
|
|
|
|
{
|
|
|
|
capacity: capacity,
|
|
|
|
start: @malloc(capacity * @sizeof<T>())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-07 18:51:33 +00:00
|
|
|
AddressOf(index: uint): MemoryAddress
|
2021-06-03 00:26:26 +00:00
|
|
|
{
|
2021-06-07 18:51:33 +00:00
|
|
|
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);
|
2021-06-03 00:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 06:59:43 +00:00
|
|
|
struct Array<T>
|
|
|
|
{
|
|
|
|
memoryBlock: MemoryBlock<T>;
|
|
|
|
|
|
|
|
static Init(capacity: uint): Array<T>
|
|
|
|
{
|
|
|
|
return Array<T>
|
|
|
|
{
|
2021-09-07 19:46:47 +00:00
|
|
|
memoryBlock: MemoryBlock<T>.Init(capacity)
|
2021-09-07 06:59:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Get(index: uint): T
|
|
|
|
{
|
|
|
|
return memoryBlock.Get(index);
|
|
|
|
}
|
2021-09-07 18:54:31 +00:00
|
|
|
|
|
|
|
Set(index: uint, value: T): void
|
|
|
|
{
|
|
|
|
memoryBlock.Set(index, value);
|
|
|
|
}
|
2021-09-07 06:59:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 19:58:46 +00:00
|
|
|
struct Program {
|
|
|
|
static Main(): int {
|
2021-09-07 06:59:43 +00:00
|
|
|
array: Array<int> = Array<int>.Init(4);
|
2021-06-01 19:58:46 +00:00
|
|
|
x: int = 4;
|
|
|
|
y: int = Foo.Func(x);
|
2021-09-07 18:54:31 +00:00
|
|
|
array.Set(0, 2);
|
|
|
|
array.Set(1, 0);
|
|
|
|
array.Set(2, 5);
|
|
|
|
array.Set(3, 9);
|
|
|
|
Console.PrintLine("%i", array.Get(0));
|
|
|
|
Console.PrintLine("%i", array.Get(3));
|
2021-06-07 18:51:33 +00:00
|
|
|
return 0;
|
2021-06-01 19:58:46 +00:00
|
|
|
}
|
|
|
|
}
|