arturocepeda / Cflat

Embeddable lightweight scripting language with C++ syntax
49 stars 8 forks source link

"Complex" array indexing will throw off the parser #20

Open mundusnine opened 5 months ago

mundusnine commented 5 months ago

The while loop conditional can be thrown of course by "complex" array indexing. This will work:

char text[256] = {0};
int count = 0;
size_t len = strlen((const char*)level_names[i-1]);
char cursor = level_names[i-1][len - 1 - count ];
while( cursor != '_'){
  text[count++] = cursor; 
  cursor = level_names[i-1][len - 1 - count ]; 
} 
//This also:
int idx = i-1;
while( level_names[idx][len - 1 - count ] != '_'){
  text[count++] = level_names[idx][len - 1 - count ]; 
} 

this wont:

while( level_names[i-1][len - 1 - count ]  !=  '_'){
  text[count++] = level_names[i-1][len - 1 - count ]; 
} 

From some testing, the i-1 seems to be the offender in the complete line. FYI, the i comes from : for(int i=1; i < appstate->num_levels;++i){ before the while loop

arturocepeda commented 5 months ago

Multi-dimensional arrays are not supported - in principle I never intended to support them, as that would be somewhat complex to add and you can always use an ordinary array instead. I probably should add an error message, though.

mundusnine commented 5 months ago

Multi-dimensional arrays are not supported - in principle I never intended to support them, as that would be somewhat complex to add and you can always use an ordinary array instead. I probably should add an error message, though.

I guess we could also use a 2d std::vector since we use the STL implementation for the actual calls ?

arturocepeda commented 5 months ago

Oops, sorry, I'm just realizing that I misinterpreted the description of the issue. You are trying to access individual chars from a char array, not to use multi-dimensional arrays. I'll look into it!

Edit: I have just written a little test with double indexing (const char* [ ] --> char) and it seems to work now, after my last commit. Please give it a try and let me know if it did the trick!