Open KAYLukas opened 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.
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 :).
Care should be taken to also support foxy(list at [10] set object)
It would be nice to have something like arrays(fixedLists) or arrayaccess in foxyc. An array access could work as follows:
Transforming to:
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
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.