ast.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. type StringLiteral struct {
  120. Token token.Token
  121. Value string
  122. }
  123. func (sl *StringLiteral) TokenLiteral() string { return sl.Token.Literal }
  124. func (sl *StringLiteral) String() string { return sl.Token.Literal }
  125. func (sl *StringLiteral) expressionNode() {}
  126. // PrefixExpression 前缀表示式 e.g. !a -6
  127. type PrefixExpression struct {
  128. Token token.Token // The prefix token, e.g. !
  129. Operator string
  130. Right Expression
  131. }
  132. func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
  133. func (pe *PrefixExpression) String() string {
  134. var out bytes.Buffer
  135. out.WriteString("(")
  136. out.WriteString(pe.Operator)
  137. out.WriteString(pe.Right.String())
  138. out.WriteString(")")
  139. return out.String()
  140. }
  141. func (pe *PrefixExpression) expressionNode() {}
  142. // InfixExpression <expression> <infix operator> <expression>
  143. type InfixExpression struct {
  144. Token token.Token // The operator token, e.g. + - * /
  145. Left Expression
  146. Operator string
  147. Right Expression
  148. }
  149. func (oe *InfixExpression) TokenLiteral() string { return oe.Token.Literal }
  150. func (oe *InfixExpression) String() string {
  151. var out bytes.Buffer
  152. out.WriteString("(")
  153. out.WriteString(oe.Left.String())
  154. out.WriteString(" " + oe.Operator + " ")
  155. out.WriteString(oe.Right.String())
  156. out.WriteString(")")
  157. return out.String()
  158. }
  159. func (oe *InfixExpression) expressionNode() {}
  160. // Boolean bool 表达式 e.g. true/false
  161. type Boolean struct {
  162. Token token.Token
  163. Value bool
  164. }
  165. func (b *Boolean) TokenLiteral() string {
  166. return b.Token.Literal
  167. }
  168. func (b *Boolean) String() string {
  169. return b.Token.Literal
  170. }
  171. func (b *Boolean) expressionNode() {}
  172. // IfExpression if (<condition>) <consequence> else <alternative>
  173. type IfExpression struct {
  174. Token token.Token // The 'if' token
  175. Condition Expression
  176. Consequence *BlockStatement
  177. Alternative *BlockStatement
  178. }
  179. func (ie *IfExpression) TokenLiteral() string {
  180. return ie.Token.Literal
  181. }
  182. func (ie *IfExpression) String() string {
  183. var out bytes.Buffer
  184. out.WriteString("if")
  185. out.WriteString(ie.Condition.String())
  186. out.WriteString(" ")
  187. out.WriteString(ie.Consequence.String())
  188. if ie.Alternative != nil {
  189. out.WriteString("else ")
  190. out.WriteString(ie.Alternative.String())
  191. }
  192. return out.String()
  193. }
  194. func (ie *IfExpression) expressionNode() {}
  195. type BlockStatement struct {
  196. Token token.Token // the { token
  197. Statements []Statement
  198. }
  199. func (bs *BlockStatement) TokenLiteral() string {
  200. return bs.Token.Literal
  201. }
  202. func (bs *BlockStatement) String() string {
  203. var out bytes.Buffer
  204. for _, s := range bs.Statements {
  205. out.WriteString(s.String())
  206. }
  207. return out.String()
  208. }
  209. func (bs *BlockStatement) statementNode() {}
  210. // FunctionLiteral fn <parameters> <block statement>
  211. type FunctionLiteral struct {
  212. Token token.Token // The 'fn' token
  213. Parameters []*Identifier
  214. Body *BlockStatement
  215. }
  216. func (fl *FunctionLiteral) TokenLiteral() string {
  217. return fl.Token.Literal
  218. }
  219. func (fl *FunctionLiteral) String() string {
  220. var out bytes.Buffer
  221. params := []string{}
  222. for _, p := range fl.Parameters {
  223. params = append(params, p.String())
  224. }
  225. out.WriteString(fl.TokenLiteral())
  226. out.WriteString("(")
  227. out.WriteString(strings.Join(params, ", "))
  228. out.WriteString(")")
  229. out.WriteString(fl.Body.String())
  230. return out.String()
  231. }
  232. func (fl *FunctionLiteral) expressionNode() {}
  233. // CallExpression <expression>(<comma separated expressions>);
  234. type CallExpression struct {
  235. Token token.Token // the '(' token
  236. Function Expression
  237. Arguments []Expression
  238. }
  239. func (ce *CallExpression) TokenLiteral() string {
  240. return ce.Token.Literal
  241. }
  242. func (ce *CallExpression) String() string {
  243. var out bytes.Buffer
  244. args := []string{}
  245. for _, a := range ce.Arguments {
  246. args = append(args, a.String())
  247. }
  248. out.WriteString(ce.Function.String())
  249. out.WriteString("(")
  250. out.WriteString(strings.Join(args, ", "))
  251. out.WriteString(")")
  252. return out.String()
  253. }
  254. func (ce *CallExpression) expressionNode() {}