Skip to content

Commit

Permalink
Merge pull request #77 from rooby-lang/fix-while-syntax
Browse files Browse the repository at this point in the history
Fix while statement's weird syntax
  • Loading branch information
st0012 authored May 7, 2017
2 parents 62eb814 + 7728395 commit f32c1fc
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 14 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,14 @@ A lot people have questions about `rooby` since it's a new language and you may
- Array
- Flow control
- If statement
- while statement with [special syntax](https://github.com/rooby-lang/rooby/wiki/Special-syntaxes)
- while statement
- Import other files
- require_relative
- IO
- `puts`
- `ARGV`

**(You can open an issue for any feature request)**

## Special syntax

Currently our parser has some limitation that cause `rooby` unable to support some syntaxes. These special cases are documented in [wiki's special syntax page](https://github.com/rooby-lang/rooby/wiki/Special-syntaxes).
**(You can open an issue for any feature request)**

## TODO & WIP

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/million_loop.ro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
i = 1000000
while i > 0; do
while i > 0 do
i = i - 1
end

Expand Down
2 changes: 1 addition & 1 deletion bytecode/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func TestWhileStatementWithMethodCallInCondition(t *testing.T) {
i = 10
a = [1, 2, 3]
while i > a.length; do
while i > a.length do
i = i - 1
end
Expand Down
2 changes: 1 addition & 1 deletion parser/expression_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (p *Parser) parseCallExpression(receiver ast.Expression) ast.Expression {
}

// Parse block
if p.peekTokenIs(token.Do) {
if p.peekTokenIs(token.Do) && p.acceptBlock {
p.parseBlockParameters(exp)
}

Expand Down
10 changes: 8 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type Parser struct {

prefixParseFns map[token.Type]prefixParseFn
infixParseFns map[token.Type]infixParseFn

// Determine if call expression should accept block argument,
// currently only used when parsing while statement.
// However, this is not a very good practice should change it in the future.
acceptBlock bool
}

// BuildAST tokenizes and parses given file to build AST
Expand All @@ -35,8 +40,9 @@ func BuildAST(file []byte) *ast.Program {
// New initializes a parser and returns it
func New(l *lexer.Lexer) *Parser {
p := &Parser{
l: l,
errors: []string{},
l: l,
errors: []string{},
acceptBlock: true,
}

// Read two tokens, so curToken and peekToken are both set.
Expand Down
3 changes: 3 additions & 0 deletions parser/statement_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ func (p *Parser) parseWhileStatement() *ast.WhileStatement {
ws := &ast.WhileStatement{Token: p.curToken}

p.nextToken()
// Prevent expression's method call to consume while's block as argument.
p.acceptBlock = false
ws.Condition = p.parseExpression(LOWEST)
p.acceptBlock = true
p.nextToken()

if p.curTokenIs(token.Semicolon) {
Expand Down
2 changes: 1 addition & 1 deletion parser/statement_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func TestRequireRelativeStatement(t *testing.T) {

func TestWhileStatement(t *testing.T) {
input := `
while i < a.length; do
while i < a.length do
puts(i)
i++
end
Expand Down
2 changes: 1 addition & 1 deletion samples/loop.ro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
i = 0
while i < 10; do
while i < 10 do
i = i + 1
puts(i)
end
Expand Down
2 changes: 1 addition & 1 deletion vm/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestWhileStatement(t *testing.T) {
`
a = [1, 2, 3, 4, 5]
i = 0
while i < a.length; do
while i < a.length do
a[i]++
i++
end
Expand Down

0 comments on commit f32c1fc

Please sign in to comment.