Skip to content

Commit 2da664b

Browse files
committed
fix syntax
1 parent 1701fae commit 2da664b

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

lib/ast.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,9 @@ exports.For = For = (function(superclass){
28172817
o.ref = this.item.value;
28182818
}
28192819
if (this['let']) {
2820+
if (that = this.body.getJump()) {
2821+
that instanceof Return || that.carp('inconvertible statement');
2822+
}
28202823
if (this.returns) {
28212824
this.body = this.body.makeReturn();
28222825
}

lib/lexer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ exports.doID = function(code, index){
136136
case 'super':
137137
return this.token('LITERAL', id, true).length;
138138
case 'for':
139+
if ((ref$ = this.tokens)[ref$.length - 1][0] === 'LET') {
140+
this.carp("unexpected 'FOR'");
141+
}
139142
this.seenFor = true;
140143
this.wantBy = false;
141144
break;
@@ -206,9 +209,11 @@ exports.doID = function(code, index){
206209
break;
207210
case 'let':
208211
if ((ref$ = this.tokens)[ref$.length - 1][0] === 'FOR') {
209-
this.tokens.pop();
210-
this.token('LET', 'let');
211-
this.token('FOR', 'for');
212+
this.tokens.splice(this.tokens.length - 1, 0, ['LET', 'let']);
213+
return input.length;
214+
}
215+
if ((ref$ = this.tokens)[ref$.length - 1][0] === 'OWN' && (ref$ = this.tokens)[ref$.length - 2][0] === 'FOR') {
216+
this.tokens.splice(this.tokens.length - 2, 0, ['LET', 'let']);
212217
return input.length;
213218
}
214219
break;

src/ast.co

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,8 @@ class exports.For extends While
17461746
head += \\n + o.indent + itemAssign.compile(o, LEVEL_TOP) + \;
17471747
o.ref = @item.value if @ref
17481748
if @let
1749+
if @body.getJump!
1750+
that instanceof Return or that.carp 'inconvertible statement'
17491751
@body.=makeReturn! if @returns
17501752
@body = Block Call.let capture, @body
17511753
body = @compileBody o

src/lexer.co

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ exports import
105105
case \return \throw then tag = \HURL
106106
case \break \continue then tag = \JUMP
107107
case \this \eval \super then return @token(\LITERAL id, true)length
108-
case \for then @seenFor = true; @wantBy = false
108+
case \for then
109+
if @tokens[*-1]0 is \LET
110+
@carp "unexpected 'FOR'"
111+
@seenFor = true; @wantBy = false
109112
case \then then @seenFor = @wantBy = false
110113
case \catch \function then id = ''
111114
case \in \of
@@ -139,11 +142,13 @@ exports import
139142
if able @tokens then id = \<<<; break
140143
fallthrough
141144
case \export \const \var then tag = \DECL
142-
case \let then if @tokens[*-1]0 is \FOR
143-
@tokens.pop!
144-
@token \LET \let
145-
@token \FOR \for
146-
return input.length
145+
case \let then
146+
if @tokens[*-1]0 is \FOR
147+
@tokens.splice @tokens.length-1 0 <[LET let]>
148+
return input.length
149+
if @tokens[*-1]0 is \OWN and @tokens[*-2]0 is \FOR
150+
@tokens.splice @tokens.length-2 0 <[LET let]>
151+
return input.length
147152
default
148153
break if id of KEYWORDS_SHARED
149154
@carp "reserved word \"#id\"" if id of KEYWORDS_UNUSED

test/loop.co

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,24 @@ for [0]
342342
for [2] => ok 2 of [2]
343343
ok 3 not in []
344344

345+
# `(1; continue)` to avoid `continue` getting optimized away by `While::addBody`
346+
compileThrows 'inconvertible statement' 1 'for let a in b then (1; continue)'
347+
compileThrows 'inconvertible statement' 1 'for let a in b then break'
348+
compileThrows "unexpected 'FOR'" 1 'let for a in b then'
349+
350+
# parses correctly with `own`
351+
for own let a in []
352+
1
353+
354+
for let own a in []
355+
1
356+
357+
i = 2
358+
a = while --i
359+
for let a of [0 1]
360+
a + i
361+
ok '' ''+a
362+
345363
x = 2
346364
for let a in []
347365
x = 3

0 commit comments

Comments
 (0)