LLJS icon indicating copy to clipboard operation
LLJS copied to clipboard

Setting array sizes from constants throws

Open gmarty opened this issue 12 years ago • 3 comments

Defining an array size from a constant or variable throws a messageFormat is undefined error:

const SIZE = 100;

let int arr[SIZE];
struct ArrayStruct {
  int arr[SIZE];
};

Both declarations throws the error.

Arrays allocated on the heap are not affected though.

gmarty avatar Jul 26 '12 17:07 gmarty

This is a bit tricky, and what you really need is a macro pre-processor, which we don't have. SIZE here is an identifier and is not substituted with 100. Imagine you had const SIZE = getSize(), you can't figure out statically how much space to allocate on the stack. Maybe we can do something to track const declarations that can be evaluated at compile time.

mbebenita avatar Jul 26 '12 19:07 mbebenita

C++ has constexpr. Perhaps LLJS could implement a similar mechanism? Alternatively, C99 has variable length arrays. So it should be possible.

luiscubal avatar Jul 26 '12 21:07 luiscubal

Sorry, it shouldn't have thrown. I was missing an error message. Trying to use a const (or any other expression) now at least prints a nice error.

As far as implementing const sizes, yes, it might be possible, but at the moment the structure of the compiler and when array size calculation happens is not conducive to checking expressions to make sure they are really const literals (we don't have scoping or variable information at the point where size calculation happens).

rinon avatar Jul 27 '12 19:07 rinon