Fixed bug when using indexing on a generic type. Made array::list::List work with [] and foreach.

This commit is contained in:
Christoffer Lerno
2022-01-26 21:27:13 +01:00
parent e5bcb74822
commit cf56825d26
6 changed files with 6 additions and 220 deletions

View File

@@ -1,46 +0,0 @@
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;
}

View File

@@ -1,70 +0,0 @@
module vector(Type);
public struct Vector
{
Type[] array;
}
public fn void Vector.init()
{
array = nil;
}
public fn void Vector.add(Vector *vector, Type type)
{
vector.array += type;
}
public fn usize Vector.size(Vector *vector)
{
return vector.array.size;
}
public fn void Vector.removeLast(Vector *vector)
{
vector.array.pop();
}
public fn void Vector.removefirst(Vector *vector)
{
vector.array.removeAt(0);
}
public fn void Type *Vector.first(Vector *vector)
{
return &vector.array.first;
}
public fn void Type *Vector.last(Vector *vector)
{
return &vector.array.last();
}
public fn bool Vector.empty(Vector *vector)
{
return !vector.array.size;
}
public macro Vector.foreach(Vector *vector, macro void(Type value) body)
{
for (usize i = 0, i < vector.array.size; i++)
{
@body(vector.array[i]);
}
}
test
{
define IntVector = Vector(int);
IntVector *vector = @calloc(IntVector);
vector.add(1);
vector.add(2);
for (int i : vector)
{
printDigit(i);
}
@vector.foreach(int i)
{
printDigit(i);
}
}

View File

@@ -1,51 +0,0 @@
module comparable;
import std::math;
interface Geometry
{
fn double area();
fn double perim();
}
struct Rect
{
double width, height;
}
struct Circle
{
double radius;
}
fn double Rect.area(Rect *r)
{
return r.width * r.height;
}
fn double Rect.perim(Rect *r)
{
return 2 * r.width + 2 * r.height;
}
fn double Circle.area(Circle *c)
{
return math::PI * c.radius * c.radius
}
fn double Circle.perim(Circle *c)
{
return math::PI * c.radius * 2;
}
fn void measure(virtual Geometry g)
{
printf("area: %f, perimeter: %f\n", g.area(), g.perim());
}
fn void main()
{
Rect r = { 3, 4 };
Circle c = { 5 };
measure(&r);
measure(&c);
}

View File

@@ -1,17 +0,0 @@
module std::io;
interface File : Closable, Readable, Seekable
{
FileInfo[]! readdir(int count);
FileInfo! stat();
}
interface File
{
inline Closable;
inline Readable;
inline Seekable;
FileInfo[]! readdir(int count);
FileInfo! stat();
}

View File

@@ -116,43 +116,12 @@ fn void List.free(List *list)
list.size = 0;
}
/*
operator for(List *list; index, Type type)
macro usize List.operator_len(List &list)
{
$IndexType = typeof(index);
$IndexType last_index = ($IndexType)(list.size);
for ($IndexType i = 0; i < last_index; i++)
{
yield(i, list.entries[index]);
}
return list.len();
}
operator for(List *list; index, Type *type)
macro Type List.operator_element_at(List &list, usize index)
{
$IndexType = typeof(index);
$IndexType last_index = ($IndexType)(list.size);
for ($IndexType i = 0; i < last_index; i++)
{
yield(i, &list.entries[index]);
}
return list.entries[index];
}
operator for(List *list; Type *type)
{
usize size = list.size;
for (usize i = 0; i < last_index; i++)
{
yield(i, &list.entries[index]);
}
}
*/
/*
operator for(List *list; Type type)
{
usize size = list.size;
for (usize i = 0; i < last_index; i++)
{
yield(i, list.entries[index]);
}
}*/