Parsed grouped expressions (that was easy!).
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
c417e90f36
commit
4acc09faf9
2 changed files with 31 additions and 0 deletions
|
|
@ -37,6 +37,7 @@ func New(l *lexer.Lexer) *Parser {
|
|||
p.registerPrefix(token.BANG, p.parsePrefixExpression)
|
||||
p.registerPrefix(token.TRUE, p.parseBoolean)
|
||||
p.registerPrefix(token.FALSE, p.parseBoolean)
|
||||
p.registerPrefix(token.LPAREN, p.parseGroupedExpression)
|
||||
// Infix registrations
|
||||
p.registerInfix(token.PLUS, p.parseInfixExpression)
|
||||
p.registerInfix(token.MINUS, p.parseInfixExpression)
|
||||
|
|
@ -176,6 +177,16 @@ func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression {
|
|||
return exp
|
||||
}
|
||||
|
||||
func (p *Parser) parseGroupedExpression() ast.Expression {
|
||||
p.nextToken()
|
||||
exp := p.parseExpression(LOWEST)
|
||||
if !p.expectPeek(token.RPAREN) {
|
||||
// TODO: Would probably be good to emit an error here?
|
||||
return nil
|
||||
}
|
||||
return exp
|
||||
}
|
||||
|
||||
func (p *Parser) curTokenIs(typ token.TokenType) bool {
|
||||
return p.curToken.Type == typ
|
||||
}
|
||||
|
|
|
|||
|
|
@ -316,6 +316,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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue