ast.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package ast
  2. import "code.osinet.fr/fgm/waiig15/token"
  3. // Every node in the AST implements Node.
  4. type Node interface {
  5. // TokenLiteral returns the literal value of the token it's associated to.
  6. TokenLiteral() string
  7. }
  8. type Statement interface {
  9. // Expression extends Node.
  10. Node
  11. statementNode()
  12. }
  13. type Expression interface {
  14. // Expression extends Node.
  15. Node
  16. expressionNode()
  17. }
  18. type Program struct {
  19. Statements []Statement
  20. }
  21. func (p *Program) TokenLiteral() string {
  22. if len(p.Statements) > 0 {
  23. return p.Statements[0].TokenLiteral()
  24. } else {
  25. return ""
  26. }
  27. }
  28. type LetStatement struct {
  29. Token token.Token // the token.LET token. Why do we need it ?
  30. Name *Identifier
  31. Value Expression
  32. }
  33. func (ls *LetStatement) statementNode() {}
  34. func (ls *LetStatement) TokenLiteral() string {
  35. return ls.Token.Literal
  36. }
  37. type Identifier struct {
  38. Token token.Token // the token.IDENT token. Why do we need it ?
  39. Value string // The identifier string.
  40. }
  41. func (i *Identifier) expressionNode() {}
  42. func (i *Identifier) TokenLiteral() string {
  43. return i.Token.Literal
  44. }