rt_general.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/util"
  16. )
  17. // Base Runtime
  18. // ============
  19. /*
  20. baseRuntime models a base runtime component which provides the essential fields and functions.
  21. */
  22. type baseRuntime struct {
  23. instanceID string // Unique identifier (should be used when instance state is stored)
  24. erp *ECALRuntimeProvider // Runtime provider
  25. node *parser.ASTNode // AST node which this runtime component is servicing
  26. }
  27. var instanceCounter uint64 // Global instance counter to create unique identifiers for every runtime component instance
  28. /*
  29. Validate this node and all its child nodes.
  30. */
  31. func (rt *baseRuntime) Validate() error {
  32. // Validate all children
  33. for _, child := range rt.node.Children {
  34. if err := child.Runtime.Validate(); err != nil {
  35. return err
  36. }
  37. }
  38. return nil
  39. }
  40. /*
  41. Eval evaluate this runtime component.
  42. */
  43. func (rt *baseRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  44. return nil, nil
  45. }
  46. /*
  47. newBaseRuntime returns a new instance of baseRuntime.
  48. */
  49. func newBaseRuntime(erp *ECALRuntimeProvider, node *parser.ASTNode) *baseRuntime {
  50. instanceCounter++
  51. return &baseRuntime{fmt.Sprint(instanceCounter), erp, node}
  52. }
  53. // Void Runtime
  54. // ============
  55. /*
  56. voidRuntime is a special runtime for constructs which are only evaluated as part
  57. of other components.
  58. */
  59. type voidRuntime struct {
  60. *baseRuntime
  61. }
  62. /*
  63. voidRuntimeInst returns a new runtime component instance.
  64. */
  65. func voidRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  66. return &voidRuntime{newBaseRuntime(erp, node)}
  67. }
  68. /*
  69. Validate this node and all its child nodes.
  70. */
  71. func (rt *voidRuntime) Validate() error {
  72. return rt.baseRuntime.Validate()
  73. }
  74. /*
  75. Eval evaluate this runtime component.
  76. */
  77. func (rt *voidRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  78. return rt.baseRuntime.Eval(vs, is)
  79. }
  80. // Not Implemented Runtime
  81. // =======================
  82. /*
  83. invalidRuntime is a special runtime for not implemented constructs.
  84. */
  85. type invalidRuntime struct {
  86. *baseRuntime
  87. }
  88. /*
  89. invalidRuntimeInst returns a new runtime component instance.
  90. */
  91. func invalidRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  92. return &invalidRuntime{newBaseRuntime(erp, node)}
  93. }
  94. /*
  95. Validate this node and all its child nodes.
  96. */
  97. func (rt *invalidRuntime) Validate() error {
  98. err := rt.baseRuntime.Validate()
  99. if err == nil {
  100. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  101. fmt.Sprintf("Unknown node: %s", rt.node.Name), rt.node)
  102. }
  103. return err
  104. }
  105. /*
  106. Eval evaluate this runtime component.
  107. */
  108. func (rt *invalidRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  109. _, err := rt.baseRuntime.Eval(vs, is)
  110. if err == nil {
  111. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct, fmt.Sprintf("Unknown node: %s", rt.node.Name), rt.node)
  112. }
  113. return nil, err
  114. }
  115. // General Operator Runtime
  116. // ========================
  117. /*
  118. operatorRuntime is a general operator operation. Used for embedding.
  119. */
  120. type operatorRuntime struct {
  121. *baseRuntime
  122. }
  123. /*
  124. errorDetailString produces a detail string for errors.
  125. */
  126. func (rt *operatorRuntime) errorDetailString(token *parser.LexToken, opVal interface{}) string {
  127. if token.Identifier {
  128. return token.Val
  129. }
  130. return fmt.Sprintf("%v=%v", token.Val, opVal)
  131. }
  132. /*
  133. numVal returns a transformed number value.
  134. */
  135. func (rt *operatorRuntime) numVal(op func(float64) interface{}, vs parser.Scope,
  136. is map[string]interface{}) (interface{}, error) {
  137. errorutil.AssertTrue(len(rt.node.Children) == 1,
  138. fmt.Sprint("Operation requires 1 operand", rt.node))
  139. res, err := rt.node.Children[0].Runtime.Eval(vs, is)
  140. if err != nil {
  141. return nil, err
  142. }
  143. // Check if the value is a number
  144. resNum, ok := res.(float64)
  145. if !ok {
  146. // Produce a runtime error if the value is not a number
  147. return nil, rt.erp.NewRuntimeError(util.ErrNotANumber,
  148. rt.errorDetailString(rt.node.Children[0].Token, res), rt.node.Children[0])
  149. }
  150. return op(resNum), nil
  151. }
  152. /*
  153. boolVal returns a transformed boolean value.
  154. */
  155. func (rt *operatorRuntime) boolVal(op func(bool) interface{},
  156. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  157. var err error
  158. errorutil.AssertTrue(len(rt.node.Children) == 1,
  159. fmt.Sprint("Operation requires 1 operand", rt.node))
  160. res, err := rt.node.Children[0].Runtime.Eval(vs, is)
  161. if err != nil {
  162. return nil, err
  163. }
  164. resBool, ok := res.(bool)
  165. if !ok {
  166. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  167. rt.errorDetailString(rt.node.Children[0].Token, res), rt.node.Children[0])
  168. }
  169. return op(resBool), nil
  170. }
  171. /*
  172. numOp executes an operation on two number values.
  173. */
  174. func (rt *operatorRuntime) numOp(op func(float64, float64) interface{},
  175. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  176. var ok bool
  177. var res1, res2 interface{}
  178. var err error
  179. errorutil.AssertTrue(len(rt.node.Children) == 2,
  180. fmt.Sprint("Operation requires 2 operands", rt.node))
  181. if res1, err = rt.node.Children[0].Runtime.Eval(vs, is); err == nil {
  182. if res2, err = rt.node.Children[1].Runtime.Eval(vs, is); err == nil {
  183. var res1Num, res2Num float64
  184. if res1Num, ok = res1.(float64); !ok {
  185. err = rt.erp.NewRuntimeError(util.ErrNotANumber,
  186. rt.errorDetailString(rt.node.Children[0].Token, res1), rt.node.Children[0])
  187. } else {
  188. if res2Num, ok = res2.(float64); !ok {
  189. err = rt.erp.NewRuntimeError(util.ErrNotANumber,
  190. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[1])
  191. } else {
  192. return op(res1Num, res2Num), nil
  193. }
  194. }
  195. }
  196. }
  197. return nil, err
  198. }
  199. /*
  200. genOp executes an operation on two general values.
  201. */
  202. func (rt *operatorRuntime) genOp(op func(interface{}, interface{}) interface{},
  203. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  204. errorutil.AssertTrue(len(rt.node.Children) == 2,
  205. fmt.Sprint("Operation requires 2 operands", rt.node))
  206. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  207. if err != nil {
  208. return nil, err
  209. }
  210. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return op(res1, res2), nil
  215. }
  216. /*
  217. strOp executes an operation on two string values.
  218. */
  219. func (rt *operatorRuntime) strOp(op func(string, string) interface{},
  220. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  221. errorutil.AssertTrue(len(rt.node.Children) == 2,
  222. fmt.Sprint("Operation requires 2 operands", rt.node))
  223. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  224. if err != nil {
  225. return nil, err
  226. }
  227. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  228. if err != nil {
  229. return nil, err
  230. }
  231. return op(fmt.Sprint(res1), fmt.Sprint(res2)), nil
  232. }
  233. /*
  234. boolOp executes an operation on two boolean values.
  235. */
  236. func (rt *operatorRuntime) boolOp(op func(bool, bool) interface{},
  237. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  238. errorutil.AssertTrue(len(rt.node.Children) == 2,
  239. fmt.Sprint("Operation requires 2 operands", rt.node))
  240. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  241. if err != nil {
  242. return nil, err
  243. }
  244. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  245. if err != nil {
  246. return nil, err
  247. }
  248. res1bool, ok := res1.(bool)
  249. if !ok {
  250. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  251. rt.errorDetailString(rt.node.Children[0].Token, res1), rt.node.Children[0])
  252. }
  253. res2bool, ok := res2.(bool)
  254. if !ok {
  255. return nil, rt.erp.NewRuntimeError(util.ErrNotABoolean,
  256. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[0])
  257. }
  258. return op(res1bool, res2bool), nil
  259. }
  260. /*
  261. listOp executes an operation on a value and a list.
  262. */
  263. func (rt *operatorRuntime) listOp(op func(interface{}, []interface{}) interface{},
  264. vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  265. errorutil.AssertTrue(len(rt.node.Children) == 2,
  266. fmt.Sprint("Operation requires 2 operands", rt.node))
  267. res1, err := rt.node.Children[0].Runtime.Eval(vs, is)
  268. if err != nil {
  269. return nil, err
  270. }
  271. res2, err := rt.node.Children[1].Runtime.Eval(vs, is)
  272. if err != nil {
  273. return nil, err
  274. }
  275. res2list, ok := res2.([]interface{})
  276. if !ok {
  277. return nil, rt.erp.NewRuntimeError(util.ErrNotAList,
  278. rt.errorDetailString(rt.node.Children[1].Token, res2), rt.node.Children[0])
  279. }
  280. return op(res1, res2list), nil
  281. }