ast.go 17 KB

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