ast.go 910 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package ast
  2. // Node is the interface implemented by every node in the AST.
  3. type Node interface {
  4. // TokenLiteral returns the literal value of the token it's associated to.
  5. TokenLiteral() string
  6. }
  7. // Statement extends Node and is only a way to differentiate between expressions
  8. // and statements.
  9. type Statement interface {
  10. Node
  11. statementNode()
  12. }
  13. // Expression extends Node and is only a way to differentiate between
  14. // expressions and statements.
  15. type Expression interface {
  16. // Expression extends Node.
  17. Node
  18. expressionNode()
  19. }
  20. // Program represents the outer structure of the parser program.
  21. type Program struct {
  22. Statements []Statement
  23. }
  24. // TokenLiteral returns the string contents of the first statement token, which
  25. // can be an empty string if the program is empty.
  26. func (p *Program) TokenLiteral() string {
  27. if len(p.Statements) > 0 {
  28. return p.Statements[0].TokenLiteral()
  29. }
  30. return ""
  31. }