LLJS
LLJS copied to clipboard
Setting array sizes from constants throws
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.
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.
C++ has constexpr. Perhaps LLJS could implement a similar mechanism? Alternatively, C99 has variable length arrays. So it should be possible.
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).