rt_boolean.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. "regexp"
  14. "strings"
  15. "devt.de/krotik/common/errorutil"
  16. "devt.de/krotik/ecal/parser"
  17. )
  18. // Basic Boolean Operator Runtimes
  19. // ===============================
  20. type greaterequalOpRuntime struct {
  21. *operatorRuntime
  22. }
  23. /*
  24. greaterequalOpRuntimeInst returns a new runtime component instance.
  25. */
  26. func greaterequalOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  27. return &greaterequalOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  28. }
  29. /*
  30. Eval evaluate this runtime component.
  31. */
  32. func (rt *greaterequalOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  33. var res interface{}
  34. _, err := rt.baseRuntime.Eval(vs, is)
  35. if err == nil {
  36. res, err = rt.numOp(func(n1 float64, n2 float64) interface{} {
  37. return n1 >= n2
  38. }, vs, is)
  39. if err != nil {
  40. res, err = rt.strOp(func(n1 string, n2 string) interface{} {
  41. return n1 >= n2
  42. }, vs, is)
  43. }
  44. }
  45. return res, err
  46. }
  47. type greaterOpRuntime struct {
  48. *operatorRuntime
  49. }
  50. /*
  51. greaterOpRuntimeInst returns a new runtime component instance.
  52. */
  53. func greaterOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  54. return &greaterOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  55. }
  56. /*
  57. Eval evaluate this runtime component.
  58. */
  59. func (rt *greaterOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  60. var res interface{}
  61. _, err := rt.baseRuntime.Eval(vs, is)
  62. if err == nil {
  63. res, err = rt.numOp(func(n1 float64, n2 float64) interface{} {
  64. return n1 > n2
  65. }, vs, is)
  66. if err != nil {
  67. res, err = rt.strOp(func(n1 string, n2 string) interface{} {
  68. return n1 > n2
  69. }, vs, is)
  70. }
  71. }
  72. return res, err
  73. }
  74. type lessequalOpRuntime struct {
  75. *operatorRuntime
  76. }
  77. /*
  78. lessequalOpRuntimeInst returns a new runtime component instance.
  79. */
  80. func lessequalOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  81. return &lessequalOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  82. }
  83. /*
  84. Eval evaluate this runtime component.
  85. */
  86. func (rt *lessequalOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  87. var res interface{}
  88. _, err := rt.baseRuntime.Eval(vs, is)
  89. if err == nil {
  90. res, err = rt.numOp(func(n1 float64, n2 float64) interface{} {
  91. return n1 <= n2
  92. }, vs, is)
  93. if err != nil {
  94. res, err = rt.strOp(func(n1 string, n2 string) interface{} {
  95. return n1 <= n2
  96. }, vs, is)
  97. }
  98. }
  99. return res, err
  100. }
  101. type lessOpRuntime struct {
  102. *operatorRuntime
  103. }
  104. /*
  105. lessOpRuntimeInst returns a new runtime component instance.
  106. */
  107. func lessOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  108. return &lessOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  109. }
  110. /*
  111. Eval evaluate this runtime component.
  112. */
  113. func (rt *lessOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  114. var res interface{}
  115. _, err := rt.baseRuntime.Eval(vs, is)
  116. if err == nil {
  117. res, err = rt.numOp(func(n1 float64, n2 float64) interface{} {
  118. return n1 < n2
  119. }, vs, is)
  120. if err != nil {
  121. res, err = rt.strOp(func(n1 string, n2 string) interface{} {
  122. return n1 < n2
  123. }, vs, is)
  124. }
  125. }
  126. return res, err
  127. }
  128. type equalOpRuntime struct {
  129. *operatorRuntime
  130. }
  131. /*
  132. equalOpRuntimeInst returns a new runtime component instance.
  133. */
  134. func equalOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  135. return &equalOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  136. }
  137. /*
  138. Eval evaluate this runtime component.
  139. */
  140. func (rt *equalOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  141. var res interface{}
  142. _, err := rt.baseRuntime.Eval(vs, is)
  143. if err == nil {
  144. res, err = rt.genOp(func(n1 interface{}, n2 interface{}) interface{} {
  145. return n1 == n2
  146. }, vs, is)
  147. }
  148. return res, err
  149. }
  150. type notequalOpRuntime struct {
  151. *operatorRuntime
  152. }
  153. /*
  154. notequalOpRuntimeInst returns a new runtime component instance.
  155. */
  156. func notequalOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  157. return &notequalOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  158. }
  159. /*
  160. Eval evaluate this runtime component.
  161. */
  162. func (rt *notequalOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  163. var res interface{}
  164. _, err := rt.baseRuntime.Eval(vs, is)
  165. if err == nil {
  166. res, err = rt.genOp(func(n1 interface{}, n2 interface{}) interface{} {
  167. return n1 != n2
  168. }, vs, is)
  169. }
  170. return res, err
  171. }
  172. type andOpRuntime struct {
  173. *operatorRuntime
  174. }
  175. /*
  176. andOpRuntimeInst returns a new runtime component instance.
  177. */
  178. func andOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  179. return &andOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  180. }
  181. /*
  182. Eval evaluate this runtime component.
  183. */
  184. func (rt *andOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  185. var res interface{}
  186. _, err := rt.baseRuntime.Eval(vs, is)
  187. if err == nil {
  188. res, err = rt.boolOp(func(b1 bool, b2 bool) interface{} {
  189. return b1 && b2
  190. }, vs, is)
  191. }
  192. return res, err
  193. }
  194. type orOpRuntime struct {
  195. *operatorRuntime
  196. }
  197. /*
  198. orOpRuntimeInst returns a new runtime component instance.
  199. */
  200. func orOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  201. return &orOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  202. }
  203. /*
  204. Eval evaluate this runtime component.
  205. */
  206. func (rt *orOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  207. var res interface{}
  208. _, err := rt.baseRuntime.Eval(vs, is)
  209. if err == nil {
  210. res, err = rt.boolOp(func(b1 bool, b2 bool) interface{} {
  211. return b1 || b2
  212. }, vs, is)
  213. }
  214. return res, err
  215. }
  216. type notOpRuntime struct {
  217. *operatorRuntime
  218. }
  219. /*
  220. notOpRuntimeInst returns a new runtime component instance.
  221. */
  222. func notOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  223. return &notOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  224. }
  225. /*
  226. Eval evaluate this runtime component.
  227. */
  228. func (rt *notOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  229. var res interface{}
  230. _, err := rt.baseRuntime.Eval(vs, is)
  231. if err == nil {
  232. res, err = rt.boolVal(func(b bool) interface{} {
  233. return !b
  234. }, vs, is)
  235. }
  236. return res, err
  237. }
  238. // In-build condition operators
  239. // ============================
  240. /*
  241. likeOpRuntime is the pattern matching operator. The syntax of the regular
  242. expressions accepted is the same general syntax used by Go, Perl, Python, and
  243. other languages.
  244. */
  245. type likeOpRuntime struct {
  246. *operatorRuntime
  247. }
  248. /*
  249. likeOpRuntimeInst returns a new runtime component instance.
  250. */
  251. func likeOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  252. return &likeOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  253. }
  254. /*
  255. Eval evaluate this runtime component.
  256. */
  257. func (rt *likeOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  258. _, err := rt.baseRuntime.Eval(vs, is)
  259. if err == nil {
  260. errorutil.AssertTrue(len(rt.node.Children) == 2,
  261. fmt.Sprint("Operation requires 2 operands", rt.node))
  262. str, err := rt.node.Children[0].Runtime.Eval(vs, is)
  263. if err == nil {
  264. var pattern interface{}
  265. pattern, err = rt.node.Children[1].Runtime.Eval(vs, is)
  266. if err == nil {
  267. var re *regexp.Regexp
  268. re, err = regexp.Compile(fmt.Sprint(pattern))
  269. if err == nil {
  270. return re.MatchString(fmt.Sprint(str)), nil
  271. }
  272. }
  273. }
  274. }
  275. return nil, err
  276. }
  277. type beginswithOpRuntime struct {
  278. *operatorRuntime
  279. }
  280. /*
  281. beginswithOpRuntimeInst returns a new runtime component instance.
  282. */
  283. func beginswithOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  284. return &beginswithOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  285. }
  286. /*
  287. Eval evaluate this runtime component.
  288. */
  289. func (rt *beginswithOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  290. var res interface{}
  291. _, err := rt.baseRuntime.Eval(vs, is)
  292. if err == nil {
  293. res, err = rt.strOp(func(s1 string, s2 string) interface{} {
  294. return strings.HasPrefix(s1, s2)
  295. }, vs, is)
  296. }
  297. return res, err
  298. }
  299. type endswithOpRuntime struct {
  300. *operatorRuntime
  301. }
  302. /*
  303. endswithOpRuntimeInst returns a new runtime component instance.
  304. */
  305. func endswithOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  306. return &endswithOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  307. }
  308. /*
  309. Eval evaluate this runtime component.
  310. */
  311. func (rt *endswithOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  312. var res interface{}
  313. _, err := rt.baseRuntime.Eval(vs, is)
  314. if err == nil {
  315. res, err = rt.strOp(func(s1 string, s2 string) interface{} {
  316. return strings.HasSuffix(s1, s2)
  317. }, vs, is)
  318. }
  319. return res, err
  320. }
  321. type inOpRuntime struct {
  322. *operatorRuntime
  323. }
  324. /*
  325. inOpRuntimeInst returns a new runtime component instance.
  326. */
  327. func inOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  328. return &inOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  329. }
  330. /*
  331. Eval evaluate this runtime component.
  332. */
  333. func (rt *inOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  334. var res interface{}
  335. _, err := rt.baseRuntime.Eval(vs, is)
  336. if err == nil {
  337. res, err = rt.listOp(func(val interface{}, list []interface{}) interface{} {
  338. for _, i := range list {
  339. if val == i {
  340. return true
  341. }
  342. }
  343. return false
  344. }, vs, is)
  345. }
  346. return res, err
  347. }
  348. type notinOpRuntime struct {
  349. *inOpRuntime
  350. }
  351. /*
  352. notinOpRuntimeInst returns a new runtime component instance.
  353. */
  354. func notinOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  355. return &notinOpRuntime{&inOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}}
  356. }
  357. /*
  358. Eval evaluate this runtime component.
  359. */
  360. func (rt *notinOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  361. var res interface{}
  362. _, err := rt.baseRuntime.Eval(vs, is)
  363. if err == nil {
  364. if res, err = rt.inOpRuntime.Eval(vs, is); err == nil {
  365. res = !res.(bool)
  366. }
  367. }
  368. return res, err
  369. }