diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 044377bf..68372f5a 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -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; diff --git a/test/variable/global_without_initializer.wren b/test/variable/global_without_initializer.wren new file mode 100644 index 00000000..c63a2769 --- /dev/null +++ b/test/variable/global_without_initializer.wren @@ -0,0 +1,2 @@ +var a +IO.write(a) // expect: null diff --git a/test/variable/local_without_initializer.wren b/test/variable/local_without_initializer.wren new file mode 100644 index 00000000..02a624d1 --- /dev/null +++ b/test/variable/local_without_initializer.wren @@ -0,0 +1,4 @@ +{ + var a + IO.write(a) // expect: null +}