Parse function literals.

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-05 16:13:08 -08:00
parent 9e9324bb56
commit 985cf24fbc
3 changed files with 149 additions and 0 deletions

View file

@ -39,6 +39,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.FALSE, p.parseBoolean)
p.registerPrefix(token.LPAREN, p.parseGroupedExpression)
p.registerPrefix(token.IF, p.parseIfExpression)
p.registerPrefix(token.FUNCTION, p.parseFunctionLiteral)
// Infix registrations
p.registerInfix(token.PLUS, p.parseInfixExpression)
p.registerInfix(token.MINUS, p.parseInfixExpression)
@ -233,6 +234,44 @@ func (p *Parser) parseIfExpression() ast.Expression {
return exp
}
func (p *Parser) parseFunctionLiteral() ast.Expression {
fn := &ast.FunctionLiteral{Token: p.curToken}
if !p.nextTokenIfPeekIs(token.LPAREN) {
// TODO: Would be good to emit an error here.
return nil
}
fn.Parameters = p.parseFunctionParameters()
if !p.nextTokenIfPeekIs(token.LBRACE) {
// TODO: Would be good to emit an error here.
return nil
}
fn.Body = p.parseBlockStatement()
return fn
}
func (p *Parser) parseFunctionParameters() []*ast.Identifier {
params := []*ast.Identifier{}
if p.peekTokenIs(token.RPAREN) {
p.nextToken()
return params
}
// Consume the LPAREN
p.nextToken()
params = append(params, &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal})
for p.peekTokenIs(token.COMMA) {
// Consume the previous identifier.
p.nextToken()
// Consume the comma.
p.nextToken()
params = append(params, &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal})
}
if !p.nextTokenIfPeekIs(token.RPAREN) {
// TODO: Would be good to emit an error here.
return nil
}
return params
}
func (p *Parser) curTokenIs(typ token.TokenType) bool {
return p.curToken.Type == typ
}