ast.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package ast
  2. import (
  3. "bytes"
  4. "github/runnignwater/monkey/token"
  5. )
  6. /**
  7. * @Author: simon
  8. * @Author: ynwdlxm@163.com
  9. * @Date: 2022/10/2 下午8:58
  10. * @Desc:
  11. */
  12. type Node interface {
  13. TokenLiteral() string
  14. // This will allow us to print AST nodes for debugging and to compare them with other AST nodes.
  15. // This is going to be really handy in tests!
  16. String() string
  17. }
  18. // expressions produce values, statements don't
  19. type Statement interface {
  20. Node
  21. statementNode()
  22. }
  23. type Expression interface {
  24. Node
  25. expressionNode()
  26. }
  27. // ---------------------implementation of Node----------------------------------BEGIN-----------------------------------
  28. // Root Node of our parser produces.
  29. type Program struct {
  30. Statements []Statement
  31. }
  32. func (p *Program) TokenLiteral() string {
  33. if len(p.Statements) > 0 {
  34. return p.Statements[0].TokenLiteral()
  35. } else {
  36. return ""
  37. }
  38. }
  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. //---------------------implementation of Node-----------------------------------END-------------------------------------
  47. // let <identifier> = <expression>;
  48. //
  49. // let x = 5 AST
  50. // |-------------------|
  51. // | *ast.Program |
  52. // |-------------------|
  53. // | Statements |
  54. // |-------------------|
  55. // ↓
  56. // |-------------------|
  57. // | *ast.LetStatement |
  58. // |-------------------|
  59. // | Name |
  60. // |-------------------|
  61. // | Value |
  62. // |-------------------|
  63. //
  64. // *ast.Identifier *ast.Expression
  65. //
  66. type LetStatement struct {
  67. Token token.Token // the token.LET token
  68. Name *Identifier
  69. Value Expression
  70. }
  71. func (ls *LetStatement) TokenLiteral() string {
  72. return ls.Token.Literal
  73. }
  74. func (ls *LetStatement) String() string {
  75. var out bytes.Buffer
  76. out.WriteString(ls.TokenLiteral() + " ")
  77. out.WriteString(ls.Name.String())
  78. out.WriteString(" = ")
  79. if ls.Value != nil {
  80. out.WriteString(ls.Value.String())
  81. }
  82. out.WriteString(";")
  83. return out.String()
  84. }
  85. func (ls *LetStatement) statementNode() {
  86. panic("implement me")
  87. }
  88. type Identifier struct {
  89. Token token.Token // the token.IDENT token
  90. Value string
  91. }
  92. func (i *Identifier) expressionNode() {
  93. panic("implement me")
  94. }
  95. func (i *Identifier) String() string {
  96. return i.Value
  97. }
  98. func (i *Identifier) TokenLiteral() string {
  99. return i.Token.Literal
  100. }
  101. // return <expression>;
  102. type ReturnStatement struct {
  103. Token token.Token // the token.RETURN
  104. returnValue Expression
  105. }
  106. func (rs *ReturnStatement) TokenLiteral() string {
  107. return rs.Token.Literal
  108. }
  109. func (rs *ReturnStatement) String() string {
  110. var out bytes.Buffer
  111. out.WriteString(rs.TokenLiteral() + " ")
  112. if rs.returnValue != nil {
  113. out.WriteString(rs.returnValue.String())
  114. }
  115. out.WriteString(";")
  116. return out.String()
  117. }
  118. func (rs *ReturnStatement) statementNode() {
  119. panic("implement me")
  120. }
  121. type ExpressionStatement struct {
  122. Token token.Token // the first token of the expression
  123. Expression Expression
  124. }
  125. func (es *ExpressionStatement) TokenLiteral() string {
  126. return es.Token.Literal
  127. }
  128. func (es *ExpressionStatement) String() string {
  129. if es.Expression != nil {
  130. return es.Expression.String()
  131. }
  132. return ""
  133. }
  134. func (es *ExpressionStatement) statementNode() {}
  135. type IntegerLiteral struct {
  136. Token token.Token
  137. Value int64
  138. }
  139. func (i *IntegerLiteral) TokenLiteral() string { return i.Token.Literal }
  140. func (i *IntegerLiteral) String() string { return i.Token.Literal }
  141. func (i *IntegerLiteral) expressionNode() {}
  142. type PrefixExpression struct {
  143. Token token.Token // The prefix token, e.g. !
  144. Operator string
  145. Right Expression
  146. }
  147. func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
  148. func (pe *PrefixExpression) String() string {
  149. var out bytes.Buffer
  150. out.WriteString("(")
  151. out.WriteString(pe.Operator)
  152. out.WriteString(pe.Right.String())
  153. out.WriteString(")")
  154. return out.String()
  155. }
  156. func (pe *PrefixExpression) expressionNode() {}
  157. // <expression> <infix operator> <expression>
  158. type InfixExpression struct {
  159. Token token.Token // The operator token, e.g. + - * /
  160. Left Expression
  161. Operator string
  162. Right Expression
  163. }
  164. func (oe *InfixExpression) TokenLiteral() string { return oe.Token.Literal }
  165. func (oe *InfixExpression) String() string {
  166. var out bytes.Buffer
  167. out.WriteString("(")
  168. out.WriteString(oe.Left.String())
  169. out.WriteString(" " + oe.Operator + " ")
  170. out.WriteString(oe.Right.String())
  171. out.WriteString(")")
  172. return out.String()
  173. }
  174. func (oe *InfixExpression) expressionNode() {}
  175. type Boolean struct {
  176. Token token.Token
  177. Value bool
  178. }
  179. func (b *Boolean) TokenLiteral() string {
  180. return b.Token.Literal
  181. }
  182. func (b *Boolean) String() string {
  183. return b.Token.Literal
  184. }
  185. func (b *Boolean) expressionNode() {}