rt_general.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. return fmt.Sprintf("%v=%v", token.Val, opVal)
  180. }
  181. /*
  182. numVal returns a transformed number value.
  183. */
  184. func (rt *operatorRuntime) numVal(op func(float64) interface{}, vs parser.Scope,
  185. is map[string]interface{}) (interface{}, error) {
  186. errorutil.AssertTrue(len(rt.node.Children) == 1,
  187. fmt.Sprint("Operation requires 1 operand", rt.node))
  188. res, err := rt.node.Children[0].Runtime.Eval(vs, is)
  189. if err != nil {
  190. return nil, err
  191. }
  192. // Check if the value is a number
  193. resNum, ok := res.(float64)
  194. if !ok {
  195. // Produce a runtime error if the value is not a number
  196. return nil, rt.erp.NewRuntimeError(util.ErrNotANumber,
  197. rt.errorDetailString(rt.node.Children[0].Token, res), rt.node.Children[0])
  198. }
  199. return op(resNum), nil
  200. }
  201. /*
  202. boolVal returns a transformed boolean value.
  203. */
  204. func (rt *operatorRuntime) boolVal(op func(bool) interface{},
  205. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  206. var err error
  207. errorutil.AssertTrue(len(rt.node.Children) == 1,
  208. fmt.Sprint("Operation requires 1 operand", rt.node))
  209. res, err := rt.node.Children[0].Runtime.Eval(vs, is)
  210. if err != nil {
  211. return nil, err
  212. }
  213. resBool, ok := res.(bool)
  214. if !ok {
  215. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  216. rt.errorDetailString(rt.node.Children[0].Token, res), rt.node.Children[0])
  217. }
  218. return op(resBool), nil
  219. }
  220. /*
  221. numOp executes an operation on two number values.
  222. */
  223. func (rt *operatorRuntime) numOp(op func(float64, float64) interface{},
  224. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  225. var ok bool
  226. var res1, res2 interface{}
  227. var err error
  228. errorutil.AssertTrue(len(rt.node.Children) == 2,
  229. fmt.Sprint("Operation requires 2 operands", rt.node))
  230. if res1, err = rt.node.Children[0].Runtime.Eval(vs, is); err == nil {
  231. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is); err == nil {
  232. var res1Num, res2Num float64
  233. if res1Num, ok = res1.(float64); !ok {
  234. err = rt.erp.NewRuntimeError(util.ErrNotANumber,
  235. rt.errorDetailString(rt.node.Children[0].Token, res1), rt.node.Children[0])
  236. } else {
  237. if res2Num, ok = res2.(float64); !ok {
  238. err = rt.erp.NewRuntimeError(util.ErrNotANumber,
  239. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[1])
  240. } else {
  241. return op(res1Num, res2Num), nil
  242. }
  243. }
  244. }
  245. }
  246. return nil, err
  247. }
  248. /*
  249. genOp executes an operation on two general values.
  250. */
  251. func (rt *operatorRuntime) genOp(op func(interface{}, interface{}) interface{},
  252. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  253. errorutil.AssertTrue(len(rt.node.Children) == 2,
  254. fmt.Sprint("Operation requires 2 operands", rt.node))
  255. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  256. if err != nil {
  257. return nil, err
  258. }
  259. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  260. if err != nil {
  261. return nil, err
  262. }
  263. return op(res1, res2), nil
  264. }
  265. /*
  266. strOp executes an operation on two string values.
  267. */
  268. func (rt *operatorRuntime) strOp(op func(string, string) interface{},
  269. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  270. errorutil.AssertTrue(len(rt.node.Children) == 2,
  271. fmt.Sprint("Operation requires 2 operands", rt.node))
  272. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  273. if err != nil {
  274. return nil, err
  275. }
  276. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  277. if err != nil {
  278. return nil, err
  279. }
  280. return op(fmt.Sprint(res1), fmt.Sprint(res2)), nil
  281. }
  282. /*
  283. boolOp executes an operation on two boolean values.
  284. */
  285. func (rt *operatorRuntime) boolOp(op func(bool, bool) interface{},
  286. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  287. errorutil.AssertTrue(len(rt.node.Children) == 2,
  288. fmt.Sprint("Operation requires 2 operands", rt.node))
  289. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  290. if err != nil {
  291. return nil, err
  292. }
  293. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  294. if err != nil {
  295. return nil, err
  296. }
  297. res1bool, ok := res1.(bool)
  298. if !ok {
  299. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  300. rt.errorDetailString(rt.node.Children[0].Token, res1), rt.node.Children[0])
  301. }
  302. res2bool, ok := res2.(bool)
  303. if !ok {
  304. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  305. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[0])
  306. }
  307. return op(res1bool, res2bool), nil
  308. }
  309. /*
  310. listOp executes an operation on a value and a list.
  311. */
  312. func (rt *operatorRuntime) listOp(op func(interface{}, []interface{}) interface{},
  313. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  314. errorutil.AssertTrue(len(rt.node.Children) == 2,
  315. fmt.Sprint("Operation requires 2 operands", rt.node))
  316. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  317. if err != nil {
  318. return nil, err
  319. }
  320. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  321. if err != nil {
  322. return nil, err
  323. }
  324. res2list, ok := res2.([]interface{})
  325. if !ok {
  326. return nil, rt.erp.NewRuntimeError(util.ErrNotAList,
  327. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[0])
  328. }
  329. return op(res1, res2list), nil
  330. }