1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package parser
- import (
- "fmt"
- "code.osinet.fr/fgm/waiig15/ast"
- "code.osinet.fr/fgm/waiig15/token"
- )
- func (p *Parser) noPrefixParseFnError(t token.TokenType) {
- msg := fmt.Sprintf("no prefix parse function for %s found", t)
- p.errors = append(p.errors, msg)
- }
- func (p *Parser) parseExpression(precedence int) ast.Expression {
- prefix := p.prefixParseFns[p.curToken.Type]
- if prefix == nil {
- p.noPrefixParseFnError(p.curToken.Type)
- return nil
- }
- leftExp := prefix()
- for !p.peekTokenIs(token.SEMICOLON) && precedence < p.peekPrecedence() {
- infix := p.infixParseFns[p.peekToken.Type]
- if infix == nil {
- return leftExp
- }
- p.nextToken()
- leftExp = infix(leftExp)
- }
- return leftExp
- }
- func (p *Parser) parseExpressionStatement() *ast.ExpressionStatement {
- stmt := &ast.ExpressionStatement{
- Token: p.curToken,
- }
- stmt.Expression = p.parseExpression(LOWEST)
- // Semicolons are optional to help use REPL input.
- if p.peekTokenIs(token.SEMICOLON) {
- p.nextToken()
- }
- return stmt
- }
|