ast.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package ast
  2. import (
  3. "bytes"
  4. "github/runnignwater/monkey/token"
  5. "strings"
  6. )
  7. // Node @Author: simon
  8. // @Author: ynwdlxm@163.com
  9. // @Date: 2022/10/2 下午8:58
  10. type Node interface {
  11. TokenLiteral() string
  12. // String This will allow us to print AST nodes for debugging and to compare them with other AST nodes.
  13. // This is going to be really handy in tests!
  14. String() string
  15. }
  16. // Statement expressions produce values, statements don't
  17. type Statement interface {
  18. Node
  19. statementNode()
  20. }
  21. type Expression interface {
  22. Node
  23. expressionNode()
  24. }
  25. // ---------------------implementation of Node----------------------------------BEGIN-----------------------------------
  26. // Program Root Node of our parser produces.
  27. type Program struct {
  28. Statements []Statement
  29. }
  30. func (p *Program) TokenLiteral() string {
  31. if len(p.Statements) > 0 {
  32. return p.Statements[0].TokenLiteral()
  33. } else {
  34. return ""
  35. }
  36. }
  37. func (p *Program) String() string {
  38. var out bytes.Buffer
  39. for _, s := range p.Statements {
  40. out.WriteString(s.String())
  41. }
  42. return out.String()
  43. }
  44. //---------------------implementation of Node-----------------------------------END-------------------------------------
  45. // LetStatement let <identifier> = <expression>;
  46. type LetStatement struct {
  47. Token token.Token // the token.LET token
  48. Name *Identifier
  49. Value Expression
  50. }
  51. func (ls *LetStatement) TokenLiteral() string {
  52. return ls.Token.Literal
  53. }
  54. func (ls *LetStatement) String() string {
  55. var out bytes.Buffer
  56. out.WriteString(ls.TokenLiteral() + " ")
  57. out.WriteString(ls.Name.String())
  58. out.WriteString(" = ")
  59. if ls.Value != nil {
  60. out.WriteString(ls.Value.String())
  61. }
  62. out.WriteString(";")
  63. return out.String()
  64. }
  65. func (ls *LetStatement) statementNode() {
  66. panic("implement me")
  67. }
  68. type Identifier struct {
  69. Token token.Token // the token.IDENT token
  70. Value string
  71. }
  72. func (i *Identifier) expressionNode() {
  73. panic("implement me")
  74. }
  75. func (i *Identifier) String() string {
  76. return i.Value
  77. }
  78. func (i *Identifier) TokenLiteral() string {
  79. return i.Token.Literal
  80. }
  81. // ReturnStatement return <expression>;
  82. type ReturnStatement struct {
  83. Token token.Token // the token.RETURN
  84. ReturnValue Expression
  85. }
  86. func (rs *ReturnStatement) TokenLiteral() string {
  87. return rs.Token.Literal
  88. }
  89. func (rs *ReturnStatement) String() string {
  90. var out bytes.Buffer
  91. out.WriteString(rs.TokenLiteral() + " ")
  92. if rs.ReturnValue != nil {
  93. out.WriteString(rs.ReturnValue.String())
  94. }
  95. out.WriteString(";")
  96. return out.String()
  97. }
  98. func (rs *ReturnStatement) statementNode() {
  99. panic("implement me")
  100. }
  101. type ExpressionStatement struct {
  102. Token token.Token // the first token of the expression
  103. Expression Expression
  104. }
  105. func (es *ExpressionStatement) TokenLiteral() string {
  106. return es.Token.Literal
  107. }
  108. func (es *ExpressionStatement) String() string {
  109. if es.Expression != nil {
  110. return es.Expression.String()
  111. }
  112. return ""
  113. }
  114. func (es *ExpressionStatement) statementNode() {}
  115. // IntegerLiteral integer value e.g. 5 6 ...
  116. type IntegerLiteral struct {
  117. Token token.Token
  118. Value int64
  119. }
  120. func (i *IntegerLiteral) TokenLiteral() string { return i.Token.Literal }
  121. func (i *IntegerLiteral) String() string { return i.Token.Literal }
  122. func (i *IntegerLiteral) expressionNode() {}
  123. // PrefixExpression 前缀表示式 e.g. !a -6
  124. type PrefixExpression struct {
  125. Token token.Token // The prefix token, e.g. !
  126. Operator string
  127. Right Expression
  128. }
  129. func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
  130. func (pe *PrefixExpression) String() string {
  131. var out bytes.Buffer
  132. out.WriteString("(")
  133. out.WriteString(pe.Operator)
  134. out.WriteString(pe.Right.String())
  135. out.WriteString(")")
  136. return out.String()
  137. }
  138. func (pe *PrefixExpression) expressionNode() {}
  139. // InfixExpression <expression> <infix operator> <expression>
  140. type InfixExpression struct {
  141. Token token.Token // The operator token, e.g. + - * /
  142. Left Expression
  143. Operator string
  144. Right Expression
  145. }
  146. func (oe *InfixExpression) TokenLiteral() string { return oe.Token.Literal }
  147. func (oe *InfixExpression) String() string {
  148. var out bytes.Buffer
  149. out.WriteString("(")
  150. out.WriteString(oe.Left.String())
  151. out.WriteString(" " + oe.Operator + " ")
  152. out.WriteString(oe.Right.String())
  153. out.WriteString(")")
  154. return out.String()
  155. }
  156. func (oe *InfixExpression) expressionNode() {}
  157. // Boolean bool 表达式 e.g. true/false
  158. type Boolean struct {
  159. Token token.Token
  160. Value bool
  161. }
  162. func (b *Boolean) TokenLiteral() string {
  163. return b.Token.Literal
  164. }
  165. func (b *Boolean) String() string {
  166. return b.Token.Literal
  167. }
  168. func (b *Boolean) expressionNode() {}
  169. // IfExpression if (<condition>) <consequence> else <alternative>
  170. type IfExpression struct {
  171. Token token.Token // The 'if' token
  172. Condition Expression
  173. Consequence *BlockStatement
  174. Alternative *BlockStatement
  175. }
  176. func (ie *IfExpression) TokenLiteral() string {
  177. return ie.Token.Literal
  178. }
  179. func (ie *IfExpression) String() string {
  180. var out bytes.Buffer
  181. out.WriteString("if")
  182. out.WriteString(ie.Condition.String())
  183. out.WriteString(" ")
  184. out.WriteString(ie.Consequence.String())
  185. if ie.Alternative != nil {
  186. out.WriteString("else ")
  187. out.WriteString(ie.Alternative.String())
  188. }
  189. return out.String()
  190. }
  191. func (ie *IfExpression) expressionNode() {}
  192. type BlockStatement struct {
  193. Token token.Token // the { token
  194. Statements []Statement
  195. }
  196. func (bs *BlockStatement) TokenLiteral() string {
  197. return bs.Token.Literal
  198. }
  199. func (bs *BlockStatement) String() string {
  200. var out bytes.Buffer
  201. for _, s := range bs.Statements {
  202. out.WriteString(s.String())
  203. }
  204. return out.String()
  205. }
  206. func (bs *BlockStatement) statementNode() {}
  207. // FunctionLiteral fn <parameters> <block statement>
  208. type FunctionLiteral struct {
  209. Token token.Token // The 'fn' token
  210. Parameters []*Identifier
  211. Body *BlockStatement
  212. }
  213. func (fl *FunctionLiteral) TokenLiteral() string {
  214. return fl.Token.Literal
  215. }
  216. func (fl *FunctionLiteral) String() string {
  217. var out bytes.Buffer
  218. params := []string{}
  219. for _, p := range fl.Parameters {
  220. params = append(params, p.String())
  221. }
  222. out.WriteString(fl.TokenLiteral())
  223. out.WriteString("(")
  224. out.WriteString(strings.Join(params, ", "))
  225. out.WriteString(")")
  226. out.WriteString(fl.Body.String())
  227. return out.String()
  228. }
  229. func (fl *FunctionLiteral) expressionNode() {}
  230. // CallExpression <expression>(<comma separated expressions>);
  231. type CallExpression struct {
  232. Token token.Token // the '(' token
  233. Function Expression
  234. Arguments []Expression
  235. }
  236. func (ce *CallExpression) TokenLiteral() string {
  237. return ce.Token.Literal
  238. }
  239. func (ce *CallExpression) String() string {
  240. var out bytes.Buffer
  241. args := []string{}
  242. for _, a := range ce.Arguments {
  243. args = append(args, a.String())
  244. }
  245. out.WriteString(ce.Function.String())
  246. out.WriteString("(")
  247. out.WriteString(strings.Join(args, ", "))
  248. out.WriteString(")")
  249. return out.String()
  250. }
  251. func (ce *CallExpression) expressionNode() {}