forked from Mirror/wren
Allow variables without initializers.
This commit is contained in:
@ -1900,11 +1900,16 @@ void statement(Compiler* compiler)
|
||||
// TODO: Variable should not be in scope until after initializer.
|
||||
int symbol = declareVariable(compiler);
|
||||
|
||||
// TODO: Allow uninitialized vars?
|
||||
consume(compiler, TOKEN_EQ, "Expect '=' after variable name.");
|
||||
|
||||
// Compile the initializer.
|
||||
expression(compiler);
|
||||
if (match(compiler, TOKEN_EQ))
|
||||
{
|
||||
expression(compiler);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default initialize it to null.
|
||||
null(compiler, false);
|
||||
}
|
||||
|
||||
defineVariable(compiler, symbol);
|
||||
return;
|
||||
|
||||
2
test/variable/global_without_initializer.wren
Normal file
2
test/variable/global_without_initializer.wren
Normal file
@ -0,0 +1,2 @@
|
||||
var a
|
||||
IO.write(a) // expect: null
|
||||
4
test/variable/local_without_initializer.wren
Normal file
4
test/variable/local_without_initializer.wren
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
var a
|
||||
IO.write(a) // expect: null
|
||||
}
|
||||
Reference in New Issue
Block a user