ast.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package ast
  2. import (
  3. "bytes"
  4. "code.osinet.fr/fgm/waiig15/token"
  5. )
  6. // Node is the interface implemented by every node in the AST. It extends the
  7. // fmt.Stringer interface, allowing simple printing of nodes.
  8. type Node interface {
  9. // TokenLiteral returns the literal value of the token it's associated to.
  10. TokenLiteral() string
  11. String() string
  12. }
  13. // Statement extends Node and is only a way to differentiate between expressions
  14. // and statements.
  15. type Statement interface {
  16. Node
  17. statementNode()
  18. }
  19. // Expression extends Node and is only a way to differentiate between
  20. // expressions and statements.
  21. type Expression interface {
  22. // Expression extends Node.
  23. Node
  24. expressionNode()
  25. }
  26. // Program represents the outer structure of the parser program.
  27. type Program struct {
  28. Statements []Statement
  29. }
  30. // TokenLiteral returns the string contents of the first statement token, which
  31. // can be an empty string if the program is empty.
  32. func (p *Program) TokenLiteral() string {
  33. if len(p.Statements) > 0 {
  34. return p.Statements[0].TokenLiteral()
  35. }
  36. return ""
  37. }
  38. // String satisfies the Node and fmt.Stringer interfaces.
  39. func (p *Program) String() string {
  40. var out bytes.Buffer
  41. for _, s := range p.Statements {
  42. out.WriteString(s.String())
  43. }
  44. return out.String()
  45. }
  46. // LetStatement is the Node type for Let statements.
  47. type LetStatement struct {
  48. Token token.Token // the token.LET token. Why do we need it ?
  49. Name *Identifier
  50. Value Expression
  51. }
  52. func (ls *LetStatement) statementNode() {}
  53. // TokenLiteral satisfies the Node interface.
  54. func (ls *LetStatement) TokenLiteral() string {
  55. return ls.Token.Literal
  56. }
  57. // String implements Node and fmt.Stringer.
  58. func (ls *LetStatement) String() string {
  59. var out bytes.Buffer
  60. out.WriteString(ls.TokenLiteral() + " ")
  61. out.WriteString(ls.Name.String())
  62. out.WriteString(" = ")
  63. if ls.Value != nil {
  64. out.WriteString(ls.Value.String())
  65. }
  66. out.WriteString(";")
  67. return out.String()
  68. }
  69. // ReturnStatement fulfills the Node and Statement interfaces.
  70. type ReturnStatement struct {
  71. Token token.Token // the token.RETURN token. Why do we need it ?
  72. Name *Identifier
  73. ReturnValue Expression
  74. }
  75. func (rs *ReturnStatement) statementNode() {}
  76. // TokenLiteral satisfies the Node interface.
  77. func (rs *ReturnStatement) TokenLiteral() string {
  78. return rs.Token.Literal
  79. }
  80. // String satisfies the Node and fmt.Stringer interfaces.
  81. func (rs *ReturnStatement) String() string {
  82. var out bytes.Buffer
  83. out.WriteString(rs.TokenLiteral() + " ")
  84. if rs.ReturnValue != nil {
  85. out.WriteString(rs.ReturnValue.String())
  86. }
  87. out.WriteString(";")
  88. return out.String()
  89. }
  90. // ExpressionStatement fulfills the Node and Statement interfaces.
  91. // It represents a statement made of a bare expression like:
  92. // x + 10;
  93. type ExpressionStatement struct {
  94. Token token.Token // the first token of the expression
  95. Expression Expression
  96. }
  97. func (es *ExpressionStatement) statementNode() {}
  98. // TokenLiteral satisfies the Node interface.
  99. func (es *ExpressionStatement) TokenLiteral() string {
  100. return es.Token.Literal
  101. }
  102. // String satisfies the Node and fmt.Stringer interfaces.
  103. func (es *ExpressionStatement) String() string {
  104. if es.Expression != nil {
  105. return es.Expression.String()
  106. }
  107. return ""
  108. }
  109. // Identifier is the Node type for identifiers.
  110. type Identifier struct {
  111. Token token.Token // the token.IDENT token. Why do we need it ?
  112. Value string // The identifier string.
  113. }
  114. func (i *Identifier) expressionNode() {}
  115. // TokenLiteral satisfies the Node interface.
  116. func (i *Identifier) TokenLiteral() string {
  117. return i.Token.Literal
  118. }
  119. func (i *Identifier) String() string {
  120. return i.Value
  121. }
  122. // IntegerLiteral fulfills ast.Expression.
  123. type IntegerLiteral struct {
  124. Token token.Token
  125. Value int64
  126. }
  127. func (il *IntegerLiteral) expressionNode() {}
  128. // TokenLiteral satisfies the Node interface.
  129. func (il *IntegerLiteral) TokenLiteral() string {
  130. return il.Token.Literal
  131. }
  132. func (il *IntegerLiteral) String() string {
  133. return il.Token.Literal
  134. }
  135. // PrefixExpression fulfills the Node and Statement interfaces.
  136. // It represents a prefixed expression like:
  137. // "-5;"
  138. type PrefixExpression struct {
  139. Token token.Token // The prefix token, e.g. !
  140. Operator string
  141. Right Expression
  142. }
  143. func (pe *PrefixExpression) expressionNode() {}
  144. // TokenLiteral satisfies the Node interface.
  145. func (pe *PrefixExpression) TokenLiteral() string {
  146. return pe.Token.Literal
  147. }
  148. // String satisfies the Node and fmt.Stringer interfaces.
  149. func (pe *PrefixExpression) String() string {
  150. var out bytes.Buffer
  151. out.WriteString("(")
  152. out.WriteString(pe.Operator)
  153. out.WriteString(pe.Right.String())
  154. out.WriteString(")")
  155. return out.String()
  156. }
  157. // InfixExpression fulfills the Node and Statement interfaces.
  158. // It represents a prefixed expression like:
  159. // "-5;"
  160. type InfixExpression struct {
  161. Token token.Token // The operator token, e.g. +
  162. Left Expression
  163. Operator string
  164. Right Expression
  165. }
  166. func (ie *InfixExpression) expressionNode() {}
  167. // TokenLiteral satisfies the Node interface.
  168. func (ie *InfixExpression) TokenLiteral() string {
  169. return ie.Token.Literal
  170. }
  171. // String satisfies the Node and fmt.Stringer interfaces.
  172. func (ie *InfixExpression) String() string {
  173. var out bytes.Buffer
  174. out.WriteString("(")
  175. out.WriteString(ie.Left.String())
  176. out.WriteString(" " + ie.Operator + " ")
  177. out.WriteString(ie.Right.String())
  178. out.WriteString(")")
  179. return out.String()
  180. }