Skip to content

Commit

Permalink
2.8: Add grouped expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
nibral committed Jan 13, 2019
1 parent 9b60d31 commit e80c585
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions parser/parser.go
Expand Up @@ -64,6 +64,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.MINUS, p.parsePrefixExpression)
p.registerPrefix(token.TRUE, p.parseBoolean)
p.registerPrefix(token.FALSE, p.parseBoolean)
p.registerPrefix(token.LPAREN, p.parseGroupedExpression)

p.infixParseFns = make(map[token.TokenType]infixParseFn)
p.registerInfix(token.PLUS, p.parseInfixExpression)
Expand Down Expand Up @@ -289,3 +290,15 @@ func (p *Parser) parseBoolean() ast.Expression {
Value: p.curTokenIs(token.TRUE),
}
}

func (p *Parser) parseGroupedExpression() ast.Expression {
p.nextToken()

exp := p.parseExpression(LOWEST)

if !p.expectPeek(token.RPAREN) {
return nil
}

return exp
}
20 changes: 20 additions & 0 deletions parser/parser_test.go
Expand Up @@ -415,6 +415,26 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
"3 < 5 == true",
"((3 < 5) == true)",
},
{
"1 + (2 + 3) + 4",
"((1 + (2 + 3)) + 4)",
},
{
"(5 + 5) * 2",
"((5 + 5) * 2)",
},
{
"2 / (5 + 5)",
"(2 / (5 + 5))",
},
{
"-(5 + 5)",
"(-(5 + 5))",
},
{
"!(true == true)",
"(!(true == true))",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit e80c585

Please sign in to comment.