parser_expression.go 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package parser
  2. import (
  3. "fmt"
  4. "code.osinet.fr/fgm/waiig15/ast"
  5. "code.osinet.fr/fgm/waiig15/token"
  6. )
  7. func (p *Parser) noPrefixParseFnError(t token.TokenType) {
  8. msg := fmt.Sprintf("no prefix parse function for %s found", t)
  9. p.errors = append(p.errors, msg)
  10. }
  11. func (p *Parser) parseExpression(precedence int) ast.Expression {
  12. prefix := p.prefixParseFns[p.curToken.Type]
  13. if prefix == nil {
  14. p.noPrefixParseFnError(p.curToken.Type)
  15. return nil
  16. }
  17. leftExp := prefix()
  18. for !p.peekTokenIs(token.SEMICOLON) && precedence < p.peekPrecedence() {
  19. infix := p.infixParseFns[p.peekToken.Type]
  20. if infix == nil {
  21. return leftExp
  22. }
  23. p.nextToken()
  24. leftExp = infix(leftExp)
  25. }
  26. return leftExp
  27. }
  28. func (p *Parser) parseExpressionStatement() *ast.ExpressionStatement {
  29. stmt := &ast.ExpressionStatement{
  30. Token: p.curToken,
  31. }
  32. stmt.Expression = p.parseExpression(LOWEST)
  33. // Semicolons are optional to help use REPL input.
  34. if p.peekTokenIs(token.SEMICOLON) {
  35. p.nextToken()
  36. }
  37. return stmt
  38. }