parser_let.go 789 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package parser
  2. import (
  3. "code.osinet.fr/fgm/waiig15/ast"
  4. "code.osinet.fr/fgm/waiig15/token"
  5. )
  6. func (p *Parser) parseLetStatement() *ast.LetStatement {
  7. stmt := &ast.LetStatement{
  8. Token: p.curToken,
  9. }
  10. // Let statement starts with an IDENT token, so if next token is not an
  11. // IDENT, the next statement cannot be a Let statement.
  12. if !p.expectPeek(token.IDENT) {
  13. return nil
  14. }
  15. stmt.Name = &ast.Identifier{
  16. Token: p.curToken,
  17. Value: p.curToken.Literal,
  18. }
  19. // The previous expectPeek() call fetched the next token, so we should now
  20. // be on the assignment.
  21. if !p.expectPeek(token.ASSIGN) {
  22. return nil
  23. }
  24. // Skip the expression for now, progress to the semicolon terminating the
  25. // statement.
  26. for !p.curTokenIs(token.SEMICOLON) {
  27. p.nextToken()
  28. }
  29. return stmt
  30. }