Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Setting array sizes from constants throws #11

Open
gmarty opened this Issue · 3 comments

4 participants

@gmarty

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.

@mbebenita
Owner

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.

@luiscubal

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

@rinon
Collaborator

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.