ast.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package ast
  2. import (
  3. "bytes"
  4. "github/runnignwater/monkey/token"
  5. "strings"
  6. )
  7. /**
  8. * @Author: simon
  9. * @Author: ynwdlxm@163.com
  10. * @Date: 2022/10/2 下午8:58
  11. * @Desc:
  12. */
  13. type Node interface {
  14. TokenLiteral() string
  15. // String This will allow us to print AST nodes for debugging and to compare them with other AST nodes.
  16. // This is going to be really handy in tests!
  17. String() string
  18. }
  19. // Statement expressions produce values, statements don't
  20. type Statement interface {
  21. Node
  22. statementNode()
  23. }
  24. type Expression interface {
  25. Node
  26. expressionNode()
  27. }
  28. // ---------------------implementation of Node----------------------------------BEGIN-----------------------------------
  29. // Root Node of our parser produces.
  30. type Program struct {
  31. Statements []Statement
  32. }
  33. func (p *Program) TokenLiteral() string {
  34. if len(p.Statements) > 0 {
  35. return p.Statements[0].TokenLiteral()
  36. } else {
  37. return ""
  38. }
  39. }
  40. func (p *Program) String() string {
  41. var out bytes.Buffer
  42. for _, s := range p.Statements {
  43. out.WriteString(s.String())
  44. }
  45. return out.String()
  46. }
  47. //---------------------implementation of Node-----------------------------------END-------------------------------------
  48. // let <identifier> = <expression>;
  49. //
  50. // let x = 5 AST
  51. // |-------------------|
  52. // | *ast.Program |
  53. // |-------------------|
  54. // | Statements |
  55. // |-------------------|
  56. // ↓
  57. // |-------------------|
  58. // | *ast.LetStatement |
  59. // |-------------------|
  60. // | Name |
  61. // |-------------------|
  62. // | Value |
  63. // |-------------------|
  64. //
  65. // *ast.Identifier *ast.Expression
  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. // ReturnStatement 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() {}
  186. // IfExpression if (<condition>) <consequence> else <alternative>
  187. type IfExpression struct {
  188. Token token.Token // The 'if' token
  189. Condition Expression
  190. Consequence *BlockStatement
  191. Alternative *BlockStatement
  192. }
  193. func (ie *IfExpression) TokenLiteral() string {
  194. return ie.Token.Literal
  195. }
  196. func (ie *IfExpression) String() string {
  197. var out bytes.Buffer
  198. out.WriteString("if")
  199. out.WriteString(ie.Condition.String())
  200. out.WriteString(" ")
  201. out.WriteString(ie.Consequence.String())
  202. if ie.Alternative != nil {
  203. out.WriteString("else ")
  204. out.WriteString(ie.Alternative.String())
  205. }
  206. return out.String()
  207. }
  208. func (ie *IfExpression) expressionNode() {}
  209. type BlockStatement struct {
  210. Token token.Token // the { token
  211. Statements []Statement
  212. }
  213. func (bs *BlockStatement) TokenLiteral() string {
  214. return bs.Token.Literal
  215. }
  216. func (bs *BlockStatement) String() string {
  217. var out bytes.Buffer
  218. for _, s := range bs.Statements {
  219. out.WriteString(s.String())
  220. }
  221. return out.String()
  222. }
  223. func (bs *BlockStatement) statementNode() {}
  224. type FunctionLiteral struct {
  225. Token token.Token // The 'fn' token
  226. Parameters []*Identifier
  227. Body *BlockStatement
  228. }
  229. func (fl *FunctionLiteral) TokenLiteral() string {
  230. return fl.Token.Literal
  231. }
  232. func (fl *FunctionLiteral) String() string {
  233. var out bytes.Buffer
  234. params := []string{}
  235. for _, p := range fl.Parameters {
  236. params = append(params, p.String())
  237. }
  238. out.WriteString(fl.TokenLiteral())
  239. out.WriteString("(")
  240. out.WriteString(strings.Join(params, ", "))
  241. out.WriteString(")")
  242. out.WriteString(fl.Body.String())
  243. return out.String()
  244. }
  245. func (fl *FunctionLiteral) expressionNode() {}