ast.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // Abstract Syntax tree for the MUESLI interpreter
  2. package muesli
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. type AstBasicMetaKind string
  8. type AstMetaKindNone AstBasicMetaKind
  9. type AstMetaKindProgram AstBasicMetaKind
  10. type AstMetaKindStatements AstBasicMetaKind
  11. type AstMetaKindStatement AstBasicMetaKind
  12. type AstMetaKindClosed AstBasicMetaKind
  13. type AstMetaKindSet AstBasicMetaKind
  14. type AstMetaKindGet AstBasicMetaKind
  15. type AstMetaKindTarget AstBasicMetaKind
  16. type AstMetaKindCommand AstBasicMetaKind
  17. type AstMetaKindArguments AstBasicMetaKind
  18. type AstMetaKindArgument AstBasicMetaKind
  19. type AstMetaKindExpression AstBasicMetaKind
  20. type AstMetaKindBlock AstBasicMetaKind
  21. type AstMetaKindParenthesis AstBasicMetaKind
  22. type AstMetaKindList AstBasicMetaKind
  23. type AstMetaKindCapture AstBasicMetaKind
  24. type AstMetaKindWordValue AstBasicMetaKind
  25. type AstMetaKindWord AstBasicMetaKind
  26. type AstMetaKindType AstBasicMetaKind
  27. type AstMetaKindValue AstBasicMetaKind
  28. type AstMetaKindEnd AstBasicMetaKind
  29. type AstMetaKindError AstBasicMetaKind
  30. type AstMetaKindFlatten AstBasicMetaKind
  31. /** The actual types are defined as constants, the meta types are used to be able to have different behavior for them.*/
  32. const (
  33. AstKindNone = AstMetaKindNone("None")
  34. AstKindProgram = AstMetaKindProgram("Program")
  35. AstKindStatements = AstMetaKindStatements("Statements")
  36. AstKindStatement = AstMetaKindStatement("Statement")
  37. AstKindClosed = AstMetaKindSet("Closed")
  38. AstKindSet = AstMetaKindSet("Set")
  39. AstKindGet = AstMetaKindGet("Get")
  40. AstKindTarget = AstMetaKindTarget("Target")
  41. AstKindCommand = AstMetaKindCommand("Command")
  42. AstKindArguments = AstMetaKindArguments("Arguments")
  43. AstKindArgument = AstMetaKindArgument("Argument")
  44. AstKindExpression = AstMetaKindExpression("Expression")
  45. AstKindBlock = AstMetaKindBlock("Block")
  46. AstKindParenthesis = AstMetaKindParenthesis("Parenthesis")
  47. AstKindList = AstMetaKindList("List")
  48. AstKindCapture = AstMetaKindCapture("Capture")
  49. AstKindWordValue = AstMetaKindWordValue("WordValue")
  50. AstKindWord = AstMetaKindWord("Word")
  51. AstKindType = AstMetaKindType("Type")
  52. AstKindValue = AstMetaKindValue("Value")
  53. AstKindEnd = AstMetaKindEnd("End")
  54. AstKindError = AstMetaKindError("Error")
  55. // AstKindFlatten is a special ast kind that insruct the parser not to generate a token
  56. AstKindFlatten = AstMetaKindFlatten("Flatten")
  57. )
  58. func (astkind AstBasicMetaKind) String() string {
  59. return string(astkind)
  60. }
  61. func (astkind AstBasicMetaKind) Run(vm *VM, ast Ast, val []Value) []Value {
  62. return EmptyValueArray()
  63. }
  64. func (astkind AstMetaKindNone) String() string { return "AstNone " }
  65. func (astkind AstMetaKindProgram) String() string { return "AstProgram " }
  66. func (astkind AstMetaKindStatements) String() string { return "AstStatements " }
  67. func (astkind AstMetaKindStatement) String() string { return "AstStatement " }
  68. func (astkind AstMetaKindClosed) String() string { return "AstClosed " }
  69. func (astkind AstMetaKindSet) String() string { return "AstSet " }
  70. func (astkind AstMetaKindGet) String() string { return "AstGet " }
  71. func (astkind AstMetaKindTarget) String() string { return "AstTarget " }
  72. func (astkind AstMetaKindCommand) String() string { return "AstCommand " }
  73. func (astkind AstMetaKindArguments) String() string { return "AstArguments " }
  74. func (astkind AstMetaKindArgument) String() string { return "AstArgument " }
  75. func (astkind AstMetaKindExpression) String() string { return "AstExpression " }
  76. func (astkind AstMetaKindBlock) String() string { return "AstBlock " }
  77. func (astkind AstMetaKindParenthesis) String() string { return "AstParenthesis" }
  78. func (astkind AstMetaKindList) String() string { return "AstList " }
  79. func (astkind AstMetaKindCapture) String() string { return "AstCapture " }
  80. func (astkind AstMetaKindWordValue) String() string { return "AstWordValue " }
  81. func (astkind AstMetaKindWord) String() string { return "AstWord " }
  82. func (astkind AstMetaKindType) String() string { return "AstType " }
  83. func (astkind AstMetaKindValue) String() string { return "AstValue " }
  84. func (astkind AstMetaKindEnd) String() string { return "AstEnd " }
  85. func (astkind AstMetaKindError) String() string { return "AstError " }
  86. func (astkind AstMetaKindFlatten) String() string { return "AstFlatten " }
  87. func (astkind AstMetaKindNone) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  88. func (astkind AstMetaKindProgram) Run(vm *VM, ast Ast, val []Value) []Value {
  89. res := EmptyValueArray()
  90. for _, child := range ast.Children() {
  91. res = append(res, child.Run(vm, val)...)
  92. }
  93. return res
  94. }
  95. func (astkind AstMetaKindStatements) Run(vm *VM, ast Ast, val []Value) []Value {
  96. return EmptyValueArray()
  97. }
  98. func (astkind AstMetaKindStatement) Run(vm *VM, ast Ast, val []Value) []Value {
  99. return EmptyValueArray()
  100. }
  101. func (astkind AstMetaKindClosed) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  102. func (astkind AstMetaKindSet) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  103. func (astkind AstMetaKindGet) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  104. func (astkind AstMetaKindTarget) Run(vm *VM, ast Ast, val []Value) []Value {
  105. return EmptyValueArray()
  106. }
  107. func (astkind AstMetaKindCommand) Run(vm *VM, ast Ast, val []Value) []Value {
  108. return EmptyValueArray()
  109. }
  110. func (astkind AstMetaKindArguments) Run(vm *VM, ast Ast, val []Value) []Value {
  111. return EmptyValueArray()
  112. }
  113. func (astkind AstMetaKindArgument) Run(vm *VM, ast Ast, val []Value) []Value {
  114. return EmptyValueArray()
  115. }
  116. func (astkind AstMetaKindExpression) Run(vm *VM, ast Ast, val []Value) []Value {
  117. return EmptyValueArray()
  118. }
  119. func (astkind AstMetaKindBlock) Run(vm *VM, ast Ast, val []Value) []Value {
  120. return EmptyValueArray()
  121. }
  122. func (astkind AstMetaKindParenthesis) Run(vm *VM, ast Ast, val []Value) []Value {
  123. return EmptyValueArray()
  124. }
  125. func (astkind AstMetaKindList) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  126. func (astkind AstMetaKindCapture) Run(vm *VM, ast Ast, val []Value) []Value {
  127. return EmptyValueArray()
  128. }
  129. func (astkind AstMetaKindWordValue) Run(vm *VM, ast Ast, val []Value) []Value {
  130. return EmptyValueArray()
  131. }
  132. func (astkind AstMetaKindWord) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  133. func (astkind AstMetaKindType) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  134. func (astkind AstMetaKindValue) Run(vm *VM, ast Ast, val []Value) []Value {
  135. return EmptyValueArray()
  136. }
  137. func (astkind AstMetaKindEnd) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
  138. func (astkind AstMetaKindError) Run(vm *VM, ast Ast, val []Value) []Value {
  139. return EmptyValueArray()
  140. }
  141. func (astkind AstMetaKindFlatten) Run(vm *VM, ast Ast, val []Value) []Value {
  142. return EmptyValueArray()
  143. }
  144. type AstKind interface {
  145. String() string
  146. Run(vm *VM, ast Ast, val []Value) []Value
  147. }
  148. /*
  149. func (Ast Ast) String() string {
  150. switch astkind {
  151. case AstMetaKindProgram:
  152. return "AstMetaKindProgram"
  153. case AstMetaKindStatements:
  154. return "AstMetaKindStatements"
  155. case AstMetaKindStatement:
  156. return "AstMetaKindStatement"
  157. case AstMetaKindSet:
  158. return "AstMetaKindSet"
  159. case AstMetaKindGet:
  160. return "AstMetaKindGet"
  161. case AstMetaKindTarget:
  162. return "AstMetaKindTarget"
  163. case AstMetaKindCommand:
  164. return "AstMetaKindCommand"
  165. case AstMetaKindArguments:
  166. return "AstMetaKindArguments"
  167. case AstMetaKindArgument:
  168. return "AstMetaKindArgument"
  169. case AstMetaKindExpression:
  170. return "AstMetaKindExpression"
  171. case AstMetaKindBlock:
  172. return "AstMetaKindBlock"
  173. case AstMetaKindParenthesis:
  174. return "AstMetaKindParenthesis"
  175. case AstMetaKindList:
  176. return "AstMetaKindList"
  177. case AstMetaKindCapture:
  178. return "AstMetaKindCapture"
  179. case AstMetaKindWordValue:
  180. return "AstMetaKindWordValue"
  181. case AstMetaKindWord:
  182. return "AstMetaKindWord"
  183. case AstMetaKindType:
  184. return "AstMetaKindType"
  185. case AstMetaKindValue:
  186. return "AstMetaKindValue"
  187. case AstMetaKindEnd:
  188. return "AstMetaKindEnd"
  189. case AstMetaKindError:
  190. return "AstMetaKindError"
  191. default:
  192. return "Unknown AstMetaKind"
  193. }
  194. }
  195. */
  196. /*
  197. func AstAppendChild(parent Ast, child Ast) Ast {
  198. basicParent := parent.(*Ast)
  199. return basicParent.AppendChild(child)
  200. }
  201. func AstNewChild(parent Ast, kind AstMetaKind, token Token) Ast {
  202. basicParent := parent.(*Ast)
  203. return basicParent.NewChild(kind, token)
  204. }
  205. */
  206. /* AST node kind */
  207. type Ast struct {
  208. AstKind
  209. parent *Ast
  210. children []*Ast
  211. token Token
  212. }
  213. func NewAst(kind AstKind, parent *Ast, children []*Ast, token Token) *Ast {
  214. ast := &Ast{AstKind: kind, parent: parent, token: token}
  215. return ast.AppendChildren(children...)
  216. }
  217. func (ast *Ast) AppendChildren(children ...*Ast) *Ast {
  218. for _, child := range children {
  219. ast.AppendChild(child)
  220. }
  221. return ast
  222. }
  223. func (ast *Ast) AppendChild(child * Ast) *Ast {
  224. child.SetParent(ast)
  225. ast.children = append(ast.children, child)
  226. return child
  227. }
  228. func (ast *Ast) NewChild(kind AstKind, token Token) *Ast {
  229. child := NewAst(kind, ast, make([]*Ast, 0), token)
  230. ast.AppendChild(child)
  231. return child
  232. }
  233. func (ast Ast) IsKind(astkind AstKind) bool {
  234. return ast.AstKind == astkind
  235. }
  236. func (ast Ast) IsError() bool {
  237. return ast.AstKind == AstKindError
  238. }
  239. func (ast Ast) IsNone() bool {
  240. return ast.AstKind == AstKindNone
  241. }
  242. func (ast Ast) IsFlatten() bool {
  243. return ast.AstKind == AstKindFlatten
  244. }
  245. func (ast Ast) Token() Token {
  246. return ast.token
  247. }
  248. func (ast Ast) Parent() *Ast {
  249. return ast.parent
  250. }
  251. func (ast Ast) Children() []*Ast {
  252. return ast.children
  253. }
  254. func (ast Ast) Kind() AstKind {
  255. return ast.AstKind
  256. }
  257. func (ast *Ast) SetParent(parent *Ast) {
  258. ast.parent = parent
  259. }
  260. func (ast Ast) Child(index int) *Ast {
  261. count := ast.CountChildren()
  262. if index < 0 || index > count {
  263. return nil
  264. }
  265. return ast.children[index]
  266. }
  267. func (ast Ast) Run(vm *VM, val []Value) []Value {
  268. return ast.AstKind.Run(vm, ast, val)
  269. }
  270. func (ast *Ast) Walk(walker func(node *Ast) *Ast) *Ast {
  271. if found := walker(ast); found != nil {
  272. return found
  273. }
  274. for _, child := range ast.children {
  275. if found := child.Walk(walker); found != nil {
  276. return found
  277. }
  278. }
  279. return nil
  280. }
  281. func (ast Ast) String() string {
  282. return fmt.Sprintf("Ast %s: %s", ast.AstKind.String(), ast.token.String())
  283. }
  284. func (ast Ast) Display() {
  285. ast.Walk(func(node *Ast) *Ast {
  286. depth := node.Depth()
  287. fmt.Printf("%s", strings.Repeat("--", depth))
  288. if node != nil {
  289. fmt.Printf("Ast: %s\n", node.String())
  290. } else {
  291. fmt.Printf("Ast: nil node\n")
  292. }
  293. return nil
  294. })
  295. }
  296. func (ast Ast) Depth() int {
  297. var depth int = 0
  298. parent := ast.Parent()
  299. for parent != nil {
  300. depth++
  301. parent = parent.Parent()
  302. }
  303. return depth
  304. }
  305. func (ast Ast) CountChildren() int {
  306. return len(ast.children)
  307. }
  308. func AstIsError(ast * Ast) bool {
  309. return ast.IsError()
  310. }
  311. func (ast Ast) Errors() []*Ast {
  312. res := make([]*Ast, 0)
  313. ast.Walk(func(node *Ast) *Ast {
  314. if node != nil && ast.IsError() {
  315. res = append(res, node)
  316. }
  317. return nil
  318. })
  319. return res
  320. }
  321. func EmptyAstArray() []*Ast {
  322. return make([]*Ast, 0)
  323. }
  324. func NewEmptyAst(astkind AstKind) *Ast {
  325. return NewAst(astkind, nil, EmptyAstArray(), NoToken())
  326. }
  327. func NewAstNone() *Ast {
  328. return NewEmptyAst(AstKindNone)
  329. }
  330. func NewAstWithToken(astkind AstKind, token Token) *Ast {
  331. return NewAst(astkind, nil, EmptyAstArray(), token)
  332. }
  333. /*
  334. type AstProgram struct{ Ast }
  335. type AstStatements struct{ Ast }
  336. type AstStatement struct{ Ast }
  337. type AstSet struct{ Ast }
  338. type AstGet struct{ Ast }
  339. type AstTarget struct{ Ast }
  340. type AstCommand struct{ Ast }
  341. type AstArguments struct{ Ast }
  342. type AstArgument struct{ Ast }
  343. type AstExpression struct{ Ast }
  344. type AstBlock struct{ Ast }
  345. type AstParenthesis struct{ Ast }
  346. type AstList struct{ Ast }
  347. type AstCapture struct{ Ast }
  348. type AstWordValue struct{ Ast }
  349. type AstWord struct{ Ast }
  350. type AstType struct{ Ast }
  351. type AstValue struct{ Ast }
  352. type AstEnd struct{ Ast }
  353. type AstError struct{ Ast }
  354. */
  355. var _ Ast = Ast{}
  356. /* The kind of an AST is also it's constructor function. This is so deep I'm almost
  357. getting scared. :) */
  358. // The type is defined as a constructor
  359. /*
  360. type AstMetaKind func(parent Ast, children []Ast, token Token) Ast
  361. type AstMetaKind2 interface {
  362. New(parent Ast, children []Ast, token Token) Ast
  363. }
  364. type AstTypeProgramImp struct{}
  365. func (ap AstTypeProgramImp) New(parent Ast, children []Ast, token Token) Ast {
  366. return AstProgram{NewBasicAst(parent, children, token)}
  367. }
  368. func NewAstProgram(parent Ast, children []Ast, token Token) Ast {
  369. return AstProgram{NewBasicAst(parent, children, token)}
  370. }
  371. var AstMetaKindBasic = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast { return NewBasicAst(parent, children, token) })
  372. var AstMetaKindProgram = AstMetaKind(NewAstProgram)
  373. var AstMetaKindProgram2 = AstMetaKind2(AstTypeProgramImp{})
  374. var AstMetaKindStatements = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  375. return &AstStatements{NewBasicAst(parent, children, token)}
  376. })
  377. var AstMetaKindStatement = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  378. return &AstStatement{NewBasicAst(parent, children, token)}
  379. })
  380. var AstMetaKindSet = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  381. return &AstSet{NewBasicAst(parent, children, token)}
  382. })
  383. var AstMetaKindGet = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  384. return &AstGet{NewBasicAst(parent, children, token)}
  385. })
  386. var AstMetaKindTarget = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  387. return &AstTarget{NewBasicAst(parent, children, token)}
  388. })
  389. var AstMetaKindCommand = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  390. return &AstCommand{NewBasicAst(parent, children, token)}
  391. })
  392. var AstMetaKindArguments = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  393. return &AstArguments{NewBasicAst(parent, children, token)}
  394. })
  395. var AstMetaKindArgument = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  396. return &AstArgument{NewBasicAst(parent, children, token)}
  397. })
  398. var AstMetaKindExpression = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  399. return &AstExpression{NewBasicAst(parent, children, token)}
  400. })
  401. var AstMetaKindBlock = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  402. return &AstBlock{NewBasicAst(parent, children, token)}
  403. })
  404. var AstMetaKindParenthesis = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  405. return &AstParenthesis{NewBasicAst(parent, children, token)}
  406. })
  407. var AstMetaKindList = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  408. return &AstList{NewBasicAst(parent, children, token)}
  409. })
  410. var AstMetaKindCapture = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  411. return &AstCapture{NewBasicAst(parent, children, token)}
  412. })
  413. var AstMetaKindWordValue = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  414. return &AstWordValue{NewBasicAst(parent, children, token)}
  415. })
  416. var AstMetaKindWord = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  417. return &AstWord{NewBasicAst(parent, children, token)}
  418. })
  419. var AstMetaKindType = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  420. return &AstType{NewBasicAst(parent, children, token)}
  421. })
  422. var AstMetaKindValue = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  423. return &AstValue{NewBasicAst(parent, children, token)}
  424. })
  425. var AstMetaKindEnd = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  426. return &AstEnd{NewBasicAst(parent, children, token)}
  427. })
  428. var AstMetaKindError = AstMetaKind(func(parent Ast, children []Ast, token Token) Ast {
  429. return &AstError{NewBasicAst(parent, children, token)}
  430. })
  431. */