rt_general.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/errorutil"
  14. "devt.de/krotik/ecal/parser"
  15. "devt.de/krotik/ecal/scope"
  16. "devt.de/krotik/ecal/util"
  17. )
  18. // Base Runtime
  19. // ============
  20. /*
  21. baseRuntime models a base runtime component which provides the essential fields and functions.
  22. */
  23. type baseRuntime struct {
  24. instanceID string // Unique identifier (should be used when instance state is stored)
  25. erp *ECALRuntimeProvider // Runtime provider
  26. node *parser.ASTNode // AST node which this runtime component is servicing
  27. validated bool
  28. }
  29. var instanceCounter uint64 // Global instance counter to create unique identifiers for every runtime component instance
  30. /*
  31. Validate this node and all its child nodes.
  32. */
  33. func (rt *baseRuntime) Validate() error {
  34. rt.validated = true
  35. // Validate all children
  36. for _, child := range rt.node.Children {
  37. if err := child.Runtime.Validate(); err != nil {
  38. return err
  39. }
  40. }
  41. return nil
  42. }
  43. /*
  44. Eval evaluate this runtime component.
  45. */
  46. func (rt *baseRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  47. var err error
  48. errorutil.AssertTrue(rt.validated, "Runtime component has not been validated - please call Validate() before Eval()")
  49. if rt.erp.Debugger != nil {
  50. err = rt.erp.Debugger.VisitState(rt.node, vs, tid)
  51. rt.erp.Debugger.SetLockingState(rt.erp.MutexeOwners, rt.erp.MutexLog)
  52. rt.erp.Debugger.SetThreadPool(rt.erp.Processor.ThreadPool())
  53. }
  54. return nil, err
  55. }
  56. /*
  57. newBaseRuntime returns a new instance of baseRuntime.
  58. */
  59. func newBaseRuntime(erp *ECALRuntimeProvider, node *parser.ASTNode) *baseRuntime {
  60. instanceCounter++
  61. return &baseRuntime{fmt.Sprint(instanceCounter), erp, node, false}
  62. }
  63. // Void Runtime
  64. // ============
  65. /*
  66. voidRuntime is a special runtime for constructs which are only evaluated as part
  67. of other components.
  68. */
  69. type voidRuntime struct {
  70. *baseRuntime
  71. }
  72. /*
  73. voidRuntimeInst returns a new runtime component instance.
  74. */
  75. func voidRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  76. return &voidRuntime{newBaseRuntime(erp, node)}
  77. }
  78. /*
  79. Validate this node and all its child nodes.
  80. */
  81. func (rt *voidRuntime) Validate() error {
  82. return rt.baseRuntime.Validate()
  83. }
  84. /*
  85. Eval evaluate this runtime component.
  86. */
  87. func (rt *voidRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  88. return rt.baseRuntime.Eval(vs, is, tid)
  89. }
  90. // Import Runtime
  91. // ==============
  92. /*
  93. importRuntime handles import statements.
  94. */
  95. type importRuntime struct {
  96. *baseRuntime
  97. }
  98. /*
  99. importRuntimeInst returns a new runtime component instance.
  100. */
  101. func importRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  102. return &importRuntime{newBaseRuntime(erp, node)}
  103. }
  104. /*
  105. Validate this node and all its child nodes.
  106. */
  107. func (rt *importRuntime) Validate() error {
  108. return rt.baseRuntime.Validate()
  109. }
  110. /*
  111. Eval evaluate this runtime component.
  112. */
  113. func (rt *importRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  114. _, err := rt.baseRuntime.Eval(vs, is, tid)
  115. if rt.erp.ImportLocator == nil {
  116. err = rt.erp.NewRuntimeError(util.ErrRuntimeError, "No import locator was specified", rt.node)
  117. }
  118. if err == nil {
  119. var importPath interface{}
  120. if importPath, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
  121. var codeText string
  122. if codeText, err = rt.erp.ImportLocator.Resolve(fmt.Sprint(importPath)); err == nil {
  123. var ast *parser.ASTNode
  124. if ast, err = parser.ParseWithRuntime(fmt.Sprint(importPath), codeText, rt.erp); err == nil {
  125. if err = ast.Runtime.Validate(); err == nil {
  126. ivs := scope.NewScope(scope.GlobalScope)
  127. if _, err = ast.Runtime.Eval(ivs, make(map[string]interface{}), tid); err == nil {
  128. irt := rt.node.Children[1].Runtime.(*identifierRuntime)
  129. irt.Set(vs, is, tid, scope.ToObject(ivs))
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. return nil, err
  137. }
  138. // Not Implemented Runtime
  139. // =======================
  140. /*
  141. invalidRuntime is a special runtime for not implemented constructs.
  142. */
  143. type invalidRuntime struct {
  144. *baseRuntime
  145. }
  146. /*
  147. invalidRuntimeInst returns a new runtime component instance.
  148. */
  149. func invalidRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  150. return &invalidRuntime{newBaseRuntime(erp, node)}
  151. }
  152. /*
  153. Validate this node and all its child nodes.
  154. */
  155. func (rt *invalidRuntime) Validate() error {
  156. err := rt.baseRuntime.Validate()
  157. if err == nil {
  158. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  159. fmt.Sprintf("Unknown node: %s", rt.node.Name), rt.node)
  160. }
  161. return err
  162. }
  163. /*
  164. Eval evaluate this runtime component.
  165. */
  166. func (rt *invalidRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  167. _, err := rt.baseRuntime.Eval(vs, is, tid)
  168. if err == nil {
  169. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct, fmt.Sprintf("Unknown node: %s", rt.node.Name), rt.node)
  170. }
  171. return nil, err
  172. }
  173. // General Operator Runtime
  174. // ========================
  175. /*
  176. operatorRuntime is a general operator operation. Used for embedding.
  177. */
  178. type operatorRuntime struct {
  179. *baseRuntime
  180. }
  181. /*
  182. errorDetailString produces a detail string for errors.
  183. */
  184. func (rt *operatorRuntime) errorDetailString(token *parser.LexToken, opVal interface{}) string {
  185. if !token.Identifier {
  186. return token.Val
  187. }
  188. if opVal == nil {
  189. opVal = "NULL"
  190. }
  191. return fmt.Sprintf("%v=%v", token.Val, opVal)
  192. }
  193. /*
  194. numVal returns a transformed number value.
  195. */
  196. func (rt *operatorRuntime) numVal(op func(float64) interface{}, vs parser.Scope,
  197. is map[string]interface{}, tid uint64) (interface{}, error) {
  198. var ret interface{}
  199. errorutil.AssertTrue(len(rt.node.Children) == 1,
  200. fmt.Sprint("Operation requires 1 operand", rt.node))
  201. res, err := rt.node.Children[0].Runtime.Eval(vs, is, tid)
  202. if err == nil {
  203. // Check if the value is a number
  204. resNum, ok := res.(float64)
  205. if !ok {
  206. // Produce a runtime error if the value is not a number
  207. return nil, rt.erp.NewRuntimeError(util.ErrNotANumber,
  208. rt.errorDetailString(rt.node.Children[0].Token, res), rt.node.Children[0])
  209. }
  210. ret = op(resNum)
  211. }
  212. return ret, err
  213. }
  214. /*
  215. boolVal returns a transformed boolean value.
  216. */
  217. func (rt *operatorRuntime) boolVal(op func(bool) interface{},
  218. vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  219. var ret interface{}
  220. errorutil.AssertTrue(len(rt.node.Children) == 1,
  221. fmt.Sprint("Operation requires 1 operand", rt.node))
  222. res, err := rt.node.Children[0].Runtime.Eval(vs, is, tid)
  223. if err == nil {
  224. resBool, ok := res.(bool)
  225. if !ok {
  226. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  227. rt.errorDetailString(rt.node.Children[0].Token, res), rt.node.Children[0])
  228. }
  229. ret = op(resBool)
  230. }
  231. return ret, err
  232. }
  233. /*
  234. numOp executes an operation on two number values.
  235. */
  236. func (rt *operatorRuntime) numOp(op func(float64, float64) interface{},
  237. vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  238. var ok bool
  239. var res1, res2 interface{}
  240. var err error
  241. errorutil.AssertTrue(len(rt.node.Children) == 2,
  242. fmt.Sprint("Operation requires 2 operands", rt.node))
  243. if res1, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
  244. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is, tid); err == nil {
  245. var res1Num, res2Num float64
  246. if res1Num, ok = res1.(float64); !ok {
  247. err = rt.erp.NewRuntimeError(util.ErrNotANumber,
  248. rt.errorDetailString(rt.node.Children[0].Token, res1), rt.node.Children[0])
  249. } else {
  250. if res2Num, ok = res2.(float64); !ok {
  251. err = rt.erp.NewRuntimeError(util.ErrNotANumber,
  252. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[1])
  253. } else {
  254. return op(res1Num, res2Num), err
  255. }
  256. }
  257. }
  258. }
  259. return nil, err
  260. }
  261. /*
  262. genOp executes an operation on two general values.
  263. */
  264. func (rt *operatorRuntime) genOp(op func(interface{}, interface{}) interface{},
  265. vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  266. var ret interface{}
  267. errorutil.AssertTrue(len(rt.node.Children) == 2,
  268. fmt.Sprint("Operation requires 2 operands", rt.node))
  269. res1, err := rt.node.Children[0].Runtime.Eval(vs, is, tid)
  270. if err == nil {
  271. var res2 interface{}
  272. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is, tid); err == nil {
  273. ret = op(res1, res2)
  274. }
  275. }
  276. return ret, err
  277. }
  278. /*
  279. strOp executes an operation on two string values.
  280. */
  281. func (rt *operatorRuntime) strOp(op func(string, string) interface{},
  282. vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  283. var ret interface{}
  284. errorutil.AssertTrue(len(rt.node.Children) == 2,
  285. fmt.Sprint("Operation requires 2 operands", rt.node))
  286. res1, err := rt.node.Children[0].Runtime.Eval(vs, is, tid)
  287. if err == nil {
  288. var res2 interface{}
  289. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is, tid); err == nil {
  290. ret = op(fmt.Sprint(res1), fmt.Sprint(res2))
  291. }
  292. }
  293. return ret, err
  294. }
  295. /*
  296. boolOp executes an operation on two boolean values.
  297. */
  298. func (rt *operatorRuntime) boolOp(op func(bool, bool) interface{},
  299. vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  300. var res interface{}
  301. errorutil.AssertTrue(len(rt.node.Children) == 2,
  302. fmt.Sprint("Operation requires 2 operands", rt.node))
  303. res1, err := rt.node.Children[0].Runtime.Eval(vs, is, tid)
  304. if err == nil {
  305. var res2 interface{}
  306. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is, tid); err == nil {
  307. res1bool, ok := res1.(bool)
  308. if !ok {
  309. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  310. rt.errorDetailString(rt.node.Children[0].Token, res1), rt.node.Children[0])
  311. }
  312. res2bool, ok := res2.(bool)
  313. if !ok {
  314. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  315. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[0])
  316. }
  317. res = op(res1bool, res2bool)
  318. }
  319. }
  320. return res, err
  321. }
  322. /*
  323. listOp executes an operation on a value and a list.
  324. */
  325. func (rt *operatorRuntime) listOp(op func(interface{}, []interface{}) interface{},
  326. vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  327. var res interface{}
  328. errorutil.AssertTrue(len(rt.node.Children) == 2,
  329. fmt.Sprint("Operation requires 2 operands", rt.node))
  330. res1, err := rt.node.Children[0].Runtime.Eval(vs, is, tid)
  331. if err == nil {
  332. var res2 interface{}
  333. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is, tid); err == nil {
  334. res2list, ok := res2.([]interface{})
  335. if !ok {
  336. err = rt.erp.NewRuntimeError(util.ErrNotAList,
  337. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[0])
  338. } else {
  339. res = op(res1, res2list)
  340. }
  341. }
  342. }
  343. return res, err
  344. }