ast.go 15 KB

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