Skip to content

Commit

Permalink
Fix - print statement parsing (#345)
Browse files Browse the repository at this point in the history
* Print statement parsing needs to stop when it reaches an `else`

* Handle `return` without value in inline if
  • Loading branch information
Philippe authored Mar 2, 2021
1 parent 27110fb commit 5d440d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,8 @@ export class Parser {
values.push(this.advance() as PrintSeparatorSpace);
} else if (this.check(TokenKind.Comma)) {
values.push(this.advance() as PrintSeparatorTab);
} else if (this.check(TokenKind.Else)) {
break; // inline branch
} else {
values.push(this.expression());
}
Expand Down Expand Up @@ -1780,7 +1782,7 @@ export class Parser {
return new ReturnStatement(tokens);
}

let toReturn = this.expression();
let toReturn = this.check(TokenKind.Else) ? undefined : this.expression();
return new ReturnStatement(tokens, toReturn);
}

Expand Down
15 changes: 15 additions & 0 deletions src/parser/tests/controlFlow/If.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ describe('parser if statements', () => {
expect(diagnostics).to.be.lengthOf(0);
expect(statements).to.be.length.greaterThan(0);
});

it('parses special statements in inline block', () => {
const { statements, diagnostics } = Parser.parse(`
if true print 1 else print 1
if true then print 1 else print 1
if true print "x=" ; 1 else print 1
if true then print "x=", 1 else print 1
if true print "x=" 1 else print 1
if true return else print 1
if true then return else print 1
`);

expect(diagnostics).to.be.lengthOf(0);
expect(statements).to.be.length.greaterThan(0);
});
});

describe('block if', () => {
Expand Down

0 comments on commit 5d440d3

Please sign in to comment.