1
0
forked from Mirror/wren

Add continue statement (#822)

Note that documentation is still required.
This commit is contained in:
Alan Stagner
2020-12-03 08:30:36 -08:00
committed by GitHub
parent f5339993ce
commit 55b926410d
11 changed files with 138 additions and 0 deletions

View File

@ -87,6 +87,7 @@ typedef enum
TOKEN_BANGEQ,
TOKEN_BREAK,
TOKEN_CONTINUE,
TOKEN_CLASS,
TOKEN_CONSTRUCT,
TOKEN_ELSE,
@ -567,6 +568,7 @@ typedef struct
static Keyword keywords[] =
{
{"break", 5, TOKEN_BREAK},
{"continue", 8, TOKEN_CONTINUE},
{"class", 5, TOKEN_CLASS},
{"construct", 9, TOKEN_CONSTRUCT},
{"else", 4, TOKEN_ELSE},
@ -2621,6 +2623,7 @@ GrammarRule rules[] =
/* TOKEN_EQEQ */ INFIX_OPERATOR(PREC_EQUALITY, "=="),
/* TOKEN_BANGEQ */ INFIX_OPERATOR(PREC_EQUALITY, "!="),
/* TOKEN_BREAK */ UNUSED,
/* TOKEN_CONTINUE */ UNUSED,
/* TOKEN_CLASS */ UNUSED,
/* TOKEN_CONSTRUCT */ { NULL, NULL, constructorSignature, PREC_NONE, NULL },
/* TOKEN_ELSE */ UNUSED,
@ -3028,6 +3031,22 @@ void statement(Compiler* compiler)
// bytecode.
emitJump(compiler, CODE_END);
}
else if (match(compiler, TOKEN_CONTINUE))
{
if (compiler->loop == NULL)
{
error(compiler, "Cannot use 'continue' outside of a loop.");
return;
}
// Since we will be jumping out of the scope, make sure any locals in it
// are discarded first.
discardLocals(compiler, compiler->loop->scopeDepth + 1);
// emit a jump back to the top of the loop
int loopOffset = compiler->fn->code.count - compiler->loop->start + 2;
emitShortArg(compiler, CODE_LOOP, loopOffset);
}
else if (match(compiler, TOKEN_FOR))
{
forStatement(compiler);

View File

@ -0,0 +1,9 @@
var f
for (i in [1, 2, 3]) {
var j = 4
f = Fn.new { System.print(i + j) }
continue
}
f.call()
// expect: 7

View File

@ -0,0 +1,9 @@
var f
while (f == null) {
var i = "i"
f = Fn.new { System.print(i) }
continue
}
f.call()
// expect: i

View File

@ -0,0 +1,20 @@
for (i in 0..5) {
{
var a = "a"
{
var b = "b"
{
var c = "c"
if (i == 1) continue
}
}
}
System.print(i)
}
// expect: 0
// expect: 2
// expect: 3
// expect: 4
// expect: 5

View File

@ -0,0 +1,12 @@
for (i in [1, 2, 3, 4, 5]) {
System.print(i)
if (i > 2) continue
System.print(i)
}
// expect: 1
// expect: 1
// expect: 2
// expect: 2
// expect: 3
// expect: 4
// expect: 5

View File

@ -0,0 +1,7 @@
var done = false
while (!done) {
Fn.new {
continue // expect error
}
done = true
}

View File

@ -0,0 +1,9 @@
var done = false
while (!done) {
class Foo {
method {
continue // expect error
}
}
done = true
}

View File

@ -0,0 +1,12 @@
var i = 0
while (true) {
i = i + 1
System.print(i)
if (i <= 2) continue
System.print(i)
break
}
// expect: 1
// expect: 2
// expect: 3
// expect: 3

View File

@ -0,0 +1,17 @@
for (i in 0..2) {
System.print("outer %(i)")
if (i == 1) continue
for (j in 0..2) {
if (j == 1) continue
System.print("inner %(j)")
}
}
// expect: outer 0
// expect: inner 0
// expect: inner 2
// expect: outer 1
// expect: outer 2
// expect: inner 0
// expect: inner 2

View File

@ -0,0 +1,23 @@
var i = 0
while (i <= 2) {
i = i + 1
System.print("outer %(i)")
if (i == 2) continue
var j = 0
while (j <= 2) {
j = j + 1
if(j == 2) continue
System.print("inner %(j)")
}
}
// expect: outer 1
// expect: inner 1
// expect: inner 3
// expect: outer 2
// expect: outer 3
// expect: inner 1
// expect: inner 3

View File

@ -0,0 +1 @@
continue // expect error