rt_general.go 11 KB

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