rt_statements.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "fmt"
  13. "devt.de/krotik/common/sortutil"
  14. "devt.de/krotik/ecal/parser"
  15. "devt.de/krotik/ecal/scope"
  16. "devt.de/krotik/ecal/util"
  17. )
  18. // Statements Runtime
  19. // ==================
  20. /*
  21. statementsRuntime is the runtime component for sequences of statements.
  22. */
  23. type statementsRuntime struct {
  24. *baseRuntime
  25. }
  26. /*
  27. statementsRuntimeInst returns a new runtime component instance.
  28. */
  29. func statementsRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  30. return &statementsRuntime{newBaseRuntime(erp, node)}
  31. }
  32. /*
  33. Eval evaluate this runtime component.
  34. */
  35. func (rt *statementsRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  36. _, err := rt.baseRuntime.Eval(vs, is)
  37. if err == nil {
  38. for _, child := range rt.node.Children {
  39. if _, err := child.Runtime.Eval(vs, is); err != nil {
  40. return nil, err
  41. }
  42. }
  43. }
  44. return nil, err
  45. }
  46. // Condition statement
  47. // ===================
  48. /*
  49. ifRuntime is the runtime for the if condition statement.
  50. */
  51. type ifRuntime struct {
  52. *baseRuntime
  53. }
  54. /*
  55. ifRuntimeInst returns a new runtime component instance.
  56. */
  57. func ifRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  58. return &ifRuntime{newBaseRuntime(erp, node)}
  59. }
  60. /*
  61. Eval evaluate this runtime component.
  62. */
  63. func (rt *ifRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  64. _, err := rt.baseRuntime.Eval(vs, is)
  65. if err == nil {
  66. // Create a new variable scope
  67. vs = vs.NewChild(scope.NameFromASTNode(rt.node))
  68. for offset := 0; offset < len(rt.node.Children); offset += 2 {
  69. var guardres interface{}
  70. // Evaluate guard
  71. if err == nil {
  72. guardres, err = rt.node.Children[offset].Runtime.Eval(vs, is)
  73. if err == nil && guardres.(bool) {
  74. // The guard holds true so we execture its statements
  75. return rt.node.Children[offset+1].Runtime.Eval(vs, is)
  76. }
  77. }
  78. }
  79. }
  80. return nil, err
  81. }
  82. // Guard Runtime
  83. // =============
  84. /*
  85. guardRuntime is the runtime for any guard condition (used in if, for, etc...).
  86. */
  87. type guardRuntime struct {
  88. *baseRuntime
  89. }
  90. /*
  91. guardRuntimeInst returns a new runtime component instance.
  92. */
  93. func guardRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  94. return &guardRuntime{newBaseRuntime(erp, node)}
  95. }
  96. /*
  97. Eval evaluate this runtime component.
  98. */
  99. func (rt *guardRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  100. var res interface{}
  101. _, err := rt.baseRuntime.Eval(vs, is)
  102. if err == nil {
  103. var ret interface{}
  104. // Evaluate the condition
  105. ret, err = rt.node.Children[0].Runtime.Eval(vs, is)
  106. // Guard returns always a boolean
  107. res = ret != nil && ret != false && ret != 0
  108. }
  109. return res, err
  110. }
  111. // Loop statement
  112. // ==============
  113. /*
  114. loopRuntime is the runtime for the loop statement (for).
  115. */
  116. type loopRuntime struct {
  117. *baseRuntime
  118. leftInVarName []string
  119. }
  120. /*
  121. loopRuntimeInst returns a new runtime component instance.
  122. */
  123. func loopRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  124. return &loopRuntime{newBaseRuntime(erp, node), nil}
  125. }
  126. /*
  127. Validate this node and all its child nodes.
  128. */
  129. func (rt *loopRuntime) Validate() error {
  130. err := rt.baseRuntime.Validate()
  131. if err == nil {
  132. if rt.node.Children[0].Name == parser.NodeIN {
  133. inVar := rt.node.Children[0].Children[0]
  134. if inVar.Name == parser.NodeIDENTIFIER {
  135. if len(inVar.Children) != 0 {
  136. return rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  137. "Must have a simple variable on the left side of the In expression", rt.node)
  138. }
  139. rt.leftInVarName = []string{inVar.Token.Val}
  140. } else if inVar.Name == parser.NodeLIST {
  141. rt.leftInVarName = make([]string, 0, len(inVar.Children))
  142. for _, child := range inVar.Children {
  143. if child.Name != parser.NodeIDENTIFIER || len(child.Children) != 0 {
  144. return rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  145. "Must have a list of simple variables on the left side of the In expression", rt.node)
  146. }
  147. rt.leftInVarName = append(rt.leftInVarName, child.Token.Val)
  148. }
  149. }
  150. }
  151. }
  152. return err
  153. }
  154. /*
  155. Eval evaluate this runtime component.
  156. */
  157. func (rt *loopRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  158. _, err := rt.baseRuntime.Eval(vs, is)
  159. if err == nil {
  160. var guardres interface{}
  161. // Create a new variable scope
  162. vs = vs.NewChild(scope.NameFromASTNode(rt.node))
  163. // Create a new instance scope - elements in each loop iteration start from scratch
  164. is = make(map[string]interface{})
  165. if rt.node.Children[0].Name == parser.NodeGUARD {
  166. // Evaluate guard
  167. guardres, err = rt.node.Children[0].Runtime.Eval(vs, is)
  168. for err == nil && guardres.(bool) {
  169. // Execute block
  170. _, err = rt.node.Children[1].Runtime.Eval(vs, is)
  171. // Check for continue
  172. if err != nil {
  173. if eoi, ok := err.(*util.RuntimeError); ok {
  174. if eoi.Type == util.ErrContinueIteration {
  175. err = nil
  176. }
  177. }
  178. }
  179. if err == nil {
  180. // Evaluate guard
  181. guardres, err = rt.node.Children[0].Runtime.Eval(vs, is)
  182. }
  183. }
  184. } else if rt.node.Children[0].Name == parser.NodeIN {
  185. var iterator func() (interface{}, error)
  186. var val interface{}
  187. it := rt.node.Children[0].Children[1]
  188. val, err = it.Runtime.Eval(vs, is)
  189. // Create an iterator object
  190. if rterr, ok := err.(*util.RuntimeError); ok && rterr.Type == util.ErrIsIterator {
  191. // We got an iterator - all subsequent calls will return values
  192. iterator = func() (interface{}, error) {
  193. return it.Runtime.Eval(vs, is)
  194. }
  195. err = nil
  196. } else {
  197. // We got a value over which we need to iterate
  198. if valList, isList := val.([]interface{}); isList {
  199. index := -1
  200. end := len(valList)
  201. iterator = func() (interface{}, error) {
  202. index++
  203. if index >= end {
  204. return nil, rt.erp.NewRuntimeError(util.ErrEndOfIteration, "", rt.node)
  205. }
  206. return valList[index], nil
  207. }
  208. } else if valMap, isMap := val.(map[interface{}]interface{}); isMap {
  209. var keys []interface{}
  210. index := -1
  211. for k := range valMap {
  212. keys = append(keys, k)
  213. }
  214. end := len(keys)
  215. // Try to sort according to string value
  216. sortutil.InterfaceStrings(keys)
  217. iterator = func() (interface{}, error) {
  218. index++
  219. if index >= end {
  220. return nil, rt.erp.NewRuntimeError(util.ErrEndOfIteration, "", rt.node)
  221. }
  222. key := keys[index]
  223. return []interface{}{key, valMap[key]}, nil
  224. }
  225. } else {
  226. // A single value will do exactly one iteration
  227. index := -1
  228. iterator = func() (interface{}, error) {
  229. index++
  230. if index > 0 {
  231. return nil, rt.erp.NewRuntimeError(util.ErrEndOfIteration, "", rt.node)
  232. }
  233. return val, nil
  234. }
  235. }
  236. }
  237. vars := rt.leftInVarName
  238. for err == nil {
  239. var res interface{}
  240. res, err = iterator()
  241. if err != nil {
  242. if eoi, ok := err.(*util.RuntimeError); ok {
  243. if eoi.Type == util.ErrIsIterator {
  244. err = nil
  245. }
  246. }
  247. }
  248. if err == nil {
  249. if len(vars) == 1 {
  250. if err = vs.SetValue(vars[0], res); err != nil {
  251. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  252. err.Error(), rt.node)
  253. }
  254. } else if resList, ok := res.([]interface{}); ok {
  255. if len(vars) != len(resList) {
  256. return nil, rt.erp.NewRuntimeError(util.ErrInvalidState,
  257. fmt.Sprintf("Assigned number of variables is different to "+
  258. "number of values (%v variables vs %v values)",
  259. len(vars), len(resList)), rt.node)
  260. }
  261. for i, v := range vars {
  262. if err == nil {
  263. if err = vs.SetValue(v, resList[i]); err != nil {
  264. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  265. err.Error(), rt.node)
  266. }
  267. }
  268. }
  269. } else {
  270. return nil, rt.erp.NewRuntimeError(util.ErrInvalidState,
  271. fmt.Sprintf("Result for loop variable is not a list (value is %v)", res),
  272. rt.node)
  273. }
  274. // Execute block
  275. if err == nil {
  276. _, err = rt.node.Children[1].Runtime.Eval(vs, is)
  277. }
  278. }
  279. // Check for continue
  280. if err != nil {
  281. if eoi, ok := err.(*util.RuntimeError); ok {
  282. if eoi.Type == util.ErrContinueIteration {
  283. err = nil
  284. }
  285. }
  286. }
  287. }
  288. // Check for end of iteration error
  289. if eoi, ok := err.(*util.RuntimeError); ok {
  290. if eoi.Type == util.ErrEndOfIteration {
  291. err = nil
  292. }
  293. }
  294. }
  295. }
  296. return nil, err
  297. }
  298. // Break statement
  299. // ===============
  300. /*
  301. breakRuntime is the runtime for the break statement.
  302. */
  303. type breakRuntime struct {
  304. *baseRuntime
  305. }
  306. /*
  307. breakRuntimeInst returns a new runtime component instance.
  308. */
  309. func breakRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  310. return &breakRuntime{newBaseRuntime(erp, node)}
  311. }
  312. /*
  313. Eval evaluate this runtime component.
  314. */
  315. func (rt *breakRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  316. _, err := rt.baseRuntime.Eval(vs, is)
  317. if err == nil {
  318. err = rt.erp.NewRuntimeError(util.ErrEndOfIteration, "", rt.node)
  319. }
  320. return nil, err
  321. }
  322. // Continue statement
  323. // ==================
  324. /*
  325. continueRuntime is the runtime for the continue statement.
  326. */
  327. type continueRuntime struct {
  328. *baseRuntime
  329. }
  330. /*
  331. continueRuntimeInst returns a new runtime component instance.
  332. */
  333. func continueRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  334. return &continueRuntime{newBaseRuntime(erp, node)}
  335. }
  336. /*
  337. Eval evaluate this runtime component.
  338. */
  339. func (rt *continueRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  340. _, err := rt.baseRuntime.Eval(vs, is)
  341. if err == nil {
  342. err = rt.erp.NewRuntimeError(util.ErrContinueIteration, "", rt.node)
  343. }
  344. return nil, err
  345. }