KAYLukas / FoxyC

Adds class-oriented syntaxis to C, by using macro's only.
0 stars 0 forks source link

Native arrays/list interface #8

Open KAYLukas opened 10 years ago

KAYLukas commented 10 years ago

It would be nice to have something like arrays(fixedLists) or arrayaccess in foxyc. An array access could work as follows:

foxy(list $ [10])

Transforming to:

({
                  if(list->class== getClass(FixedList)){
                     if(10 < ((FixedList*)list)->length){
                     return ((Object*)list + ARRAY_OFFSET)[10]);
                     }else{
                         throw new ArrayOutOfBoundsException();
                     }
                  }else{
                      _FoxyC_SyntaxLogic_Runtime_Push_ObjectScope(list);
                        result = list->class->get(_EXTRACT_INT([10]));
                       _FoxyC_SyntaxLogic_Runtime_Pop_ObjectScope(list);
                        return result;
                  });

Note that the get function is a function always defined in class, it just redirects towards ->ArrayAccess->_get, requiring that the array access interface is implemented, other wise throwing an error. We should note, that it is impossible to do this check at compiletime as this we can't merge this in the instance method function. Also 10 can be extracted from [10] by doing

size_t count = ((size_t)&(((char*)0)[10]) );

As arrays will have different sizes depending on the length, the class of an array will only describe the header.

An extra alternative to a fixedlist could be a immutableList, which allocates items contiguous in memory, but only elements of the same type. This explicitly will deny assignments, and will have to be initialized using a lambda function of some sort.

KAYLukas commented 10 years ago

The problem with the foxy(list $ [0]) statement is that it overlaps with the instance method call. We can resolve this in the following way: prepend all function calls with a predefined character (say '_') Define in class (can be done via a union, just needs to exists) a void\ property. Then the instance call will be translated to:

({
                  _FoxyC_SyntaxLogic_Runtime_Push_ObjectScope(t);
                  result = t->class->_[0];
                  _FoxyC_SyntaxLogic_Runtime_Pop_ObjectScope(t);
                  return result;
                  });

This trivially will compile as _ exists. Now we can implement the array access by doing (first check on empty argument!)

#define STARTS_WITH_SQUARE_BRACKET(x) #x[0] == '['
..
if(STARTS_WITH_SQUARE_BRACKET([10])){
    //array access

}else{
//normal function call

}

Of course, a comparison like "[10]"[0] == "[" will easily be optimized to true by the compiler, inducing no performance lag.

KAYLukas commented 10 years ago

We need to ensure that if we implement the if "[" route, we need to ensure that the arrayaccess part also compiles when the parameter is not a [0] type.

Not sure if this is possible, better way would be to just introduce an index keyword to access elements, also allows type checking :).

KAYLukas commented 10 years ago

Care should be taken to also support foxy(list at [10] set object)