|
|
|
@@ -1020,11 +1020,12 @@ static int defineLocal(Compiler* compiler, const char* name, int length)
|
|
|
|
|
return compiler->numLocals++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Declares a variable in the current scope with the name of the previously
|
|
|
|
|
// consumed token. Returns its symbol.
|
|
|
|
|
static int declareVariable(Compiler* compiler)
|
|
|
|
|
// Declares a variable in the current scope whose name is the given token.
|
|
|
|
|
//
|
|
|
|
|
// If [token] is `NULL`, uses the previously consumed token. Returns its symbol.
|
|
|
|
|
static int declareVariable(Compiler* compiler, Token* token)
|
|
|
|
|
{
|
|
|
|
|
Token* token = &compiler->parser->previous;
|
|
|
|
|
if (token == NULL) token = &compiler->parser->previous;
|
|
|
|
|
|
|
|
|
|
if (token->length > MAX_VARIABLE_NAME)
|
|
|
|
|
{
|
|
|
|
@@ -1083,7 +1084,7 @@ static int declareVariable(Compiler* compiler)
|
|
|
|
|
static int declareNamedVariable(Compiler* compiler)
|
|
|
|
|
{
|
|
|
|
|
consume(compiler, TOKEN_NAME, "Expect variable name.");
|
|
|
|
|
return declareVariable(compiler);
|
|
|
|
|
return declareVariable(compiler, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stores a variable with the previously defined symbol in the current scope.
|
|
|
|
@@ -1440,7 +1441,7 @@ static void patchJump(Compiler* compiler, int offset)
|
|
|
|
|
|
|
|
|
|
// Parses a block body, after the initial "{" has been consumed.
|
|
|
|
|
//
|
|
|
|
|
// Returns true if it was a expression body, false if it was an statement body.
|
|
|
|
|
// Returns true if it was a expression body, false if it was a statement body.
|
|
|
|
|
// (More precisely, returns true if a value was left on the stack. An empty
|
|
|
|
|
// block returns false.)
|
|
|
|
|
static bool finishBlock(Compiler* compiler)
|
|
|
|
@@ -1991,7 +1992,7 @@ static void staticField(Compiler* compiler, bool allowAssignment)
|
|
|
|
|
// define it as a variable in the scope surrounding the class definition.
|
|
|
|
|
if (resolveLocal(classCompiler, token->start, token->length) == -1)
|
|
|
|
|
{
|
|
|
|
|
int symbol = declareVariable(classCompiler);
|
|
|
|
|
int symbol = declareVariable(classCompiler, NULL);
|
|
|
|
|
|
|
|
|
|
// Implicitly initialize it to null.
|
|
|
|
|
emit(classCompiler, CODE_NULL);
|
|
|
|
@@ -3071,8 +3072,7 @@ static void import(Compiler* compiler)
|
|
|
|
|
// Compile the comma-separated list of variables to import.
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
consume(compiler, TOKEN_NAME, "Expect name of variable to import.");
|
|
|
|
|
int slot = declareVariable(compiler);
|
|
|
|
|
int slot = declareNamedVariable(compiler);
|
|
|
|
|
|
|
|
|
|
// Define a string constant for the variable name.
|
|
|
|
|
int variableConstant = addConstant(compiler,
|
|
|
|
@@ -3091,8 +3091,10 @@ static void import(Compiler* compiler)
|
|
|
|
|
|
|
|
|
|
static void variableDefinition(Compiler* compiler)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Variable should not be in scope until after initializer.
|
|
|
|
|
int symbol = declareNamedVariable(compiler);
|
|
|
|
|
// Grab its name, but don't declare it yet. A (local) variable shouldn't be
|
|
|
|
|
// in scope in its own initializer.
|
|
|
|
|
consume(compiler, TOKEN_NAME, "Expect variable name.");
|
|
|
|
|
Token nameToken = compiler->parser->previous;
|
|
|
|
|
|
|
|
|
|
// Compile the initializer.
|
|
|
|
|
if (match(compiler, TOKEN_EQ))
|
|
|
|
@@ -3105,6 +3107,8 @@ static void variableDefinition(Compiler* compiler)
|
|
|
|
|
null(compiler, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now put it in scope.
|
|
|
|
|
int symbol = declareVariable(compiler, &nameToken);
|
|
|
|
|
defineVariable(compiler, symbol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|