feat: convert fiber tests to use block arguments instead of anonymous function literals

Update all fiber test files to replace `Fiber.create(fn { ... })` with `Fiber.create { ... }` block syntax, and add compiler support for parsing block bodies without requiring a newline after the opening brace. The compiler changes include removing `TOKEN_LEFT_BRACE` from the newline-discarding token list and adding `match(compiler, TOKEN_LINE)` calls in `finishBlock`, `methodCall`, `function`, and `block` to handle empty block bodies and inline expressions.
This commit is contained in:
Bob Nystrom
2014-04-03 00:31:58 +00:00
parent 5a017d17de
commit c606e93516
13 changed files with 45 additions and 27 deletions
+20 -2
View File
@@ -846,7 +846,6 @@ static void nextToken(Parser* parser)
// Discard newlines after tokens that cannot end an expression.
case TOKEN_LEFT_PAREN:
case TOKEN_LEFT_BRACKET:
case TOKEN_LEFT_BRACE:
case TOKEN_DOT:
case TOKEN_DOTDOT:
case TOKEN_DOTDOTDOT:
@@ -1349,6 +1348,9 @@ static void patchJump(Compiler* compiler, int offset)
// Parses a block body, after the initial "{" has been consumed.
static void finishBlock(Compiler* compiler)
{
// TODO: If no newline, just parse expr.
match(compiler, TOKEN_LINE);
// Empty blocks do nothing.
if (match(compiler, TOKEN_RIGHT_BRACE)) return;
@@ -1465,6 +1467,12 @@ static void methodCall(Compiler* compiler, Code instruction,
emit(&fnCompiler, CODE_NULL);
emit(&fnCompiler, CODE_RETURN);
}
else if (match(&fnCompiler, TOKEN_RIGHT_BRACE))
{
// Empty body.
emit(&fnCompiler, CODE_NULL);
emit(&fnCompiler, CODE_RETURN);
}
else
{
// TODO: Are we OK with a newline determining whether or not there's an
@@ -1580,6 +1588,9 @@ static void function(Compiler* compiler, bool allowAssignment)
if (match(&fnCompiler, TOKEN_LEFT_BRACE))
{
// TODO: If no newline, just parse expr.
match(compiler, TOKEN_LINE);
// Block body.
finishBlock(&fnCompiler);
@@ -2147,6 +2158,9 @@ void block(Compiler* compiler)
// Curly block.
if (match(compiler, TOKEN_LEFT_BRACE))
{
// TODO: If no newline, just parse expr.
match(compiler, TOKEN_LINE);
pushScope(compiler);
finishBlock(compiler);
popScope(compiler);
@@ -2587,7 +2601,11 @@ static void classDefinition(Compiler* compiler)
compiler->enclosingClass = &classCompiler;
// Compile the method definitions.
consume(compiler, TOKEN_LEFT_BRACE, "Expect '{' after class body.");
consume(compiler, TOKEN_LEFT_BRACE, "Expect '{' after class declaration.");
// TODO: Should newline be required here?
match(compiler, TOKEN_LINE);
while (!match(compiler, TOKEN_RIGHT_BRACE))
{
Code instruction = CODE_METHOD_INSTANCE;