rt_general.go 10 KB

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