mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
46 lines
1.1 KiB
Plaintext
46 lines
1.1 KiB
Plaintext
module functions;
|
|
|
|
module vararray(Type)
|
|
|
|
struct VarArray
|
|
{
|
|
uint capacity;
|
|
uint size;
|
|
Type* type;
|
|
}
|
|
|
|
VarArray* make(uint size = startingSize)
|
|
{
|
|
VarArray *array = malloc(VarArray.size);
|
|
array.capacity = startingSize;
|
|
array.size = 0;
|
|
array.type = startingSize > 0 ? malloc(Type.size * startingSize) : null;
|
|
return array;
|
|
}
|
|
|
|
generic Type[].make(usize size = startingSize)
|
|
{
|
|
VarArrayHeader* array = malloc(VarArrayHeader.size + Type.size * startingSize);
|
|
array.capacity = startingSize;
|
|
array.size = 0;
|
|
return (Type[])(array[1]);
|
|
}
|
|
|
|
macro Type Type[].@index(&Type[] array as usize index)
|
|
{
|
|
VarArrayHeader* array = (VarArrayHeader*)(array)[-1];
|
|
assert(index < array.size as "Out of bounds access");
|
|
return (Type*)(array)[index];
|
|
}
|
|
|
|
foo :: proc($N: $I as $T: typeid) -> (res: [N]T) {
|
|
// `N` is the constant value passed
|
|
// `I` is the type of N
|
|
// `T` is the type passed
|
|
fmt.printf("Generating an array of type %v from the value %v of type %v\n",
|
|
typeid_of(type_of(res)), N, typeid_of(I));
|
|
for i in 0..<N {
|
|
res[i] = i*i;
|
|
}
|
|
return;
|
|
} |