rt_boolean.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. var res interface{}
  259. _, err := rt.baseRuntime.Eval(vs, is)
  260. if err == nil {
  261. errorutil.AssertTrue(len(rt.node.Children) == 2,
  262. fmt.Sprint("Operation requires 2 operands", rt.node))
  263. str, err := rt.node.Children[0].Runtime.Eval(vs, is)
  264. if err == nil {
  265. var pattern interface{}
  266. pattern, err = rt.node.Children[1].Runtime.Eval(vs, is)
  267. if err == nil {
  268. var re *regexp.Regexp
  269. re, err = regexp.Compile(fmt.Sprint(pattern))
  270. if err == nil {
  271. res = re.MatchString(fmt.Sprint(str))
  272. }
  273. }
  274. }
  275. }
  276. return res, err
  277. }
  278. type beginswithOpRuntime struct {
  279. *operatorRuntime
  280. }
  281. /*
  282. beginswithOpRuntimeInst returns a new runtime component instance.
  283. */
  284. func beginswithOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  285. return &beginswithOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  286. }
  287. /*
  288. Eval evaluate this runtime component.
  289. */
  290. func (rt *beginswithOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  291. var res interface{}
  292. _, err := rt.baseRuntime.Eval(vs, is)
  293. if err == nil {
  294. res, err = rt.strOp(func(s1 string, s2 string) interface{} {
  295. return strings.HasPrefix(s1, s2)
  296. }, vs, is)
  297. }
  298. return res, err
  299. }
  300. type endswithOpRuntime struct {
  301. *operatorRuntime
  302. }
  303. /*
  304. endswithOpRuntimeInst returns a new runtime component instance.
  305. */
  306. func endswithOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  307. return &endswithOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  308. }
  309. /*
  310. Eval evaluate this runtime component.
  311. */
  312. func (rt *endswithOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  313. var res interface{}
  314. _, err := rt.baseRuntime.Eval(vs, is)
  315. if err == nil {
  316. res, err = rt.strOp(func(s1 string, s2 string) interface{} {
  317. return strings.HasSuffix(s1, s2)
  318. }, vs, is)
  319. }
  320. return res, err
  321. }
  322. type inOpRuntime struct {
  323. *operatorRuntime
  324. }
  325. /*
  326. inOpRuntimeInst returns a new runtime component instance.
  327. */
  328. func inOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  329. return &inOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}
  330. }
  331. /*
  332. Eval evaluate this runtime component.
  333. */
  334. func (rt *inOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  335. var res interface{}
  336. _, err := rt.baseRuntime.Eval(vs, is)
  337. if err == nil {
  338. res, err = rt.listOp(func(val interface{}, list []interface{}) interface{} {
  339. for _, i := range list {
  340. if val == i {
  341. return true
  342. }
  343. }
  344. return false
  345. }, vs, is)
  346. }
  347. return res, err
  348. }
  349. type notinOpRuntime struct {
  350. *inOpRuntime
  351. }
  352. /*
  353. notinOpRuntimeInst returns a new runtime component instance.
  354. */
  355. func notinOpRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  356. return &notinOpRuntime{&inOpRuntime{&operatorRuntime{newBaseRuntime(erp, node)}}}
  357. }
  358. /*
  359. Eval evaluate this runtime component.
  360. */
  361. func (rt *notinOpRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  362. var res interface{}
  363. _, err := rt.baseRuntime.Eval(vs, is)
  364. if err == nil {
  365. if res, err = rt.inOpRuntime.Eval(vs, is); err == nil {
  366. res = !res.(bool)
  367. }
  368. }
  369. return res, err
  370. }