Parsed grouped expressions (that was easy!).

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-05 11:37:13 -08:00
parent c417e90f36
commit 4acc09faf9
2 changed files with 31 additions and 0 deletions

View file

@ -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
}