Make sure we don't go over the maximum number of locals.

This commit is contained in:
Bob Nystrom
2013-11-30 18:51:27 -08:00
parent 88852960ca
commit 60c97b5c2f
5 changed files with 1325 additions and 5 deletions
+16 -5
View File
@@ -21,6 +21,15 @@
// `CODE_CALL_XX` instructions assume a certain maximum number.
#define MAX_PARAMETERS (16)
// The maximum number of local (i.e. non-global) variables that can be declared
// in a single function, method, or chunk of top level code. This is the
// maximum number of variables in scope at one time, and spans block scopes.
//
// Note that this limitation is also explicit in the bytecode. Since
// [CODE_LOAD_LOCAL] and [CODE_STORE_LOCAL] use a single argument byte to
// identify the local, only 256 can be in scope at one time.
#define MAX_LOCALS (256)
typedef enum
{
TOKEN_LEFT_PAREN,
@@ -123,9 +132,6 @@ typedef struct
int currentStringLength;
} Parser;
// TODO(bob): Move and doc.
#define MAX_LOCALS (255)
typedef struct
{
// The name of the local variable. This points directly into the original
@@ -692,13 +698,18 @@ static int declareVariable(Compiler* compiler)
}
}
if (compiler->numLocals == MAX_LOCALS)
{
error(compiler, "Cannot declare more than %d variables in one scope.",
MAX_LOCALS);
return -1;
}
// Define a new local variable in the current scope.
Local* local = &compiler->locals[compiler->numLocals];
local->name = name;
local->length = length;
local->depth = compiler->scopeDepth;
// TODO(bob): Check for too many.
return compiler->numLocals++;
}