ast.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 { return ls.Token.Literal }
  52. func (ls *LetStatement) String() string {
  53. var out bytes.Buffer
  54. out.WriteString(ls.TokenLiteral() + " ")
  55. out.WriteString(ls.Name.String())
  56. out.WriteString(" = ")
  57. if ls.Value != nil {
  58. out.WriteString(ls.Value.String())
  59. }
  60. out.WriteString(";")
  61. return out.String()
  62. }
  63. func (ls *LetStatement) statementNode() {}
  64. type Identifier struct {
  65. Token token.Token // the token.IDENT token
  66. Value string
  67. }
  68. func (i *Identifier) expressionNode() {
  69. panic("implement me")
  70. }
  71. func (i *Identifier) String() string {
  72. return i.Value
  73. }
  74. func (i *Identifier) TokenLiteral() string {
  75. return i.Token.Literal
  76. }
  77. // ReturnStatement return <expression>;
  78. type ReturnStatement struct {
  79. Token token.Token // the token.RETURN
  80. ReturnValue Expression
  81. }
  82. func (rs *ReturnStatement) TokenLiteral() string {
  83. return rs.Token.Literal
  84. }
  85. func (rs *ReturnStatement) String() string {
  86. var out bytes.Buffer
  87. out.WriteString(rs.TokenLiteral() + " ")
  88. if rs.ReturnValue != nil {
  89. out.WriteString(rs.ReturnValue.String())
  90. }
  91. out.WriteString(";")
  92. return out.String()
  93. }
  94. func (rs *ReturnStatement) statementNode() {
  95. panic("implement me")
  96. }
  97. type ExpressionStatement struct {
  98. Token token.Token // the first token of the expression
  99. Expression Expression
  100. }
  101. func (es *ExpressionStatement) TokenLiteral() string {
  102. return es.Token.Literal
  103. }
  104. func (es *ExpressionStatement) String() string {
  105. if es.Expression != nil {
  106. return es.Expression.String()
  107. }
  108. return ""
  109. }
  110. func (es *ExpressionStatement) statementNode() {}
  111. // IntegerLiteral integer value e.g. 5 6 ...
  112. type IntegerLiteral struct {
  113. Token token.Token
  114. Value int64
  115. }
  116. func (i *IntegerLiteral) TokenLiteral() string { return i.Token.Literal }
  117. func (i *IntegerLiteral) String() string { return i.Token.Literal }
  118. func (i *IntegerLiteral) expressionNode() {}
  119. // StringLiteral 字符串表达式
  120. type StringLiteral struct {
  121. Token token.Token
  122. Value string
  123. }
  124. func (sl *StringLiteral) TokenLiteral() string { return sl.Token.Literal }
  125. func (sl *StringLiteral) String() string { return sl.Token.Literal }
  126. func (sl *StringLiteral) expressionNode() {}
  127. // PrefixExpression 前缀表示式 e.g. !a -6
  128. type PrefixExpression struct {
  129. Token token.Token // The prefix token, e.g. !
  130. Operator string
  131. Right Expression
  132. }
  133. func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
  134. func (pe *PrefixExpression) String() string {
  135. var out bytes.Buffer
  136. out.WriteString("(")
  137. out.WriteString(pe.Operator)
  138. out.WriteString(pe.Right.String())
  139. out.WriteString(")")
  140. return out.String()
  141. }
  142. func (pe *PrefixExpression) expressionNode() {}
  143. // InfixExpression <expression> <infix operator> <expression>
  144. type InfixExpression struct {
  145. Token token.Token // The operator token, e.g. + - * /
  146. Left Expression
  147. Operator string
  148. Right Expression
  149. }
  150. func (oe *InfixExpression) TokenLiteral() string { return oe.Token.Literal }
  151. func (oe *InfixExpression) String() string {
  152. var out bytes.Buffer
  153. out.WriteString("(")
  154. out.WriteString(oe.Left.String())
  155. out.WriteString(" " + oe.Operator + " ")
  156. out.WriteString(oe.Right.String())
  157. out.WriteString(")")
  158. return out.String()
  159. }
  160. func (oe *InfixExpression) expressionNode() {}
  161. // Boolean bool 表达式 e.g. true/false
  162. type Boolean struct {
  163. Token token.Token
  164. Value bool
  165. }
  166. func (b *Boolean) TokenLiteral() string {
  167. return b.Token.Literal
  168. }
  169. func (b *Boolean) String() string {
  170. return b.Token.Literal
  171. }
  172. func (b *Boolean) expressionNode() {}
  173. // IfExpression if (<condition>) <consequence> else <alternative>
  174. type IfExpression struct {
  175. Token token.Token // The 'if' token
  176. Condition Expression
  177. Consequence *BlockStatement
  178. Alternative *BlockStatement
  179. }
  180. func (ie *IfExpression) TokenLiteral() string {
  181. return ie.Token.Literal
  182. }
  183. func (ie *IfExpression) String() string {
  184. var out bytes.Buffer
  185. out.WriteString("if")
  186. out.WriteString(ie.Condition.String())
  187. out.WriteString(" ")
  188. out.WriteString(ie.Consequence.String())
  189. if ie.Alternative != nil {
  190. out.WriteString("else ")
  191. out.WriteString(ie.Alternative.String())
  192. }
  193. return out.String()
  194. }
  195. func (ie *IfExpression) expressionNode() {}
  196. // BlockStatement {<expression>...}
  197. type BlockStatement struct {
  198. Token token.Token // the { token
  199. Statements []Statement
  200. }
  201. func (bs *BlockStatement) TokenLiteral() string {
  202. return bs.Token.Literal
  203. }
  204. func (bs *BlockStatement) String() string {
  205. var out bytes.Buffer
  206. for _, s := range bs.Statements {
  207. out.WriteString(s.String())
  208. }
  209. return out.String()
  210. }
  211. func (bs *BlockStatement) statementNode() {}
  212. // FunctionLiteral fn <parameters> <block statement>
  213. type FunctionLiteral struct {
  214. Token token.Token // The 'fn' token
  215. Parameters []*Identifier
  216. Body *BlockStatement
  217. }
  218. func (fl *FunctionLiteral) TokenLiteral() string {
  219. return fl.Token.Literal
  220. }
  221. func (fl *FunctionLiteral) String() string {
  222. var out bytes.Buffer
  223. params := []string{}
  224. for _, p := range fl.Parameters {
  225. params = append(params, p.String())
  226. }
  227. out.WriteString(fl.TokenLiteral())
  228. out.WriteString("(")
  229. out.WriteString(strings.Join(params, ", "))
  230. out.WriteString(")")
  231. out.WriteString(fl.Body.String())
  232. return out.String()
  233. }
  234. func (fl *FunctionLiteral) expressionNode() {}
  235. // CallExpression <expression>(<comma separated expressions>);
  236. type CallExpression struct {
  237. Token token.Token // the '(' token
  238. Function Expression
  239. Arguments []Expression
  240. }
  241. func (ce *CallExpression) TokenLiteral() string {
  242. return ce.Token.Literal
  243. }
  244. func (ce *CallExpression) String() string {
  245. var out bytes.Buffer
  246. args := []string{}
  247. for _, a := range ce.Arguments {
  248. args = append(args, a.String())
  249. }
  250. out.WriteString(ce.Function.String())
  251. out.WriteString("(")
  252. out.WriteString(strings.Join(args, ", "))
  253. out.WriteString(")")
  254. return out.String()
  255. }
  256. func (ce *CallExpression) expressionNode() {}
  257. // ArrayLiteral 数组表达式 [<expression>]
  258. type ArrayLiteral struct {
  259. Token token.Token // the '[' token
  260. Element []Expression
  261. }
  262. func (al *ArrayLiteral) TokenLiteral() string { return al.Token.Literal }
  263. func (al *ArrayLiteral) String() string {
  264. var out bytes.Buffer
  265. elements := []string{}
  266. for _, el := range al.Element {
  267. elements = append(elements, el.String())
  268. }
  269. out.WriteString("[")
  270. out.WriteString(strings.Join(elements, ", "))
  271. out.WriteString("]")
  272. return out.String()
  273. }
  274. func (al *ArrayLiteral) expressionNode() {}
  275. // IndexExpression <expression>[<expression>]
  276. // e.g let myArray = [1,2,3,4]
  277. //
  278. // myArray[2];
  279. type IndexExpression struct {
  280. Token token.Token // the [ token
  281. Left Expression
  282. Index Expression
  283. }
  284. func (ie *IndexExpression) TokenLiteral() string { return ie.Token.Literal }
  285. func (ie *IndexExpression) String() string {
  286. var out bytes.Buffer
  287. out.WriteString("(")
  288. out.WriteString(ie.Left.String())
  289. out.WriteString("[")
  290. out.WriteString(ie.Index.String())
  291. out.WriteString("])")
  292. return out.String()
  293. }
  294. func (ie *IndexExpression) expressionNode() {}
  295. // HashLiteral {<expression> : <expression>, <expression> : <expression>, ...}
  296. type HashLiteral struct {
  297. Token token.Token // the '{' token
  298. Pairs map[Expression]Expression
  299. }
  300. func (hl *HashLiteral) TokenLiteral() string { return hl.Token.Literal }
  301. func (hl *HashLiteral) String() string {
  302. var out bytes.Buffer
  303. pairs := []string{}
  304. for key, value := range hl.Pairs {
  305. pairs = append(pairs, key.String()+" : "+value.String())
  306. }
  307. out.WriteString("{")
  308. out.WriteString(strings.Join(pairs, ", "))
  309. out.WriteString("}")
  310. return out.String()
  311. }
  312. func (hl *HashLiteral) expressionNode() {}