expr.go 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000
  1. // Copyright 2014 The ql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found pIn the LICENSE file.
  4. package ql
  5. import (
  6. "fmt"
  7. "math/big"
  8. "regexp"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. _ expression = (*binaryOperation)(nil)
  14. _ expression = (*call)(nil)
  15. _ expression = (*conversion)(nil)
  16. _ expression = (*ident)(nil)
  17. _ expression = (*indexOp)(nil)
  18. _ expression = (*isNull)(nil)
  19. _ expression = (*pIn)(nil)
  20. _ expression = (*pLike)(nil)
  21. _ expression = (*parameter)(nil)
  22. _ expression = (*pexpr)(nil)
  23. _ expression = (*slice)(nil)
  24. _ expression = (*unaryOperation)(nil)
  25. _ expression = value{}
  26. )
  27. type expression interface {
  28. clone(arg []interface{}, unqualify ...string) (expression, error)
  29. eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error)
  30. isStatic() bool
  31. String() string
  32. }
  33. func cloneExpressionList(arg []interface{}, list []expression, unqualify ...string) ([]expression, error) {
  34. r := make([]expression, len(list))
  35. var err error
  36. for i, v := range list {
  37. if r[i], err = v.clone(arg, unqualify...); err != nil {
  38. return nil, err
  39. }
  40. }
  41. return r, nil
  42. }
  43. func isConstValue(v interface{}) interface{} {
  44. switch x := v.(type) {
  45. case value:
  46. return x.val
  47. case
  48. idealComplex,
  49. idealFloat,
  50. idealInt,
  51. idealRune,
  52. idealUint:
  53. return v
  54. default:
  55. return nil
  56. }
  57. }
  58. func isColumnExpression(v expression) (bool, string) {
  59. x, ok := v.(*ident)
  60. if ok {
  61. return true, x.s
  62. }
  63. c, ok := v.(*call)
  64. if !ok || c.f != "id" || len(c.arg) != 0 {
  65. return false, ""
  66. }
  67. return true, "id()"
  68. }
  69. func mentionedColumns0(e expression, q, nq bool, m map[string]struct{}) {
  70. switch x := e.(type) {
  71. case parameter,
  72. value:
  73. // nop
  74. case *binaryOperation:
  75. mentionedColumns0(x.l, q, nq, m)
  76. mentionedColumns0(x.r, q, nq, m)
  77. case *call:
  78. if x.f != "id" {
  79. for _, e := range x.arg {
  80. mentionedColumns0(e, q, nq, m)
  81. }
  82. }
  83. case *conversion:
  84. mentionedColumns0(x.val, q, nq, m)
  85. case *ident:
  86. if q && x.isQualified() {
  87. m[x.s] = struct{}{}
  88. }
  89. if nq && !x.isQualified() {
  90. m[x.s] = struct{}{}
  91. }
  92. case *indexOp:
  93. mentionedColumns0(x.expr, q, nq, m)
  94. mentionedColumns0(x.x, q, nq, m)
  95. case *isNull:
  96. mentionedColumns0(x.expr, q, nq, m)
  97. case *pexpr:
  98. mentionedColumns0(x.expr, q, nq, m)
  99. case *pIn:
  100. mentionedColumns0(x.expr, q, nq, m)
  101. for _, e := range x.list {
  102. mentionedColumns0(e, q, nq, m)
  103. }
  104. case *pLike:
  105. mentionedColumns0(x.expr, q, nq, m)
  106. mentionedColumns0(x.pattern, q, nq, m)
  107. case *slice:
  108. mentionedColumns0(x.expr, q, nq, m)
  109. if y := x.lo; y != nil {
  110. mentionedColumns0(*y, q, nq, m)
  111. }
  112. if y := x.hi; y != nil {
  113. mentionedColumns0(*y, q, nq, m)
  114. }
  115. case *unaryOperation:
  116. mentionedColumns0(x.v, q, nq, m)
  117. default:
  118. panic("internal error 052")
  119. }
  120. }
  121. func mentionedColumns(e expression) map[string]struct{} {
  122. m := map[string]struct{}{}
  123. mentionedColumns0(e, false, true, m)
  124. return m
  125. }
  126. func staticExpr(e expression) (expression, error) {
  127. if e.isStatic() {
  128. v, err := e.eval(nil, nil)
  129. if err != nil {
  130. return nil, err
  131. }
  132. if v == nil {
  133. return value{nil}, nil
  134. }
  135. return value{v}, nil
  136. }
  137. return e, nil
  138. }
  139. type (
  140. idealComplex complex128
  141. idealFloat float64
  142. idealInt int64
  143. idealRune int32
  144. idealUint uint64
  145. )
  146. type pexpr struct {
  147. expr expression
  148. }
  149. func (p *pexpr) clone(arg []interface{}, unqualify ...string) (expression, error) {
  150. expr, err := p.expr.clone(arg, unqualify...)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return &pexpr{expr: expr}, nil
  155. }
  156. func (p *pexpr) isStatic() bool { return p.expr.isStatic() }
  157. func (p *pexpr) String() string {
  158. return fmt.Sprintf("(%s)", p.expr)
  159. }
  160. func (p *pexpr) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  161. return p.expr.eval(execCtx, ctx)
  162. }
  163. //DONE newBetween
  164. //LATER like newBetween, check all others have and use new*
  165. func newBetween(expr, lo, hi interface{}, not bool) (expression, error) {
  166. e, err := staticExpr(expr.(expression))
  167. if err != nil {
  168. return nil, err
  169. }
  170. l, err := staticExpr(lo.(expression))
  171. if err != nil {
  172. return nil, err
  173. }
  174. h, err := staticExpr(hi.(expression))
  175. if err != nil {
  176. return nil, err
  177. }
  178. var a, b expression
  179. op := andand
  180. switch {
  181. case not: // e < l || e > h
  182. op = oror
  183. if a, err = newBinaryOperation('<', e, l); err != nil {
  184. return nil, err
  185. }
  186. if b, err = newBinaryOperation('>', e, h); err != nil {
  187. return nil, err
  188. }
  189. default: // e >= l && e <= h
  190. if a, err = newBinaryOperation(ge, e, l); err != nil {
  191. return nil, err
  192. }
  193. if b, err = newBinaryOperation(le, e, h); err != nil {
  194. return nil, err
  195. }
  196. }
  197. if a, err = staticExpr(a); err != nil {
  198. return nil, err
  199. }
  200. if b, err = staticExpr(b); err != nil {
  201. return nil, err
  202. }
  203. ret, err := newBinaryOperation(op, a, b)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return staticExpr(ret)
  208. }
  209. type pLike struct {
  210. expr expression
  211. pattern expression
  212. re *regexp.Regexp
  213. sexpr *string
  214. }
  215. func (p *pLike) clone(arg []interface{}, unqualify ...string) (expression, error) {
  216. expr, err := p.expr.clone(arg, unqualify...)
  217. if err != nil {
  218. return nil, err
  219. }
  220. pattern, err := p.pattern.clone(arg, unqualify...)
  221. if err != nil {
  222. return nil, err
  223. }
  224. return &pLike{
  225. expr: expr,
  226. pattern: pattern,
  227. re: p.re,
  228. sexpr: p.sexpr,
  229. }, nil
  230. }
  231. func (p *pLike) isStatic() bool { return p.expr.isStatic() && p.pattern.isStatic() }
  232. func (p *pLike) String() string { return fmt.Sprintf("%s LIKE %s", p.expr, p.pattern) }
  233. func (p *pLike) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  234. var sexpr string
  235. var ok bool
  236. switch {
  237. case p.sexpr != nil:
  238. sexpr = *p.sexpr
  239. default:
  240. expr, err := expand1(p.expr.eval(execCtx, ctx))
  241. if err != nil {
  242. return nil, err
  243. }
  244. if expr == nil {
  245. return nil, nil
  246. }
  247. sexpr, ok = expr.(string)
  248. if !ok {
  249. return nil, fmt.Errorf("non-string expression in LIKE: %v (value of type %T)", expr, expr)
  250. }
  251. if p.expr.isStatic() {
  252. p.sexpr = new(string)
  253. *p.sexpr = sexpr
  254. }
  255. }
  256. re := p.re
  257. if re == nil {
  258. pattern, err := expand1(p.pattern.eval(execCtx, ctx))
  259. if err != nil {
  260. return nil, err
  261. }
  262. if pattern == nil {
  263. return nil, nil
  264. }
  265. spattern, ok := pattern.(string)
  266. if !ok {
  267. return nil, fmt.Errorf("non-string pattern in LIKE: %v (value of type %T)", pattern, pattern)
  268. }
  269. if re, err = regexp.Compile(spattern); err != nil {
  270. return nil, err
  271. }
  272. if p.pattern.isStatic() {
  273. p.re = re
  274. }
  275. }
  276. return re.MatchString(sexpr), nil
  277. }
  278. type binaryOperation struct {
  279. op int
  280. l, r expression
  281. }
  282. func newBinaryOperation0(op int, x, y interface{}) (v expression, err error) {
  283. if op == eq {
  284. if l, ok := x.(value); ok {
  285. if b, ok := l.val.(bool); ok {
  286. if b { // true == y: y
  287. return y.(expression), nil
  288. }
  289. // false == y: !y
  290. return newUnaryOperation('!', y)
  291. }
  292. }
  293. if r, ok := y.(value); ok {
  294. if b, ok := r.val.(bool); ok {
  295. if b { // x == true: x
  296. return x.(expression), nil
  297. }
  298. // x == false: !x
  299. return newUnaryOperation('!', x)
  300. }
  301. }
  302. }
  303. if op == neq {
  304. if l, ok := x.(value); ok {
  305. if b, ok := l.val.(bool); ok {
  306. if b { // true != y: !y
  307. return newUnaryOperation('!', y)
  308. }
  309. // false != y: y
  310. return y.(expression), nil
  311. }
  312. }
  313. if r, ok := y.(value); ok {
  314. if b, ok := r.val.(bool); ok {
  315. if b { // x != true: !x
  316. return newUnaryOperation('!', x)
  317. }
  318. // x != false: x
  319. return x.(expression), nil
  320. }
  321. }
  322. }
  323. b := binaryOperation{op, x.(expression), y.(expression)}
  324. var lv interface{}
  325. if e := b.l; e.isStatic() {
  326. if lv, err = e.eval(nil, nil); err != nil {
  327. return nil, err
  328. }
  329. b.l = value{lv}
  330. }
  331. if e := b.r; e.isStatic() {
  332. v, err := e.eval(nil, nil)
  333. if err != nil {
  334. return nil, err
  335. }
  336. if v == nil {
  337. return value{nil}, nil
  338. }
  339. if op == '/' || op == '%' {
  340. rb := binaryOperation{eq, e, value{idealInt(0)}}
  341. val, err := rb.eval(nil, nil)
  342. if err != nil {
  343. return nil, err
  344. }
  345. if val.(bool) {
  346. return nil, errDivByZero
  347. }
  348. }
  349. if b.l.isStatic() && lv == nil {
  350. return value{nil}, nil
  351. }
  352. b.r = value{v}
  353. }
  354. if !b.isStatic() {
  355. return &b, nil
  356. }
  357. val, err := b.eval(nil, nil)
  358. return value{val}, err
  359. }
  360. func newBinaryOperation(op int, x, y interface{}) (v expression, err error) {
  361. expr, err := newBinaryOperation0(op, x, y)
  362. if err != nil {
  363. return nil, err
  364. }
  365. b, ok := expr.(*binaryOperation)
  366. if !ok {
  367. return expr, nil
  368. }
  369. if _, ok := b.l.(*ident); ok {
  370. return expr, nil
  371. }
  372. if c, ok := b.l.(*call); ok && c.f == "id" {
  373. return expr, nil
  374. }
  375. var r expression
  376. if r, ok = b.r.(*ident); !ok {
  377. r1, ok := b.r.(*call)
  378. if !ok || r1.f != "id" || len(r1.arg) != 0 {
  379. return expr, nil
  380. }
  381. r = r1
  382. }
  383. // Normalize expr relOp indent: ident invRelOp expr
  384. switch b.op {
  385. case '<':
  386. return &binaryOperation{'>', r, b.l}, nil
  387. case le:
  388. return &binaryOperation{ge, r, b.l}, nil
  389. case '>':
  390. return &binaryOperation{'<', r, b.l}, nil
  391. case ge:
  392. return &binaryOperation{le, r, b.l}, nil
  393. case eq, neq:
  394. return &binaryOperation{b.op, r, b.l}, nil
  395. default:
  396. return expr, nil
  397. }
  398. }
  399. func (o *binaryOperation) isIdentRelOpVal() (bool, string, interface{}, error) {
  400. sid := ""
  401. id, ok := o.l.(*ident)
  402. if !ok {
  403. f, ok := o.l.(*call)
  404. if !ok || f.f != "id" || len(f.arg) != 0 {
  405. return false, "", nil, nil
  406. }
  407. sid = "id()"
  408. } else {
  409. if id.isQualified() {
  410. return false, "", nil, nil
  411. }
  412. sid = id.s
  413. }
  414. if v, ok := o.r.(value); ok {
  415. switch o.op {
  416. case '<',
  417. le,
  418. '>',
  419. ge,
  420. eq,
  421. neq:
  422. return true, sid, v.val, nil
  423. default:
  424. return false, "", nil, nil
  425. }
  426. }
  427. return false, "", nil, nil
  428. }
  429. func (o *binaryOperation) clone(arg []interface{}, unqualify ...string) (expression, error) {
  430. l, err := o.l.clone(arg, unqualify...)
  431. if err != nil {
  432. return nil, err
  433. }
  434. r, err := o.r.clone(arg, unqualify...)
  435. if err != nil {
  436. return nil, err
  437. }
  438. return newBinaryOperation(o.op, l, r)
  439. }
  440. func (o *binaryOperation) isStatic() bool { return o.l.isStatic() && o.r.isStatic() }
  441. func (o *binaryOperation) String() string {
  442. return fmt.Sprintf("%s %s %s", o.l, iop(o.op), o.r)
  443. }
  444. func (o *binaryOperation) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (r interface{}, err error) {
  445. defer func() {
  446. if e := recover(); e != nil {
  447. switch x := e.(type) {
  448. case error:
  449. r, err = nil, x
  450. default:
  451. r, err = nil, fmt.Errorf("%v", x)
  452. }
  453. }
  454. }()
  455. switch op := o.op; op {
  456. case andand:
  457. a, err := expand1(o.l.eval(execCtx, ctx))
  458. if err != nil {
  459. return nil, err
  460. }
  461. switch x := a.(type) {
  462. case nil:
  463. b, err := expand1(o.r.eval(execCtx, ctx))
  464. if err != nil {
  465. return nil, err
  466. }
  467. switch y := b.(type) {
  468. case nil:
  469. return nil, nil
  470. case bool:
  471. if !y {
  472. return false, nil
  473. }
  474. return nil, nil
  475. default:
  476. return invOp2(x, y, op)
  477. }
  478. case bool:
  479. if !x {
  480. return false, nil
  481. }
  482. b, err := expand1(o.r.eval(execCtx, ctx))
  483. if err != nil {
  484. return nil, err
  485. }
  486. switch y := b.(type) {
  487. case nil:
  488. return nil, nil
  489. case bool:
  490. return y, nil
  491. default:
  492. return invOp2(x, y, op)
  493. }
  494. default:
  495. return undOp(x, op)
  496. }
  497. case oror:
  498. a, err := expand1(o.l.eval(execCtx, ctx))
  499. if err != nil {
  500. return nil, err
  501. }
  502. switch x := a.(type) {
  503. case nil:
  504. b, err := expand1(o.r.eval(execCtx, ctx))
  505. if err != nil {
  506. return nil, err
  507. }
  508. switch y := b.(type) {
  509. case nil:
  510. return nil, nil
  511. case bool:
  512. if y {
  513. return y, nil
  514. }
  515. return nil, nil
  516. default:
  517. return invOp2(x, y, op)
  518. }
  519. case bool:
  520. if x {
  521. return x, nil
  522. }
  523. b, err := expand1(o.r.eval(execCtx, ctx))
  524. if err != nil {
  525. return nil, err
  526. }
  527. switch y := b.(type) {
  528. case nil:
  529. return nil, nil
  530. case bool:
  531. return y, nil
  532. default:
  533. return invOp2(x, y, op)
  534. }
  535. default:
  536. return undOp(x, op)
  537. }
  538. case '>':
  539. a, b := o.get2(execCtx, ctx)
  540. if a == nil || b == nil {
  541. return
  542. }
  543. switch x := a.(type) {
  544. //case nil:
  545. case idealComplex:
  546. return undOp2(a, b, op)
  547. case idealFloat:
  548. switch y := b.(type) {
  549. case idealFloat:
  550. return x > y, nil
  551. default:
  552. return invOp2(x, y, op)
  553. }
  554. case idealInt:
  555. switch y := b.(type) {
  556. case idealInt:
  557. return x > y, nil
  558. default:
  559. return invOp2(x, y, op)
  560. }
  561. case idealRune:
  562. switch y := b.(type) {
  563. case idealRune:
  564. return x > y, nil
  565. default:
  566. return invOp2(x, y, op)
  567. }
  568. case idealUint:
  569. switch y := b.(type) {
  570. case idealUint:
  571. return x > y, nil
  572. default:
  573. return invOp2(x, y, op)
  574. }
  575. case bool:
  576. return undOp2(a, b, op)
  577. case complex64:
  578. return undOp2(a, b, op)
  579. case complex128:
  580. return undOp2(a, b, op)
  581. case float32:
  582. switch y := b.(type) {
  583. case float32:
  584. return x > y, nil
  585. default:
  586. return invOp2(x, y, op)
  587. }
  588. case float64:
  589. switch y := b.(type) {
  590. case float64:
  591. return x > y, nil
  592. default:
  593. return invOp2(x, y, op)
  594. }
  595. case int8:
  596. switch y := b.(type) {
  597. case int8:
  598. return x > y, nil
  599. default:
  600. return invOp2(x, y, op)
  601. }
  602. case int16:
  603. switch y := b.(type) {
  604. case int16:
  605. return x > y, nil
  606. default:
  607. return invOp2(x, y, op)
  608. }
  609. case int32:
  610. switch y := b.(type) {
  611. case int32:
  612. return x > y, nil
  613. default:
  614. return invOp2(x, y, op)
  615. }
  616. case int64:
  617. switch y := b.(type) {
  618. case int64:
  619. return x > y, nil
  620. default:
  621. return invOp2(x, y, op)
  622. }
  623. case string:
  624. switch y := b.(type) {
  625. case string:
  626. return x > y, nil
  627. default:
  628. return invOp2(x, y, op)
  629. }
  630. case uint8:
  631. switch y := b.(type) {
  632. case uint8:
  633. return x > y, nil
  634. default:
  635. return invOp2(x, y, op)
  636. }
  637. case uint16:
  638. switch y := b.(type) {
  639. case uint16:
  640. return x > y, nil
  641. default:
  642. return invOp2(x, y, op)
  643. }
  644. case uint32:
  645. switch y := b.(type) {
  646. case uint32:
  647. return x > y, nil
  648. default:
  649. return invOp2(x, y, op)
  650. }
  651. case uint64:
  652. switch y := b.(type) {
  653. case uint64:
  654. return x > y, nil
  655. default:
  656. return invOp2(x, y, op)
  657. }
  658. case *big.Int:
  659. switch y := b.(type) {
  660. case *big.Int:
  661. return x.Cmp(y) > 0, nil
  662. default:
  663. return invOp2(x, y, op)
  664. }
  665. case *big.Rat:
  666. switch y := b.(type) {
  667. case *big.Rat:
  668. return x.Cmp(y) > 0, nil
  669. default:
  670. return invOp2(x, y, op)
  671. }
  672. case time.Duration:
  673. switch y := b.(type) {
  674. case time.Duration:
  675. return x > y, nil
  676. default:
  677. return invOp2(x, y, op)
  678. }
  679. case time.Time:
  680. switch y := b.(type) {
  681. case time.Time:
  682. return x.After(y), nil
  683. default:
  684. return invOp2(x, y, op)
  685. }
  686. default:
  687. return invOp2(a, b, op)
  688. }
  689. case '<':
  690. a, b := o.get2(execCtx, ctx)
  691. if a == nil || b == nil {
  692. return
  693. }
  694. switch x := a.(type) {
  695. //case nil:
  696. case idealComplex:
  697. return undOp2(a, b, op)
  698. case idealFloat:
  699. switch y := b.(type) {
  700. case idealFloat:
  701. return x < y, nil
  702. default:
  703. return invOp2(x, y, op)
  704. }
  705. case idealInt:
  706. switch y := b.(type) {
  707. case idealInt:
  708. return x < y, nil
  709. default:
  710. return invOp2(x, y, op)
  711. }
  712. case idealRune:
  713. switch y := b.(type) {
  714. case idealRune:
  715. return x < y, nil
  716. default:
  717. return invOp2(x, y, op)
  718. }
  719. case idealUint:
  720. switch y := b.(type) {
  721. case idealUint:
  722. return x < y, nil
  723. default:
  724. return invOp2(x, y, op)
  725. }
  726. case bool:
  727. return undOp2(a, b, op)
  728. case complex64:
  729. return undOp2(a, b, op)
  730. case complex128:
  731. return undOp2(a, b, op)
  732. case float32:
  733. switch y := b.(type) {
  734. case float32:
  735. return x < y, nil
  736. default:
  737. return invOp2(x, y, op)
  738. }
  739. case float64:
  740. switch y := b.(type) {
  741. case float64:
  742. return x < y, nil
  743. default:
  744. return invOp2(x, y, op)
  745. }
  746. case int8:
  747. switch y := b.(type) {
  748. case int8:
  749. return x < y, nil
  750. default:
  751. return invOp2(x, y, op)
  752. }
  753. case int16:
  754. switch y := b.(type) {
  755. case int16:
  756. return x < y, nil
  757. default:
  758. return invOp2(x, y, op)
  759. }
  760. case int32:
  761. switch y := b.(type) {
  762. case int32:
  763. return x < y, nil
  764. default:
  765. return invOp2(x, y, op)
  766. }
  767. case int64:
  768. switch y := b.(type) {
  769. case int64:
  770. return x < y, nil
  771. default:
  772. return invOp2(x, y, op)
  773. }
  774. case string:
  775. switch y := b.(type) {
  776. case string:
  777. return x < y, nil
  778. default:
  779. return invOp2(x, y, op)
  780. }
  781. case uint8:
  782. switch y := b.(type) {
  783. case uint8:
  784. return x < y, nil
  785. default:
  786. return invOp2(x, y, op)
  787. }
  788. case uint16:
  789. switch y := b.(type) {
  790. case uint16:
  791. return x < y, nil
  792. default:
  793. return invOp2(x, y, op)
  794. }
  795. case uint32:
  796. switch y := b.(type) {
  797. case uint32:
  798. return x < y, nil
  799. default:
  800. return invOp2(x, y, op)
  801. }
  802. case uint64:
  803. switch y := b.(type) {
  804. case uint64:
  805. return x < y, nil
  806. default:
  807. return invOp2(x, y, op)
  808. }
  809. case *big.Int:
  810. switch y := b.(type) {
  811. case *big.Int:
  812. return x.Cmp(y) < 0, nil
  813. default:
  814. return invOp2(x, y, op)
  815. }
  816. case *big.Rat:
  817. switch y := b.(type) {
  818. case *big.Rat:
  819. return x.Cmp(y) < 0, nil
  820. default:
  821. return invOp2(x, y, op)
  822. }
  823. case time.Duration:
  824. switch y := b.(type) {
  825. case time.Duration:
  826. return x < y, nil
  827. default:
  828. return invOp2(x, y, op)
  829. }
  830. case time.Time:
  831. switch y := b.(type) {
  832. case time.Time:
  833. return x.Before(y), nil
  834. default:
  835. return invOp2(x, y, op)
  836. }
  837. default:
  838. return invOp2(a, b, op)
  839. }
  840. case le:
  841. a, b := o.get2(execCtx, ctx)
  842. if a == nil || b == nil {
  843. return
  844. }
  845. switch x := a.(type) {
  846. //case nil:
  847. case idealComplex:
  848. return undOp2(a, b, op)
  849. case idealFloat:
  850. switch y := b.(type) {
  851. case idealFloat:
  852. return x <= y, nil
  853. default:
  854. return invOp2(x, y, op)
  855. }
  856. case idealInt:
  857. switch y := b.(type) {
  858. case idealInt:
  859. return x <= y, nil
  860. default:
  861. return invOp2(x, y, op)
  862. }
  863. case idealRune:
  864. switch y := b.(type) {
  865. case idealRune:
  866. return x <= y, nil
  867. default:
  868. return invOp2(x, y, op)
  869. }
  870. case idealUint:
  871. switch y := b.(type) {
  872. case idealUint:
  873. return x <= y, nil
  874. default:
  875. return invOp2(x, y, op)
  876. }
  877. case bool:
  878. return undOp2(a, b, op)
  879. case complex64:
  880. return undOp2(a, b, op)
  881. case complex128:
  882. return undOp2(a, b, op)
  883. case float32:
  884. switch y := b.(type) {
  885. case float32:
  886. return x <= y, nil
  887. default:
  888. return invOp2(x, y, op)
  889. }
  890. case float64:
  891. switch y := b.(type) {
  892. case float64:
  893. return x <= y, nil
  894. default:
  895. return invOp2(x, y, op)
  896. }
  897. case int8:
  898. switch y := b.(type) {
  899. case int8:
  900. return x <= y, nil
  901. default:
  902. return invOp2(x, y, op)
  903. }
  904. case int16:
  905. switch y := b.(type) {
  906. case int16:
  907. return x <= y, nil
  908. default:
  909. return invOp2(x, y, op)
  910. }
  911. case int32:
  912. switch y := b.(type) {
  913. case int32:
  914. return x <= y, nil
  915. default:
  916. return invOp2(x, y, op)
  917. }
  918. case int64:
  919. switch y := b.(type) {
  920. case int64:
  921. return x <= y, nil
  922. default:
  923. return invOp2(x, y, op)
  924. }
  925. case string:
  926. switch y := b.(type) {
  927. case string:
  928. return x <= y, nil
  929. default:
  930. return invOp2(x, y, op)
  931. }
  932. case uint8:
  933. switch y := b.(type) {
  934. case uint8:
  935. return x <= y, nil
  936. default:
  937. return invOp2(x, y, op)
  938. }
  939. case uint16:
  940. switch y := b.(type) {
  941. case uint16:
  942. return x <= y, nil
  943. default:
  944. return invOp2(x, y, op)
  945. }
  946. case uint32:
  947. switch y := b.(type) {
  948. case uint32:
  949. return x <= y, nil
  950. default:
  951. return invOp2(x, y, op)
  952. }
  953. case uint64:
  954. switch y := b.(type) {
  955. case uint64:
  956. return x <= y, nil
  957. default:
  958. return invOp2(x, y, op)
  959. }
  960. case *big.Int:
  961. switch y := b.(type) {
  962. case *big.Int:
  963. return x.Cmp(y) <= 0, nil
  964. default:
  965. return invOp2(x, y, op)
  966. }
  967. case *big.Rat:
  968. switch y := b.(type) {
  969. case *big.Rat:
  970. return x.Cmp(y) <= 0, nil
  971. default:
  972. return invOp2(x, y, op)
  973. }
  974. case time.Duration:
  975. switch y := b.(type) {
  976. case time.Duration:
  977. return x <= y, nil
  978. default:
  979. return invOp2(x, y, op)
  980. }
  981. case time.Time:
  982. switch y := b.(type) {
  983. case time.Time:
  984. return x.Before(y) || x.Equal(y), nil
  985. default:
  986. return invOp2(x, y, op)
  987. }
  988. default:
  989. return invOp2(a, b, op)
  990. }
  991. case ge:
  992. a, b := o.get2(execCtx, ctx)
  993. if a == nil || b == nil {
  994. return
  995. }
  996. switch x := a.(type) {
  997. //case nil:
  998. case idealComplex:
  999. return undOp2(a, b, op)
  1000. case idealFloat:
  1001. switch y := b.(type) {
  1002. case idealFloat:
  1003. return x >= y, nil
  1004. default:
  1005. return invOp2(x, y, op)
  1006. }
  1007. case idealInt:
  1008. switch y := b.(type) {
  1009. case idealInt:
  1010. return x >= y, nil
  1011. default:
  1012. return invOp2(x, y, op)
  1013. }
  1014. case idealRune:
  1015. switch y := b.(type) {
  1016. case idealRune:
  1017. return x >= y, nil
  1018. default:
  1019. return invOp2(x, y, op)
  1020. }
  1021. case idealUint:
  1022. switch y := b.(type) {
  1023. case idealUint:
  1024. return x >= y, nil
  1025. default:
  1026. return invOp2(x, y, op)
  1027. }
  1028. case bool:
  1029. return undOp2(a, b, op)
  1030. case complex64:
  1031. return undOp2(a, b, op)
  1032. case complex128:
  1033. return undOp2(a, b, op)
  1034. case float32:
  1035. switch y := b.(type) {
  1036. case float32:
  1037. return x >= y, nil
  1038. default:
  1039. return invOp2(x, y, op)
  1040. }
  1041. case float64:
  1042. switch y := b.(type) {
  1043. case float64:
  1044. return x >= y, nil
  1045. default:
  1046. return invOp2(x, y, op)
  1047. }
  1048. case int8:
  1049. switch y := b.(type) {
  1050. case int8:
  1051. return x >= y, nil
  1052. default:
  1053. return invOp2(x, y, op)
  1054. }
  1055. case int16:
  1056. switch y := b.(type) {
  1057. case int16:
  1058. return x >= y, nil
  1059. default:
  1060. return invOp2(x, y, op)
  1061. }
  1062. case int32:
  1063. switch y := b.(type) {
  1064. case int32:
  1065. return x >= y, nil
  1066. default:
  1067. return invOp2(x, y, op)
  1068. }
  1069. case int64:
  1070. switch y := b.(type) {
  1071. case int64:
  1072. return x >= y, nil
  1073. default:
  1074. return invOp2(x, y, op)
  1075. }
  1076. case string:
  1077. switch y := b.(type) {
  1078. case string:
  1079. return x >= y, nil
  1080. default:
  1081. return invOp2(x, y, op)
  1082. }
  1083. case uint8:
  1084. switch y := b.(type) {
  1085. case uint8:
  1086. return x >= y, nil
  1087. default:
  1088. return invOp2(x, y, op)
  1089. }
  1090. case uint16:
  1091. switch y := b.(type) {
  1092. case uint16:
  1093. return x >= y, nil
  1094. default:
  1095. return invOp2(x, y, op)
  1096. }
  1097. case uint32:
  1098. switch y := b.(type) {
  1099. case uint32:
  1100. return x >= y, nil
  1101. default:
  1102. return invOp2(x, y, op)
  1103. }
  1104. case uint64:
  1105. switch y := b.(type) {
  1106. case uint64:
  1107. return x >= y, nil
  1108. default:
  1109. return invOp2(x, y, op)
  1110. }
  1111. case *big.Int:
  1112. switch y := b.(type) {
  1113. case *big.Int:
  1114. return x.Cmp(y) >= 0, nil
  1115. default:
  1116. return invOp2(x, y, op)
  1117. }
  1118. case *big.Rat:
  1119. switch y := b.(type) {
  1120. case *big.Rat:
  1121. return x.Cmp(y) >= 0, nil
  1122. default:
  1123. return invOp2(x, y, op)
  1124. }
  1125. case time.Duration:
  1126. switch y := b.(type) {
  1127. case time.Duration:
  1128. return x >= y, nil
  1129. default:
  1130. return invOp2(x, y, op)
  1131. }
  1132. case time.Time:
  1133. switch y := b.(type) {
  1134. case time.Time:
  1135. return x.After(y) || x.Equal(y), nil
  1136. default:
  1137. return invOp2(x, y, op)
  1138. }
  1139. default:
  1140. return invOp2(a, b, op)
  1141. }
  1142. case neq:
  1143. a, b := o.get2(execCtx, ctx)
  1144. if a == nil || b == nil {
  1145. return
  1146. }
  1147. switch x := a.(type) {
  1148. //case nil:
  1149. case idealComplex:
  1150. switch y := b.(type) {
  1151. case idealComplex:
  1152. return x != y, nil
  1153. default:
  1154. return invOp2(x, y, op)
  1155. }
  1156. case idealFloat:
  1157. switch y := b.(type) {
  1158. case idealFloat:
  1159. return x != y, nil
  1160. default:
  1161. return invOp2(x, y, op)
  1162. }
  1163. case idealInt:
  1164. switch y := b.(type) {
  1165. case idealInt:
  1166. return x != y, nil
  1167. default:
  1168. return invOp2(x, y, op)
  1169. }
  1170. case idealRune:
  1171. switch y := b.(type) {
  1172. case idealRune:
  1173. return x != y, nil
  1174. default:
  1175. return invOp2(x, y, op)
  1176. }
  1177. case idealUint:
  1178. switch y := b.(type) {
  1179. case idealUint:
  1180. return x != y, nil
  1181. default:
  1182. return invOp2(x, y, op)
  1183. }
  1184. case bool:
  1185. switch y := b.(type) {
  1186. case bool:
  1187. return x != y, nil
  1188. default:
  1189. return invOp2(x, y, op)
  1190. }
  1191. case complex64:
  1192. switch y := b.(type) {
  1193. case complex64:
  1194. return x != y, nil
  1195. default:
  1196. return invOp2(x, y, op)
  1197. }
  1198. case complex128:
  1199. switch y := b.(type) {
  1200. case complex128:
  1201. return x != y, nil
  1202. default:
  1203. return invOp2(x, y, op)
  1204. }
  1205. case float32:
  1206. switch y := b.(type) {
  1207. case float32:
  1208. return x != y, nil
  1209. default:
  1210. return invOp2(x, y, op)
  1211. }
  1212. case float64:
  1213. switch y := b.(type) {
  1214. case float64:
  1215. return x != y, nil
  1216. default:
  1217. return invOp2(x, y, op)
  1218. }
  1219. case int8:
  1220. switch y := b.(type) {
  1221. case int8:
  1222. return x != y, nil
  1223. default:
  1224. return invOp2(x, y, op)
  1225. }
  1226. case int16:
  1227. switch y := b.(type) {
  1228. case int16:
  1229. return x != y, nil
  1230. default:
  1231. return invOp2(x, y, op)
  1232. }
  1233. case int32:
  1234. switch y := b.(type) {
  1235. case int32:
  1236. return x != y, nil
  1237. default:
  1238. return invOp2(x, y, op)
  1239. }
  1240. case int64:
  1241. switch y := b.(type) {
  1242. case int64:
  1243. return x != y, nil
  1244. default:
  1245. return invOp2(x, y, op)
  1246. }
  1247. case string:
  1248. switch y := b.(type) {
  1249. case string:
  1250. return x != y, nil
  1251. default:
  1252. return invOp2(x, y, op)
  1253. }
  1254. case uint8:
  1255. switch y := b.(type) {
  1256. case uint8:
  1257. return x != y, nil
  1258. default:
  1259. return invOp2(x, y, op)
  1260. }
  1261. case uint16:
  1262. switch y := b.(type) {
  1263. case uint16:
  1264. return x != y, nil
  1265. default:
  1266. return invOp2(x, y, op)
  1267. }
  1268. case uint32:
  1269. switch y := b.(type) {
  1270. case uint32:
  1271. return x != y, nil
  1272. default:
  1273. return invOp2(x, y, op)
  1274. }
  1275. case uint64:
  1276. switch y := b.(type) {
  1277. case uint64:
  1278. return x != y, nil
  1279. default:
  1280. return invOp2(x, y, op)
  1281. }
  1282. case *big.Int:
  1283. switch y := b.(type) {
  1284. case *big.Int:
  1285. return x.Cmp(y) != 0, nil
  1286. default:
  1287. return invOp2(x, y, op)
  1288. }
  1289. case *big.Rat:
  1290. switch y := b.(type) {
  1291. case *big.Rat:
  1292. return x.Cmp(y) != 0, nil
  1293. default:
  1294. return invOp2(x, y, op)
  1295. }
  1296. case time.Duration:
  1297. switch y := b.(type) {
  1298. case time.Duration:
  1299. return x != y, nil
  1300. default:
  1301. return invOp2(x, y, op)
  1302. }
  1303. case time.Time:
  1304. switch y := b.(type) {
  1305. case time.Time:
  1306. return !x.Equal(y), nil
  1307. default:
  1308. return invOp2(x, y, op)
  1309. }
  1310. default:
  1311. return invOp2(a, b, op)
  1312. }
  1313. case eq:
  1314. a, b := o.get2(execCtx, ctx)
  1315. if a == nil || b == nil {
  1316. return
  1317. }
  1318. switch x := a.(type) {
  1319. //case nil:
  1320. case idealComplex:
  1321. switch y := b.(type) {
  1322. case idealComplex:
  1323. return x == y, nil
  1324. default:
  1325. return invOp2(x, y, op)
  1326. }
  1327. case idealFloat:
  1328. switch y := b.(type) {
  1329. case idealFloat:
  1330. return x == y, nil
  1331. default:
  1332. return invOp2(x, y, op)
  1333. }
  1334. case idealInt:
  1335. switch y := b.(type) {
  1336. case idealInt:
  1337. return x == y, nil
  1338. default:
  1339. return invOp2(x, y, op)
  1340. }
  1341. case idealRune:
  1342. switch y := b.(type) {
  1343. case idealRune:
  1344. return x == y, nil
  1345. default:
  1346. return invOp2(x, y, op)
  1347. }
  1348. case idealUint:
  1349. switch y := b.(type) {
  1350. case idealUint:
  1351. return x == y, nil
  1352. default:
  1353. return invOp2(x, y, op)
  1354. }
  1355. case bool:
  1356. switch y := b.(type) {
  1357. case bool:
  1358. return x == y, nil
  1359. default:
  1360. return invOp2(x, y, op)
  1361. }
  1362. case complex64:
  1363. switch y := b.(type) {
  1364. case complex64:
  1365. return x == y, nil
  1366. default:
  1367. return invOp2(x, y, op)
  1368. }
  1369. case complex128:
  1370. switch y := b.(type) {
  1371. case complex128:
  1372. return x == y, nil
  1373. default:
  1374. return invOp2(x, y, op)
  1375. }
  1376. case float32:
  1377. switch y := b.(type) {
  1378. case float32:
  1379. return x == y, nil
  1380. default:
  1381. return invOp2(x, y, op)
  1382. }
  1383. case float64:
  1384. switch y := b.(type) {
  1385. case float64:
  1386. return x == y, nil
  1387. default:
  1388. return invOp2(x, y, op)
  1389. }
  1390. case int8:
  1391. switch y := b.(type) {
  1392. case int8:
  1393. return x == y, nil
  1394. default:
  1395. return invOp2(x, y, op)
  1396. }
  1397. case int16:
  1398. switch y := b.(type) {
  1399. case int16:
  1400. return x == y, nil
  1401. default:
  1402. return invOp2(x, y, op)
  1403. }
  1404. case int32:
  1405. switch y := b.(type) {
  1406. case int32:
  1407. return x == y, nil
  1408. default:
  1409. return invOp2(x, y, op)
  1410. }
  1411. case int64:
  1412. switch y := b.(type) {
  1413. case int64:
  1414. return x == y, nil
  1415. default:
  1416. return invOp2(x, y, op)
  1417. }
  1418. case string:
  1419. switch y := b.(type) {
  1420. case string:
  1421. return x == y, nil
  1422. default:
  1423. return invOp2(x, y, op)
  1424. }
  1425. case uint8:
  1426. switch y := b.(type) {
  1427. case uint8:
  1428. return x == y, nil
  1429. default:
  1430. return invOp2(x, y, op)
  1431. }
  1432. case uint16:
  1433. switch y := b.(type) {
  1434. case uint16:
  1435. return x == y, nil
  1436. default:
  1437. return invOp2(x, y, op)
  1438. }
  1439. case uint32:
  1440. switch y := b.(type) {
  1441. case uint32:
  1442. return x == y, nil
  1443. default:
  1444. return invOp2(x, y, op)
  1445. }
  1446. case uint64:
  1447. switch y := b.(type) {
  1448. case uint64:
  1449. return x == y, nil
  1450. default:
  1451. return invOp2(x, y, op)
  1452. }
  1453. case *big.Int:
  1454. switch y := b.(type) {
  1455. case *big.Int:
  1456. return x.Cmp(y) == 0, nil
  1457. default:
  1458. return invOp2(x, y, op)
  1459. }
  1460. case *big.Rat:
  1461. switch y := b.(type) {
  1462. case *big.Rat:
  1463. return x.Cmp(y) == 0, nil
  1464. default:
  1465. return invOp2(x, y, op)
  1466. }
  1467. case time.Duration:
  1468. switch y := b.(type) {
  1469. case time.Duration:
  1470. return x == y, nil
  1471. default:
  1472. return invOp2(x, y, op)
  1473. }
  1474. case time.Time:
  1475. switch y := b.(type) {
  1476. case time.Time:
  1477. return x.Equal(y), nil
  1478. default:
  1479. return invOp2(x, y, op)
  1480. }
  1481. default:
  1482. return invOp2(a, b, op)
  1483. }
  1484. case '+':
  1485. a, b := o.get2(execCtx, ctx)
  1486. if a == nil || b == nil {
  1487. return
  1488. }
  1489. switch x := a.(type) {
  1490. //case nil:
  1491. case idealComplex:
  1492. switch y := b.(type) {
  1493. case idealComplex:
  1494. return idealComplex(complex64(x) + complex64(y)), nil
  1495. default:
  1496. return invOp2(x, y, op)
  1497. }
  1498. case idealFloat:
  1499. switch y := b.(type) {
  1500. case idealFloat:
  1501. return idealFloat(float64(x) + float64(y)), nil
  1502. default:
  1503. return invOp2(x, y, op)
  1504. }
  1505. case idealInt:
  1506. switch y := b.(type) {
  1507. case idealInt:
  1508. return idealInt(int64(x) + int64(y)), nil
  1509. default:
  1510. return invOp2(x, y, op)
  1511. }
  1512. case idealRune:
  1513. switch y := b.(type) {
  1514. case idealRune:
  1515. return idealRune(int64(x) + int64(y)), nil
  1516. default:
  1517. return invOp2(x, y, op)
  1518. }
  1519. case idealUint:
  1520. switch y := b.(type) {
  1521. case idealUint:
  1522. return idealUint(uint64(x) + uint64(y)), nil
  1523. default:
  1524. return invOp2(x, y, op)
  1525. }
  1526. case bool:
  1527. return undOp2(a, b, op)
  1528. case complex64:
  1529. switch y := b.(type) {
  1530. case complex64:
  1531. return x + y, nil
  1532. default:
  1533. return invOp2(x, y, op)
  1534. }
  1535. case complex128:
  1536. switch y := b.(type) {
  1537. case complex128:
  1538. return x + y, nil
  1539. default:
  1540. return invOp2(x, y, op)
  1541. }
  1542. case float32:
  1543. switch y := b.(type) {
  1544. case float32:
  1545. return x + y, nil
  1546. default:
  1547. return invOp2(x, y, op)
  1548. }
  1549. case float64:
  1550. switch y := b.(type) {
  1551. case float64:
  1552. return x + y, nil
  1553. default:
  1554. return invOp2(x, y, op)
  1555. }
  1556. case int8:
  1557. switch y := b.(type) {
  1558. case int8:
  1559. return x + y, nil
  1560. default:
  1561. return invOp2(x, y, op)
  1562. }
  1563. case int16:
  1564. switch y := b.(type) {
  1565. case int16:
  1566. return x + y, nil
  1567. default:
  1568. return invOp2(x, y, op)
  1569. }
  1570. case int32:
  1571. switch y := b.(type) {
  1572. case int32:
  1573. return x + y, nil
  1574. default:
  1575. return invOp2(x, y, op)
  1576. }
  1577. case int64:
  1578. switch y := b.(type) {
  1579. case int64:
  1580. return x + y, nil
  1581. default:
  1582. return invOp2(x, y, op)
  1583. }
  1584. case string:
  1585. switch y := b.(type) {
  1586. case string:
  1587. return x + y, nil
  1588. default:
  1589. return invOp2(x, y, op)
  1590. }
  1591. case uint8:
  1592. switch y := b.(type) {
  1593. case uint8:
  1594. return x + y, nil
  1595. default:
  1596. return invOp2(x, y, op)
  1597. }
  1598. case uint16:
  1599. switch y := b.(type) {
  1600. case uint16:
  1601. return x + y, nil
  1602. default:
  1603. return invOp2(x, y, op)
  1604. }
  1605. case uint32:
  1606. switch y := b.(type) {
  1607. case uint32:
  1608. return x + y, nil
  1609. default:
  1610. return invOp2(x, y, op)
  1611. }
  1612. case uint64:
  1613. switch y := b.(type) {
  1614. case uint64:
  1615. return x + y, nil
  1616. default:
  1617. return invOp2(x, y, op)
  1618. }
  1619. case *big.Int:
  1620. switch y := b.(type) {
  1621. case *big.Int:
  1622. var z big.Int
  1623. return z.Add(x, y), nil
  1624. default:
  1625. return invOp2(x, y, op)
  1626. }
  1627. case *big.Rat:
  1628. switch y := b.(type) {
  1629. case *big.Rat:
  1630. var z big.Rat
  1631. return z.Add(x, y), nil
  1632. default:
  1633. return invOp2(x, y, op)
  1634. }
  1635. case time.Duration:
  1636. switch y := b.(type) {
  1637. case time.Duration:
  1638. return x + y, nil
  1639. case time.Time:
  1640. return y.Add(x), nil
  1641. default:
  1642. return invOp2(x, y, op)
  1643. }
  1644. case time.Time:
  1645. switch y := b.(type) {
  1646. case time.Duration:
  1647. return x.Add(y), nil
  1648. default:
  1649. return invOp2(x, y, op)
  1650. }
  1651. default:
  1652. return invOp2(a, b, op)
  1653. }
  1654. case '-':
  1655. a, b := o.get2(execCtx, ctx)
  1656. if a == nil || b == nil {
  1657. return
  1658. }
  1659. switch x := a.(type) {
  1660. //case nil:
  1661. case idealComplex:
  1662. switch y := b.(type) {
  1663. case idealComplex:
  1664. return idealComplex(complex64(x) - complex64(y)), nil
  1665. default:
  1666. return invOp2(x, y, op)
  1667. }
  1668. case idealFloat:
  1669. switch y := b.(type) {
  1670. case idealFloat:
  1671. return idealFloat(float64(x) - float64(y)), nil
  1672. default:
  1673. return invOp2(x, y, op)
  1674. }
  1675. case idealInt:
  1676. switch y := b.(type) {
  1677. case idealInt:
  1678. return idealInt(int64(x) - int64(y)), nil
  1679. default:
  1680. return invOp2(x, y, op)
  1681. }
  1682. case idealRune:
  1683. switch y := b.(type) {
  1684. case idealRune:
  1685. return idealRune(int64(x) - int64(y)), nil
  1686. default:
  1687. return invOp2(x, y, op)
  1688. }
  1689. case idealUint:
  1690. switch y := b.(type) {
  1691. case idealUint:
  1692. return idealUint(uint64(x) - uint64(y)), nil
  1693. default:
  1694. return invOp2(x, y, op)
  1695. }
  1696. case bool:
  1697. return undOp2(a, b, op)
  1698. case complex64:
  1699. switch y := b.(type) {
  1700. case complex64:
  1701. return x - y, nil
  1702. default:
  1703. return invOp2(x, y, op)
  1704. }
  1705. case complex128:
  1706. switch y := b.(type) {
  1707. case complex128:
  1708. return x - y, nil
  1709. default:
  1710. return invOp2(x, y, op)
  1711. }
  1712. case float32:
  1713. switch y := b.(type) {
  1714. case float32:
  1715. return x - y, nil
  1716. default:
  1717. return invOp2(x, y, op)
  1718. }
  1719. case float64:
  1720. switch y := b.(type) {
  1721. case float64:
  1722. return x - y, nil
  1723. default:
  1724. return invOp2(x, y, op)
  1725. }
  1726. case int8:
  1727. switch y := b.(type) {
  1728. case int8:
  1729. return x - y, nil
  1730. default:
  1731. return invOp2(x, y, op)
  1732. }
  1733. case int16:
  1734. switch y := b.(type) {
  1735. case int16:
  1736. return x - y, nil
  1737. default:
  1738. return invOp2(x, y, op)
  1739. }
  1740. case int32:
  1741. switch y := b.(type) {
  1742. case int32:
  1743. return x - y, nil
  1744. default:
  1745. return invOp2(x, y, op)
  1746. }
  1747. case int64:
  1748. switch y := b.(type) {
  1749. case int64:
  1750. return x - y, nil
  1751. default:
  1752. return invOp2(x, y, op)
  1753. }
  1754. case string:
  1755. return undOp2(a, b, op)
  1756. case uint8:
  1757. switch y := b.(type) {
  1758. case uint8:
  1759. return x - y, nil
  1760. default:
  1761. return invOp2(x, y, op)
  1762. }
  1763. case uint16:
  1764. switch y := b.(type) {
  1765. case uint16:
  1766. return x - y, nil
  1767. default:
  1768. return invOp2(x, y, op)
  1769. }
  1770. case uint32:
  1771. switch y := b.(type) {
  1772. case uint32:
  1773. return x - y, nil
  1774. default:
  1775. return invOp2(x, y, op)
  1776. }
  1777. case uint64:
  1778. switch y := b.(type) {
  1779. case uint64:
  1780. return x - y, nil
  1781. default:
  1782. return invOp2(x, y, op)
  1783. }
  1784. case *big.Int:
  1785. switch y := b.(type) {
  1786. case *big.Int:
  1787. var z big.Int
  1788. return z.Sub(x, y), nil
  1789. default:
  1790. return invOp2(x, y, op)
  1791. }
  1792. case *big.Rat:
  1793. switch y := b.(type) {
  1794. case *big.Rat:
  1795. var z big.Rat
  1796. return z.Sub(x, y), nil
  1797. default:
  1798. return invOp2(x, y, op)
  1799. }
  1800. case time.Duration:
  1801. switch y := b.(type) {
  1802. case time.Duration:
  1803. return x - y, nil
  1804. default:
  1805. return invOp2(x, y, op)
  1806. }
  1807. case time.Time:
  1808. switch y := b.(type) {
  1809. case time.Duration:
  1810. return x.Add(-y), nil
  1811. case time.Time:
  1812. return x.Sub(y), nil
  1813. default:
  1814. return invOp2(x, y, op)
  1815. }
  1816. default:
  1817. return invOp2(a, b, op)
  1818. }
  1819. case rsh:
  1820. a, b := eval2(o.l, o.r, execCtx, ctx)
  1821. if a == nil || b == nil {
  1822. return
  1823. }
  1824. var cnt uint64
  1825. switch y := b.(type) {
  1826. //case nil:
  1827. case idealComplex:
  1828. return invShiftRHS(a, b)
  1829. case idealFloat:
  1830. return invShiftRHS(a, b)
  1831. case idealInt:
  1832. cnt = uint64(y)
  1833. case idealRune:
  1834. cnt = uint64(y)
  1835. case idealUint:
  1836. cnt = uint64(y)
  1837. case bool:
  1838. return invShiftRHS(a, b)
  1839. case complex64:
  1840. return invShiftRHS(a, b)
  1841. case complex128:
  1842. return invShiftRHS(a, b)
  1843. case float32:
  1844. return invShiftRHS(a, b)
  1845. case float64:
  1846. return invShiftRHS(a, b)
  1847. case int8:
  1848. return invShiftRHS(a, b)
  1849. case int16:
  1850. return invShiftRHS(a, b)
  1851. case int32:
  1852. return invShiftRHS(a, b)
  1853. case int64:
  1854. return invShiftRHS(a, b)
  1855. case string:
  1856. return invShiftRHS(a, b)
  1857. case uint8:
  1858. cnt = uint64(y)
  1859. case uint16:
  1860. cnt = uint64(y)
  1861. case uint32:
  1862. cnt = uint64(y)
  1863. case uint64:
  1864. cnt = uint64(y)
  1865. default:
  1866. return invOp2(a, b, op)
  1867. }
  1868. switch x := a.(type) {
  1869. //case nil:
  1870. case idealComplex:
  1871. return undOp2(a, b, op)
  1872. case idealFloat:
  1873. return undOp2(a, b, op)
  1874. case idealInt:
  1875. return idealInt(int64(x) >> cnt), nil
  1876. case idealRune:
  1877. return idealRune(int64(x) >> cnt), nil
  1878. case idealUint:
  1879. return idealUint(uint64(x) >> cnt), nil
  1880. case bool:
  1881. return undOp2(a, b, op)
  1882. case complex64:
  1883. return undOp2(a, b, op)
  1884. case complex128:
  1885. return undOp2(a, b, op)
  1886. case float32:
  1887. return undOp2(a, b, op)
  1888. case float64:
  1889. return undOp2(a, b, op)
  1890. case int8:
  1891. return x >> cnt, nil
  1892. case int16:
  1893. return x >> cnt, nil
  1894. case int32:
  1895. return x >> cnt, nil
  1896. case int64:
  1897. return x >> cnt, nil
  1898. case string:
  1899. return undOp2(a, b, op)
  1900. case uint8:
  1901. return x >> cnt, nil
  1902. case uint16:
  1903. return x >> cnt, nil
  1904. case uint32:
  1905. return x >> cnt, nil
  1906. case uint64:
  1907. return x >> cnt, nil
  1908. case *big.Int:
  1909. var z big.Int
  1910. return z.Rsh(x, uint(cnt)), nil
  1911. case time.Duration:
  1912. return x >> cnt, nil
  1913. default:
  1914. return invOp2(a, b, op)
  1915. }
  1916. case lsh:
  1917. a, b := eval2(o.l, o.r, execCtx, ctx)
  1918. if a == nil || b == nil {
  1919. return
  1920. }
  1921. var cnt uint64
  1922. switch y := b.(type) {
  1923. //case nil:
  1924. case idealComplex:
  1925. return invShiftRHS(a, b)
  1926. case idealFloat:
  1927. return invShiftRHS(a, b)
  1928. case idealInt:
  1929. cnt = uint64(y)
  1930. case idealRune:
  1931. cnt = uint64(y)
  1932. case idealUint:
  1933. cnt = uint64(y)
  1934. case bool:
  1935. return invShiftRHS(a, b)
  1936. case complex64:
  1937. return invShiftRHS(a, b)
  1938. case complex128:
  1939. return invShiftRHS(a, b)
  1940. case float32:
  1941. return invShiftRHS(a, b)
  1942. case float64:
  1943. return invShiftRHS(a, b)
  1944. case int8:
  1945. return invShiftRHS(a, b)
  1946. case int16:
  1947. return invShiftRHS(a, b)
  1948. case int32:
  1949. return invShiftRHS(a, b)
  1950. case int64:
  1951. return invShiftRHS(a, b)
  1952. case string:
  1953. return invShiftRHS(a, b)
  1954. case uint8:
  1955. cnt = uint64(y)
  1956. case uint16:
  1957. cnt = uint64(y)
  1958. case uint32:
  1959. cnt = uint64(y)
  1960. case uint64:
  1961. cnt = uint64(y)
  1962. default:
  1963. return invOp2(a, b, op)
  1964. }
  1965. switch x := a.(type) {
  1966. //case nil:
  1967. case idealComplex:
  1968. return undOp2(a, b, op)
  1969. case idealFloat:
  1970. return undOp2(a, b, op)
  1971. case idealInt:
  1972. return idealInt(int64(x) << cnt), nil
  1973. case idealRune:
  1974. return idealRune(int64(x) << cnt), nil
  1975. case idealUint:
  1976. return idealUint(uint64(x) << cnt), nil
  1977. case bool:
  1978. return undOp2(a, b, op)
  1979. case complex64:
  1980. return undOp2(a, b, op)
  1981. case complex128:
  1982. return undOp2(a, b, op)
  1983. case float32:
  1984. return undOp2(a, b, op)
  1985. case float64:
  1986. return undOp2(a, b, op)
  1987. case int8:
  1988. return x << cnt, nil
  1989. case int16:
  1990. return x << cnt, nil
  1991. case int32:
  1992. return x << cnt, nil
  1993. case int64:
  1994. return x << cnt, nil
  1995. case string:
  1996. return undOp2(a, b, op)
  1997. case uint8:
  1998. return x << cnt, nil
  1999. case uint16:
  2000. return x << cnt, nil
  2001. case uint32:
  2002. return x << cnt, nil
  2003. case uint64:
  2004. return x << cnt, nil
  2005. case *big.Int:
  2006. var z big.Int
  2007. return z.Lsh(x, uint(cnt)), nil
  2008. case time.Duration:
  2009. return x << cnt, nil
  2010. default:
  2011. return invOp2(a, b, op)
  2012. }
  2013. case '&':
  2014. a, b := o.get2(execCtx, ctx)
  2015. if a == nil || b == nil {
  2016. return
  2017. }
  2018. switch x := a.(type) {
  2019. //case nil:
  2020. case idealComplex:
  2021. return undOp2(a, b, op)
  2022. case idealFloat:
  2023. return undOp2(a, b, op)
  2024. case idealInt:
  2025. switch y := b.(type) {
  2026. case idealInt:
  2027. return idealInt(int64(x) & int64(y)), nil
  2028. default:
  2029. return invOp2(x, y, op)
  2030. }
  2031. case idealRune:
  2032. switch y := b.(type) {
  2033. case idealRune:
  2034. return idealRune(int64(x) & int64(y)), nil
  2035. default:
  2036. return invOp2(x, y, op)
  2037. }
  2038. case idealUint:
  2039. switch y := b.(type) {
  2040. case idealUint:
  2041. return idealUint(uint64(x) & uint64(y)), nil
  2042. default:
  2043. return invOp2(x, y, op)
  2044. }
  2045. case bool:
  2046. return undOp2(a, b, op)
  2047. case complex64:
  2048. return undOp2(a, b, op)
  2049. case complex128:
  2050. return undOp2(a, b, op)
  2051. case float32:
  2052. return undOp2(a, b, op)
  2053. case float64:
  2054. return undOp2(a, b, op)
  2055. case int8:
  2056. switch y := b.(type) {
  2057. case int8:
  2058. return x & y, nil
  2059. default:
  2060. return invOp2(x, y, op)
  2061. }
  2062. case int16:
  2063. switch y := b.(type) {
  2064. case int16:
  2065. return x & y, nil
  2066. default:
  2067. return invOp2(x, y, op)
  2068. }
  2069. case int32:
  2070. switch y := b.(type) {
  2071. case int32:
  2072. return x & y, nil
  2073. default:
  2074. return invOp2(x, y, op)
  2075. }
  2076. case int64:
  2077. switch y := b.(type) {
  2078. case int64:
  2079. return x & y, nil
  2080. default:
  2081. return invOp2(x, y, op)
  2082. }
  2083. case string:
  2084. return undOp2(a, b, op)
  2085. case uint8:
  2086. switch y := b.(type) {
  2087. case uint8:
  2088. return x & y, nil
  2089. default:
  2090. return invOp2(x, y, op)
  2091. }
  2092. case uint16:
  2093. switch y := b.(type) {
  2094. case uint16:
  2095. return x & y, nil
  2096. default:
  2097. return invOp2(x, y, op)
  2098. }
  2099. case uint32:
  2100. switch y := b.(type) {
  2101. case uint32:
  2102. return x & y, nil
  2103. default:
  2104. return invOp2(x, y, op)
  2105. }
  2106. case uint64:
  2107. switch y := b.(type) {
  2108. case uint64:
  2109. return x & y, nil
  2110. default:
  2111. return invOp2(x, y, op)
  2112. }
  2113. case *big.Int:
  2114. switch y := b.(type) {
  2115. case *big.Int:
  2116. var z big.Int
  2117. return z.And(x, y), nil
  2118. default:
  2119. return invOp2(x, y, op)
  2120. }
  2121. case time.Duration:
  2122. switch y := b.(type) {
  2123. case time.Duration:
  2124. return x & y, nil
  2125. default:
  2126. return invOp2(x, y, op)
  2127. }
  2128. default:
  2129. return invOp2(a, b, op)
  2130. }
  2131. case '|':
  2132. a, b := o.get2(execCtx, ctx)
  2133. if a == nil || b == nil {
  2134. return
  2135. }
  2136. switch x := a.(type) {
  2137. //case nil:
  2138. case idealComplex:
  2139. return undOp2(a, b, op)
  2140. case idealFloat:
  2141. return undOp2(a, b, op)
  2142. case idealInt:
  2143. switch y := b.(type) {
  2144. case idealInt:
  2145. return idealInt(int64(x) | int64(y)), nil
  2146. default:
  2147. return invOp2(x, y, op)
  2148. }
  2149. case idealRune:
  2150. switch y := b.(type) {
  2151. case idealRune:
  2152. return idealRune(int64(x) | int64(y)), nil
  2153. default:
  2154. return invOp2(x, y, op)
  2155. }
  2156. case idealUint:
  2157. switch y := b.(type) {
  2158. case idealUint:
  2159. return idealUint(uint64(x) | uint64(y)), nil
  2160. default:
  2161. return invOp2(x, y, op)
  2162. }
  2163. case bool:
  2164. return undOp2(a, b, op)
  2165. case complex64:
  2166. return undOp2(a, b, op)
  2167. case complex128:
  2168. return undOp2(a, b, op)
  2169. case float32:
  2170. return undOp2(a, b, op)
  2171. case float64:
  2172. return undOp2(a, b, op)
  2173. case int8:
  2174. switch y := b.(type) {
  2175. case int8:
  2176. return x | y, nil
  2177. default:
  2178. return invOp2(x, y, op)
  2179. }
  2180. case int16:
  2181. switch y := b.(type) {
  2182. case int16:
  2183. return x | y, nil
  2184. default:
  2185. return invOp2(x, y, op)
  2186. }
  2187. case int32:
  2188. switch y := b.(type) {
  2189. case int32:
  2190. return x | y, nil
  2191. default:
  2192. return invOp2(x, y, op)
  2193. }
  2194. case int64:
  2195. switch y := b.(type) {
  2196. case int64:
  2197. return x | y, nil
  2198. default:
  2199. return invOp2(x, y, op)
  2200. }
  2201. case string:
  2202. return undOp2(a, b, op)
  2203. case uint8:
  2204. switch y := b.(type) {
  2205. case uint8:
  2206. return x | y, nil
  2207. default:
  2208. return invOp2(x, y, op)
  2209. }
  2210. case uint16:
  2211. switch y := b.(type) {
  2212. case uint16:
  2213. return x | y, nil
  2214. default:
  2215. return invOp2(x, y, op)
  2216. }
  2217. case uint32:
  2218. switch y := b.(type) {
  2219. case uint32:
  2220. return x | y, nil
  2221. default:
  2222. return invOp2(x, y, op)
  2223. }
  2224. case uint64:
  2225. switch y := b.(type) {
  2226. case uint64:
  2227. return x | y, nil
  2228. default:
  2229. return invOp2(x, y, op)
  2230. }
  2231. case *big.Int:
  2232. switch y := b.(type) {
  2233. case *big.Int:
  2234. var z big.Int
  2235. return z.Or(x, y), nil
  2236. default:
  2237. return invOp2(x, y, op)
  2238. }
  2239. case time.Duration:
  2240. switch y := b.(type) {
  2241. case time.Duration:
  2242. return x | y, nil
  2243. default:
  2244. return invOp2(x, y, op)
  2245. }
  2246. default:
  2247. return invOp2(a, b, op)
  2248. }
  2249. case andnot:
  2250. a, b := o.get2(execCtx, ctx)
  2251. if a == nil || b == nil {
  2252. return
  2253. }
  2254. switch x := a.(type) {
  2255. //case nil:
  2256. case idealComplex:
  2257. return undOp2(a, b, op)
  2258. case idealFloat:
  2259. return undOp2(a, b, op)
  2260. case idealInt:
  2261. switch y := b.(type) {
  2262. case idealInt:
  2263. return idealInt(int64(x) &^ int64(y)), nil
  2264. default:
  2265. return invOp2(x, y, op)
  2266. }
  2267. case idealRune:
  2268. switch y := b.(type) {
  2269. case idealRune:
  2270. return idealRune(int64(x) &^ int64(y)), nil
  2271. default:
  2272. return invOp2(x, y, op)
  2273. }
  2274. case idealUint:
  2275. switch y := b.(type) {
  2276. case idealUint:
  2277. return idealUint(uint64(x) &^ uint64(y)), nil
  2278. default:
  2279. return invOp2(x, y, op)
  2280. }
  2281. case bool:
  2282. return undOp2(a, b, op)
  2283. case complex64:
  2284. return undOp2(a, b, op)
  2285. case complex128:
  2286. return undOp2(a, b, op)
  2287. case float32:
  2288. return undOp2(a, b, op)
  2289. case float64:
  2290. return undOp2(a, b, op)
  2291. case int8:
  2292. switch y := b.(type) {
  2293. case int8:
  2294. return x &^ y, nil
  2295. default:
  2296. return invOp2(x, y, op)
  2297. }
  2298. case int16:
  2299. switch y := b.(type) {
  2300. case int16:
  2301. return x &^ y, nil
  2302. default:
  2303. return invOp2(x, y, op)
  2304. }
  2305. case int32:
  2306. switch y := b.(type) {
  2307. case int32:
  2308. return x &^ y, nil
  2309. default:
  2310. return invOp2(x, y, op)
  2311. }
  2312. case int64:
  2313. switch y := b.(type) {
  2314. case int64:
  2315. return x &^ y, nil
  2316. default:
  2317. return invOp2(x, y, op)
  2318. }
  2319. case string:
  2320. return undOp2(a, b, op)
  2321. case uint8:
  2322. switch y := b.(type) {
  2323. case uint8:
  2324. return x &^ y, nil
  2325. default:
  2326. return invOp2(x, y, op)
  2327. }
  2328. case uint16:
  2329. switch y := b.(type) {
  2330. case uint16:
  2331. return x &^ y, nil
  2332. default:
  2333. return invOp2(x, y, op)
  2334. }
  2335. case uint32:
  2336. switch y := b.(type) {
  2337. case uint32:
  2338. return x &^ y, nil
  2339. default:
  2340. return invOp2(x, y, op)
  2341. }
  2342. case uint64:
  2343. switch y := b.(type) {
  2344. case uint64:
  2345. return x &^ y, nil
  2346. default:
  2347. return invOp2(x, y, op)
  2348. }
  2349. case *big.Int:
  2350. switch y := b.(type) {
  2351. case *big.Int:
  2352. var z big.Int
  2353. return z.AndNot(x, y), nil
  2354. default:
  2355. return invOp2(x, y, op)
  2356. }
  2357. case time.Duration:
  2358. switch y := b.(type) {
  2359. case time.Duration:
  2360. return x &^ y, nil
  2361. default:
  2362. return invOp2(x, y, op)
  2363. }
  2364. default:
  2365. return invOp2(a, b, op)
  2366. }
  2367. case '^':
  2368. a, b := o.get2(execCtx, ctx)
  2369. if a == nil || b == nil {
  2370. return
  2371. }
  2372. switch x := a.(type) {
  2373. //case nil:
  2374. case idealComplex:
  2375. return undOp2(a, b, op)
  2376. case idealFloat:
  2377. return undOp2(a, b, op)
  2378. case idealInt:
  2379. switch y := b.(type) {
  2380. case idealInt:
  2381. return idealInt(int64(x) ^ int64(y)), nil
  2382. default:
  2383. return invOp2(x, y, op)
  2384. }
  2385. case idealRune:
  2386. switch y := b.(type) {
  2387. case idealRune:
  2388. return idealRune(int64(x) ^ int64(y)), nil
  2389. default:
  2390. return invOp2(x, y, op)
  2391. }
  2392. case idealUint:
  2393. switch y := b.(type) {
  2394. case idealUint:
  2395. return idealUint(uint64(x) ^ uint64(y)), nil
  2396. default:
  2397. return invOp2(x, y, op)
  2398. }
  2399. case bool:
  2400. return undOp2(a, b, op)
  2401. case complex64:
  2402. return undOp2(a, b, op)
  2403. case complex128:
  2404. return undOp2(a, b, op)
  2405. case float32:
  2406. return undOp2(a, b, op)
  2407. case float64:
  2408. return undOp2(a, b, op)
  2409. case int8:
  2410. switch y := b.(type) {
  2411. case int8:
  2412. return x ^ y, nil
  2413. default:
  2414. return invOp2(x, y, op)
  2415. }
  2416. case int16:
  2417. switch y := b.(type) {
  2418. case int16:
  2419. return x ^ y, nil
  2420. default:
  2421. return invOp2(x, y, op)
  2422. }
  2423. case int32:
  2424. switch y := b.(type) {
  2425. case int32:
  2426. return x ^ y, nil
  2427. default:
  2428. return invOp2(x, y, op)
  2429. }
  2430. case int64:
  2431. switch y := b.(type) {
  2432. case int64:
  2433. return x ^ y, nil
  2434. default:
  2435. return invOp2(x, y, op)
  2436. }
  2437. case string:
  2438. return undOp2(a, b, op)
  2439. case uint8:
  2440. switch y := b.(type) {
  2441. case uint8:
  2442. return x ^ y, nil
  2443. default:
  2444. return invOp2(x, y, op)
  2445. }
  2446. case uint16:
  2447. switch y := b.(type) {
  2448. case uint16:
  2449. return x ^ y, nil
  2450. default:
  2451. return invOp2(x, y, op)
  2452. }
  2453. case uint32:
  2454. switch y := b.(type) {
  2455. case uint32:
  2456. return x ^ y, nil
  2457. default:
  2458. return invOp2(x, y, op)
  2459. }
  2460. case uint64:
  2461. switch y := b.(type) {
  2462. case uint64:
  2463. return x ^ y, nil
  2464. default:
  2465. return invOp2(x, y, op)
  2466. }
  2467. case *big.Int:
  2468. switch y := b.(type) {
  2469. case *big.Int:
  2470. var z big.Int
  2471. return z.Xor(x, y), nil
  2472. default:
  2473. return invOp2(x, y, op)
  2474. }
  2475. case time.Duration:
  2476. switch y := b.(type) {
  2477. case time.Duration:
  2478. return x ^ y, nil
  2479. default:
  2480. return invOp2(x, y, op)
  2481. }
  2482. default:
  2483. return invOp2(a, b, op)
  2484. }
  2485. case '%':
  2486. a, b := o.get2(execCtx, ctx)
  2487. if a == nil || b == nil {
  2488. return
  2489. }
  2490. switch x := a.(type) {
  2491. //case nil:
  2492. case idealComplex:
  2493. return undOp2(a, b, op)
  2494. case idealFloat:
  2495. return undOp2(a, b, op)
  2496. case idealInt:
  2497. switch y := b.(type) {
  2498. case idealInt:
  2499. return idealInt(int64(x) % int64(y)), nil
  2500. default:
  2501. return invOp2(x, y, op)
  2502. }
  2503. case idealRune:
  2504. switch y := b.(type) {
  2505. case idealRune:
  2506. return idealRune(int64(x) % int64(y)), nil
  2507. default:
  2508. return invOp2(x, y, op)
  2509. }
  2510. case idealUint:
  2511. switch y := b.(type) {
  2512. case idealUint:
  2513. return idealUint(uint64(x) % uint64(y)), nil
  2514. default:
  2515. return invOp2(x, y, op)
  2516. }
  2517. case bool:
  2518. return undOp2(a, b, op)
  2519. case complex64:
  2520. return undOp2(a, b, op)
  2521. case complex128:
  2522. return undOp2(a, b, op)
  2523. case float32:
  2524. return undOp2(a, b, op)
  2525. case float64:
  2526. return undOp2(a, b, op)
  2527. case int8:
  2528. switch y := b.(type) {
  2529. case int8:
  2530. return x % y, nil
  2531. default:
  2532. return invOp2(x, y, op)
  2533. }
  2534. case int16:
  2535. switch y := b.(type) {
  2536. case int16:
  2537. return x % y, nil
  2538. default:
  2539. return invOp2(x, y, op)
  2540. }
  2541. case int32:
  2542. switch y := b.(type) {
  2543. case int32:
  2544. return x % y, nil
  2545. default:
  2546. return invOp2(x, y, op)
  2547. }
  2548. case int64:
  2549. switch y := b.(type) {
  2550. case int64:
  2551. return x % y, nil
  2552. default:
  2553. return invOp2(x, y, op)
  2554. }
  2555. case string:
  2556. return undOp2(a, b, op)
  2557. case uint8:
  2558. switch y := b.(type) {
  2559. case uint8:
  2560. return x % y, nil
  2561. default:
  2562. return invOp2(x, y, op)
  2563. }
  2564. case uint16:
  2565. switch y := b.(type) {
  2566. case uint16:
  2567. return x % y, nil
  2568. default:
  2569. return invOp2(x, y, op)
  2570. }
  2571. case uint32:
  2572. switch y := b.(type) {
  2573. case uint32:
  2574. return x % y, nil
  2575. default:
  2576. return invOp2(x, y, op)
  2577. }
  2578. case uint64:
  2579. switch y := b.(type) {
  2580. case uint64:
  2581. return x % y, nil
  2582. default:
  2583. return invOp2(x, y, op)
  2584. }
  2585. case *big.Int:
  2586. switch y := b.(type) {
  2587. case *big.Int:
  2588. if y.Sign() == 0 {
  2589. return nil, errDivByZero
  2590. }
  2591. var z big.Int
  2592. return z.Mod(x, y), nil
  2593. default:
  2594. return invOp2(x, y, op)
  2595. }
  2596. case time.Duration:
  2597. switch y := b.(type) {
  2598. case time.Duration:
  2599. return x % y, nil
  2600. default:
  2601. return invOp2(x, y, op)
  2602. }
  2603. default:
  2604. return invOp2(a, b, op)
  2605. }
  2606. case '/':
  2607. a, b := o.get2(execCtx, ctx)
  2608. if a == nil || b == nil {
  2609. return
  2610. }
  2611. switch x := a.(type) {
  2612. //case nil:
  2613. case idealComplex:
  2614. switch y := b.(type) {
  2615. case idealComplex:
  2616. return idealComplex(complex64(x) / complex64(y)), nil
  2617. default:
  2618. return invOp2(x, y, op)
  2619. }
  2620. case idealFloat:
  2621. switch y := b.(type) {
  2622. case idealFloat:
  2623. return idealFloat(float64(x) / float64(y)), nil
  2624. default:
  2625. return invOp2(x, y, op)
  2626. }
  2627. case idealInt:
  2628. switch y := b.(type) {
  2629. case idealInt:
  2630. return idealInt(int64(x) / int64(y)), nil
  2631. default:
  2632. return invOp2(x, y, op)
  2633. }
  2634. case idealRune:
  2635. switch y := b.(type) {
  2636. case idealRune:
  2637. return idealRune(int64(x) / int64(y)), nil
  2638. default:
  2639. return invOp2(x, y, op)
  2640. }
  2641. case idealUint:
  2642. switch y := b.(type) {
  2643. case idealUint:
  2644. return idealUint(uint64(x) / uint64(y)), nil
  2645. default:
  2646. return invOp2(x, y, op)
  2647. }
  2648. case bool:
  2649. return undOp2(a, b, op)
  2650. case complex64:
  2651. switch y := b.(type) {
  2652. case complex64:
  2653. return x / y, nil
  2654. default:
  2655. return invOp2(x, y, op)
  2656. }
  2657. case complex128:
  2658. switch y := b.(type) {
  2659. case complex128:
  2660. return x / y, nil
  2661. default:
  2662. return invOp2(x, y, op)
  2663. }
  2664. case float32:
  2665. switch y := b.(type) {
  2666. case float32:
  2667. return x / y, nil
  2668. default:
  2669. return invOp2(x, y, op)
  2670. }
  2671. case float64:
  2672. switch y := b.(type) {
  2673. case float64:
  2674. return x / y, nil
  2675. default:
  2676. return invOp2(x, y, op)
  2677. }
  2678. case int8:
  2679. switch y := b.(type) {
  2680. case int8:
  2681. return x / y, nil
  2682. default:
  2683. return invOp2(x, y, op)
  2684. }
  2685. case int16:
  2686. switch y := b.(type) {
  2687. case int16:
  2688. return x / y, nil
  2689. default:
  2690. return invOp2(x, y, op)
  2691. }
  2692. case int32:
  2693. switch y := b.(type) {
  2694. case int32:
  2695. return x / y, nil
  2696. default:
  2697. return invOp2(x, y, op)
  2698. }
  2699. case int64:
  2700. switch y := b.(type) {
  2701. case int64:
  2702. return x / y, nil
  2703. default:
  2704. return invOp2(x, y, op)
  2705. }
  2706. case string:
  2707. return undOp2(a, b, op)
  2708. case uint8:
  2709. switch y := b.(type) {
  2710. case uint8:
  2711. return x / y, nil
  2712. default:
  2713. return invOp2(x, y, op)
  2714. }
  2715. case uint16:
  2716. switch y := b.(type) {
  2717. case uint16:
  2718. return x / y, nil
  2719. default:
  2720. return invOp2(x, y, op)
  2721. }
  2722. case uint32:
  2723. switch y := b.(type) {
  2724. case uint32:
  2725. return x / y, nil
  2726. default:
  2727. return invOp2(x, y, op)
  2728. }
  2729. case uint64:
  2730. switch y := b.(type) {
  2731. case uint64:
  2732. return x / y, nil
  2733. default:
  2734. return invOp2(x, y, op)
  2735. }
  2736. case *big.Int:
  2737. switch y := b.(type) {
  2738. case *big.Int:
  2739. if y.Sign() == 0 {
  2740. return nil, errDivByZero
  2741. }
  2742. var z big.Int
  2743. return z.Quo(x, y), nil
  2744. default:
  2745. return invOp2(x, y, op)
  2746. }
  2747. case *big.Rat:
  2748. switch y := b.(type) {
  2749. case *big.Rat:
  2750. if y.Sign() == 0 {
  2751. return nil, errDivByZero
  2752. }
  2753. var z big.Rat
  2754. return z.Quo(x, y), nil
  2755. default:
  2756. return invOp2(x, y, op)
  2757. }
  2758. case time.Duration:
  2759. switch y := b.(type) {
  2760. case time.Duration:
  2761. return x / y, nil
  2762. default:
  2763. return invOp2(x, y, op)
  2764. }
  2765. default:
  2766. return invOp2(a, b, op)
  2767. }
  2768. case '*':
  2769. a, b := o.get2(execCtx, ctx)
  2770. if a == nil || b == nil {
  2771. return
  2772. }
  2773. switch x := a.(type) {
  2774. //case nil:
  2775. case idealComplex:
  2776. switch y := b.(type) {
  2777. case idealComplex:
  2778. return idealComplex(complex64(x) * complex64(y)), nil
  2779. default:
  2780. return invOp2(x, y, op)
  2781. }
  2782. case idealFloat:
  2783. switch y := b.(type) {
  2784. case idealFloat:
  2785. return idealFloat(float64(x) * float64(y)), nil
  2786. default:
  2787. return invOp2(x, y, op)
  2788. }
  2789. case idealInt:
  2790. switch y := b.(type) {
  2791. case idealInt:
  2792. return idealInt(int64(x) * int64(y)), nil
  2793. default:
  2794. return invOp2(x, y, op)
  2795. }
  2796. case idealRune:
  2797. switch y := b.(type) {
  2798. case idealRune:
  2799. return idealRune(int64(x) * int64(y)), nil
  2800. default:
  2801. return invOp2(x, y, op)
  2802. }
  2803. case idealUint:
  2804. switch y := b.(type) {
  2805. case idealUint:
  2806. return idealUint(uint64(x) * uint64(y)), nil
  2807. default:
  2808. return invOp2(x, y, op)
  2809. }
  2810. case bool:
  2811. return undOp2(a, b, op)
  2812. case complex64:
  2813. switch y := b.(type) {
  2814. case complex64:
  2815. return x * y, nil
  2816. default:
  2817. return invOp2(x, y, op)
  2818. }
  2819. case complex128:
  2820. switch y := b.(type) {
  2821. case complex128:
  2822. return x * y, nil
  2823. default:
  2824. return invOp2(x, y, op)
  2825. }
  2826. case float32:
  2827. switch y := b.(type) {
  2828. case float32:
  2829. return x * y, nil
  2830. default:
  2831. return invOp2(x, y, op)
  2832. }
  2833. case float64:
  2834. switch y := b.(type) {
  2835. case float64:
  2836. return x * y, nil
  2837. default:
  2838. return invOp2(x, y, op)
  2839. }
  2840. case int8:
  2841. switch y := b.(type) {
  2842. case int8:
  2843. return x * y, nil
  2844. default:
  2845. return invOp2(x, y, op)
  2846. }
  2847. case int16:
  2848. switch y := b.(type) {
  2849. case int16:
  2850. return x * y, nil
  2851. default:
  2852. return invOp2(x, y, op)
  2853. }
  2854. case int32:
  2855. switch y := b.(type) {
  2856. case int32:
  2857. return x * y, nil
  2858. default:
  2859. return invOp2(x, y, op)
  2860. }
  2861. case int64:
  2862. switch y := b.(type) {
  2863. case int64:
  2864. return x * y, nil
  2865. default:
  2866. return invOp2(x, y, op)
  2867. }
  2868. case string:
  2869. return undOp2(a, b, op)
  2870. case uint8:
  2871. switch y := b.(type) {
  2872. case uint8:
  2873. return x * y, nil
  2874. default:
  2875. return invOp2(x, y, op)
  2876. }
  2877. case uint16:
  2878. switch y := b.(type) {
  2879. case uint16:
  2880. return x * y, nil
  2881. default:
  2882. return invOp2(x, y, op)
  2883. }
  2884. case uint32:
  2885. switch y := b.(type) {
  2886. case uint32:
  2887. return x * y, nil
  2888. default:
  2889. return invOp2(x, y, op)
  2890. }
  2891. case uint64:
  2892. switch y := b.(type) {
  2893. case uint64:
  2894. return x * y, nil
  2895. default:
  2896. return invOp2(x, y, op)
  2897. }
  2898. case *big.Int:
  2899. switch y := b.(type) {
  2900. case *big.Int:
  2901. var z big.Int
  2902. return z.Mul(x, y), nil
  2903. default:
  2904. return invOp2(x, y, op)
  2905. }
  2906. case *big.Rat:
  2907. switch y := b.(type) {
  2908. case *big.Rat:
  2909. var z big.Rat
  2910. return z.Mul(x, y), nil
  2911. default:
  2912. return invOp2(x, y, op)
  2913. }
  2914. case time.Duration:
  2915. switch y := b.(type) {
  2916. case time.Duration:
  2917. return x * y, nil
  2918. default:
  2919. return invOp2(x, y, op)
  2920. }
  2921. default:
  2922. return invOp2(a, b, op)
  2923. }
  2924. default:
  2925. panic("internal error 037")
  2926. }
  2927. }
  2928. func (o *binaryOperation) get2(execCtx *execCtx, ctx map[interface{}]interface{}) (x, y interface{}) {
  2929. x, y = eval2(o.l, o.r, execCtx, ctx)
  2930. //dbg("get2 pIn - ", x, y)
  2931. //defer func() {dbg("get2 coerced ", x, y)}()
  2932. return coerce(x, y)
  2933. }
  2934. type ident struct {
  2935. s string
  2936. }
  2937. func (i *ident) clone(arg []interface{}, unqualify ...string) (expression, error) {
  2938. x := strings.IndexByte(i.s, '.')
  2939. if x < 0 {
  2940. return &ident{s: i.s}, nil
  2941. }
  2942. q := i.s[:x]
  2943. for _, v := range unqualify {
  2944. if q == v {
  2945. return &ident{i.s[x+1:]}, nil
  2946. }
  2947. }
  2948. return &ident{s: i.s}, nil
  2949. }
  2950. func (i *ident) isQualified() bool { return strings.Contains(i.s, ".") }
  2951. func (i *ident) isStatic() bool { return false }
  2952. func (i *ident) String() string { return i.s }
  2953. func (i *ident) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  2954. if _, ok := ctx["$agg0"]; ok {
  2955. return int64(0), nil
  2956. }
  2957. //defer func() { dbg("ident %q -> %v %v", i.s, v, err) }()
  2958. v, ok := ctx[i.s]
  2959. if !ok {
  2960. err = fmt.Errorf("unknown field %s", i.s)
  2961. }
  2962. return
  2963. }
  2964. type pInEval struct {
  2965. m map[interface{}]struct{} // IN (SELECT...) results
  2966. sample interface{}
  2967. }
  2968. type pIn struct {
  2969. expr expression
  2970. list []expression
  2971. not bool
  2972. sel *selectStmt
  2973. }
  2974. func (n *pIn) clone(arg []interface{}, unqualify ...string) (expression, error) {
  2975. expr, err := n.expr.clone(arg, unqualify...)
  2976. if err != nil {
  2977. return nil, err
  2978. }
  2979. list, err := cloneExpressionList(arg, n.list)
  2980. if err != nil {
  2981. return nil, err
  2982. }
  2983. return &pIn{
  2984. expr: expr,
  2985. list: list,
  2986. not: n.not,
  2987. sel: n.sel,
  2988. }, nil
  2989. }
  2990. func (n *pIn) isStatic() bool {
  2991. if !n.expr.isStatic() || n.sel != nil {
  2992. return false
  2993. }
  2994. for _, v := range n.list {
  2995. if !v.isStatic() {
  2996. return false
  2997. }
  2998. }
  2999. return true
  3000. }
  3001. //LATER newIn
  3002. func (n *pIn) String() string {
  3003. if n.sel == nil {
  3004. a := []string{}
  3005. for _, v := range n.list {
  3006. a = append(a, v.String())
  3007. }
  3008. if n.not {
  3009. return fmt.Sprintf("%s NOT IN (%s)", n.expr, strings.Join(a, ","))
  3010. }
  3011. return fmt.Sprintf("%s IN (%s)", n.expr, strings.Join(a, ","))
  3012. }
  3013. if n.not {
  3014. return fmt.Sprintf("%s NOT IN (%s)", n.expr, n.sel)
  3015. }
  3016. return fmt.Sprintf("%s IN (%s)", n.expr, n.sel)
  3017. }
  3018. func (n *pIn) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3019. lhs, err := expand1(n.expr.eval(execCtx, ctx))
  3020. if err != nil {
  3021. return nil, err
  3022. }
  3023. if lhs == nil {
  3024. return nil, nil //TODO Add test for NULL LHS.
  3025. }
  3026. if n.sel == nil {
  3027. for _, v := range n.list {
  3028. b, err := newBinaryOperation(eq, value{lhs}, v)
  3029. if err != nil {
  3030. return nil, err
  3031. }
  3032. eval, err := b.eval(execCtx, ctx)
  3033. if err != nil {
  3034. return nil, err
  3035. }
  3036. if x, ok := eval.(bool); ok && x {
  3037. return !n.not, nil
  3038. }
  3039. }
  3040. return n.not, nil
  3041. }
  3042. var ev *pInEval
  3043. ev0 := ctx[n]
  3044. if ev0 == nil { // SELECT not yet evaluated.
  3045. r, err := n.sel.plan(execCtx)
  3046. if err != nil {
  3047. return nil, err
  3048. }
  3049. if g, e := len(r.fieldNames()), 1; g != e {
  3050. return false, fmt.Errorf("IN (%s): mismatched field count, have %d, need %d", n.sel, g, e)
  3051. }
  3052. ev = &pInEval{m: map[interface{}]struct{}{}}
  3053. ctx[n] = ev
  3054. m := ev.m
  3055. typechecked := false
  3056. if err := r.do(execCtx, func(id interface{}, data []interface{}) (more bool, err error) {
  3057. if typechecked {
  3058. if data[0] == nil {
  3059. return true, nil
  3060. }
  3061. m[data[0]] = struct{}{}
  3062. }
  3063. if data[0] == nil {
  3064. return true, nil
  3065. }
  3066. ev.sample = data[0]
  3067. switch ev.sample.(type) {
  3068. case bool, byte, complex128, complex64, float32,
  3069. float64, int16, int32, int64, int8,
  3070. string, uint16, uint32, uint64:
  3071. typechecked = true
  3072. m[ev.sample] = struct{}{}
  3073. return true, nil
  3074. default:
  3075. return false, fmt.Errorf("IN (%s): invalid field type: %T", n.sel, data[0])
  3076. }
  3077. }); err != nil {
  3078. return nil, err
  3079. }
  3080. } else {
  3081. ev = ev0.(*pInEval)
  3082. }
  3083. if ev.sample == nil {
  3084. return nil, nil
  3085. }
  3086. _, ok := ev.m[coerce1(lhs, ev.sample)]
  3087. return ok != n.not, nil
  3088. }
  3089. type value struct {
  3090. val interface{}
  3091. }
  3092. func (l value) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3093. return value{val: l.val}, nil
  3094. }
  3095. func (l value) isStatic() bool { return true }
  3096. func (l value) String() string {
  3097. switch x := l.val.(type) {
  3098. case nil:
  3099. return "NULL"
  3100. case idealComplex:
  3101. s := fmt.Sprint(x)
  3102. return s[1 : len(s)-1]
  3103. case complex64:
  3104. s := fmt.Sprint(x)
  3105. return s[1 : len(s)-1]
  3106. case complex128:
  3107. s := fmt.Sprint(x)
  3108. return s[1 : len(s)-1]
  3109. case string:
  3110. return fmt.Sprintf("%q", x)
  3111. case time.Duration:
  3112. return fmt.Sprintf("duration(%q)", l.val)
  3113. case time.Time:
  3114. y, m, d := x.Date()
  3115. zone, _ := x.Zone()
  3116. return fmt.Sprintf("date(%v, %v, %v, %v, %v, %v, %v, %v)", y, m, d, x.Hour(), x.Minute(), x.Second(), x.Nanosecond(), zone)
  3117. case *big.Rat:
  3118. return fmt.Sprintf("bigrat(%q)", l.val)
  3119. case *big.Int:
  3120. return fmt.Sprintf(`bigint("%v")`, l.val)
  3121. default:
  3122. return fmt.Sprintf("%v", l.val)
  3123. }
  3124. }
  3125. func (l value) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (interface{}, error) {
  3126. return l.val, nil
  3127. }
  3128. type conversion struct {
  3129. typ int
  3130. val expression
  3131. }
  3132. func (c *conversion) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3133. val, err := c.val.clone(arg, unqualify...)
  3134. if err != nil {
  3135. return nil, err
  3136. }
  3137. return &conversion{typ: c.typ, val: val}, nil
  3138. }
  3139. func (c *conversion) isStatic() bool {
  3140. return c.val.isStatic()
  3141. }
  3142. //LATER newConversion or fake unary op
  3143. func (c *conversion) String() string {
  3144. return fmt.Sprintf("%s(%s)", typeStr(c.typ), c.val)
  3145. }
  3146. func (c *conversion) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3147. val, err := expand1(c.val.eval(execCtx, ctx))
  3148. if err != nil {
  3149. return
  3150. }
  3151. return convert(val, c.typ)
  3152. }
  3153. type unaryOperation struct {
  3154. op int
  3155. v expression
  3156. }
  3157. func newUnaryOperation(op int, x interface{}) (v expression, err error) {
  3158. l, ok := x.(expression)
  3159. if !ok {
  3160. panic("internal error 038")
  3161. }
  3162. for {
  3163. pe, ok := l.(*pexpr)
  3164. if ok {
  3165. l = pe.expr
  3166. continue
  3167. }
  3168. break
  3169. }
  3170. if l.isStatic() {
  3171. val, err := l.eval(nil, nil)
  3172. if err != nil {
  3173. return nil, err
  3174. }
  3175. l = value{val}
  3176. }
  3177. if op == '!' {
  3178. b, ok := l.(*binaryOperation)
  3179. if ok {
  3180. switch b.op {
  3181. case eq:
  3182. b.op = neq
  3183. return b, nil
  3184. case neq:
  3185. b.op = eq
  3186. return b, nil
  3187. case '>':
  3188. b.op = le
  3189. return b, nil
  3190. case ge:
  3191. b.op = '<'
  3192. return b, nil
  3193. case '<':
  3194. b.op = ge
  3195. return b, nil
  3196. case le:
  3197. b.op = '>'
  3198. return b, nil
  3199. }
  3200. }
  3201. u, ok := l.(*unaryOperation)
  3202. if ok && u.op == '!' { // !!x: x
  3203. return u.v, nil
  3204. }
  3205. }
  3206. return &unaryOperation{op, l}, nil
  3207. }
  3208. func (u *unaryOperation) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3209. v, err := u.v.clone(arg, unqualify...)
  3210. if err != nil {
  3211. return nil, err
  3212. }
  3213. return &unaryOperation{op: u.op, v: v}, nil
  3214. }
  3215. func (u *unaryOperation) isStatic() bool { return u.v.isStatic() }
  3216. func (u *unaryOperation) String() string {
  3217. switch u.v.(type) {
  3218. case *binaryOperation:
  3219. return fmt.Sprintf("%s(%s)", iop(u.op), u.v)
  3220. default:
  3221. return fmt.Sprintf("%s%s", iop(u.op), u.v)
  3222. }
  3223. }
  3224. func (u *unaryOperation) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (r interface{}, err error) {
  3225. defer func() {
  3226. if e := recover(); e != nil {
  3227. switch x := e.(type) {
  3228. case error:
  3229. r, err = nil, x
  3230. default:
  3231. r, err = nil, fmt.Errorf("%v", x)
  3232. }
  3233. }
  3234. }()
  3235. switch op := u.op; op {
  3236. case '!':
  3237. a := eval(u.v, execCtx, ctx)
  3238. if a == nil {
  3239. return
  3240. }
  3241. switch x := a.(type) {
  3242. case bool:
  3243. return !x, nil
  3244. default:
  3245. return undOp(a, op)
  3246. }
  3247. case '^':
  3248. a := eval(u.v, execCtx, ctx)
  3249. if a == nil {
  3250. return
  3251. }
  3252. switch x := a.(type) {
  3253. //case nil:
  3254. case idealComplex:
  3255. return undOp(a, op)
  3256. case idealFloat:
  3257. return undOp(a, op)
  3258. case idealInt:
  3259. return ^x, nil
  3260. case idealRune:
  3261. return ^x, nil
  3262. case idealUint:
  3263. return ^x, nil
  3264. case bool:
  3265. return undOp(a, op)
  3266. case complex64:
  3267. return undOp(a, op)
  3268. case complex128:
  3269. return undOp(a, op)
  3270. case float32:
  3271. return undOp(a, op)
  3272. case float64:
  3273. return undOp(a, op)
  3274. case int8:
  3275. return ^x, nil
  3276. case int16:
  3277. return ^x, nil
  3278. case int32:
  3279. return ^x, nil
  3280. case int64:
  3281. return ^x, nil
  3282. case string:
  3283. return undOp(a, op)
  3284. case uint8:
  3285. return ^x, nil
  3286. case uint16:
  3287. return ^x, nil
  3288. case uint32:
  3289. return ^x, nil
  3290. case uint64:
  3291. return ^x, nil
  3292. case *big.Int:
  3293. var z big.Int
  3294. return z.Not(x), nil
  3295. case time.Duration:
  3296. return ^x, nil
  3297. default:
  3298. return undOp(a, op)
  3299. }
  3300. case '+':
  3301. a := eval(u.v, execCtx, ctx)
  3302. if a == nil {
  3303. return
  3304. }
  3305. switch x := a.(type) {
  3306. //case nil:
  3307. case idealComplex:
  3308. return +x, nil
  3309. case idealFloat:
  3310. return +x, nil
  3311. case idealInt:
  3312. return +x, nil
  3313. case idealRune:
  3314. return +x, nil
  3315. case idealUint:
  3316. return +x, nil
  3317. case bool:
  3318. return undOp(a, op)
  3319. case complex64:
  3320. return +x, nil
  3321. case complex128:
  3322. return +x, nil
  3323. case float32:
  3324. return +x, nil
  3325. case float64:
  3326. return +x, nil
  3327. case int8:
  3328. return +x, nil
  3329. case int16:
  3330. return +x, nil
  3331. case int32:
  3332. return +x, nil
  3333. case int64:
  3334. return +x, nil
  3335. case string:
  3336. return undOp(a, op)
  3337. case uint8:
  3338. return +x, nil
  3339. case uint16:
  3340. return +x, nil
  3341. case uint32:
  3342. return +x, nil
  3343. case uint64:
  3344. return +x, nil
  3345. case *big.Int:
  3346. var z big.Int
  3347. return z.Set(x), nil
  3348. case *big.Rat:
  3349. var z big.Rat
  3350. return z.Set(x), nil
  3351. case time.Duration:
  3352. return x, nil
  3353. default:
  3354. return undOp(a, op)
  3355. }
  3356. case '-':
  3357. a := eval(u.v, execCtx, ctx)
  3358. if a == nil {
  3359. return
  3360. }
  3361. switch x := a.(type) {
  3362. //case nil:
  3363. case idealComplex:
  3364. return -x, nil
  3365. case idealFloat:
  3366. return -x, nil
  3367. case idealInt:
  3368. return -x, nil
  3369. case idealRune:
  3370. return -x, nil
  3371. case idealUint:
  3372. return -x, nil
  3373. case bool:
  3374. return undOp(a, op)
  3375. case complex64:
  3376. return -x, nil
  3377. case complex128:
  3378. return -x, nil
  3379. case float32:
  3380. return -x, nil
  3381. case float64:
  3382. return -x, nil
  3383. case int8:
  3384. return -x, nil
  3385. case int16:
  3386. return -x, nil
  3387. case int32:
  3388. return -x, nil
  3389. case int64:
  3390. return -x, nil
  3391. case string:
  3392. return undOp(a, op)
  3393. case uint8:
  3394. return -x, nil
  3395. case uint16:
  3396. return -x, nil
  3397. case uint32:
  3398. return -x, nil
  3399. case uint64:
  3400. return -x, nil
  3401. case *big.Int:
  3402. var z big.Int
  3403. return z.Neg(x), nil
  3404. case *big.Rat:
  3405. var z big.Rat
  3406. return z.Neg(x), nil
  3407. case time.Duration:
  3408. return -x, nil
  3409. default:
  3410. return undOp(a, op)
  3411. }
  3412. default:
  3413. panic("internal error 039")
  3414. }
  3415. }
  3416. type call struct {
  3417. f string
  3418. arg []expression
  3419. }
  3420. func newCall(f string, arg []expression) (v expression, isAgg bool, err error) {
  3421. x := builtin[f]
  3422. if x.f == nil {
  3423. return nil, false, fmt.Errorf("undefined: %s", f)
  3424. }
  3425. isAgg = x.isAggregate
  3426. if g, min, max := len(arg), x.minArgs, x.maxArgs; g < min || g > max {
  3427. a := []interface{}{}
  3428. for _, v := range arg {
  3429. a = append(a, v)
  3430. }
  3431. return nil, false, badNArgs(min, f, a)
  3432. }
  3433. c := call{f: f}
  3434. for _, val := range arg {
  3435. if !val.isStatic() {
  3436. c.arg = append(c.arg, val)
  3437. continue
  3438. }
  3439. eval, err := val.eval(nil, nil)
  3440. if err != nil {
  3441. return nil, isAgg, err
  3442. }
  3443. c.arg = append(c.arg, value{eval})
  3444. }
  3445. return &c, isAgg, nil
  3446. }
  3447. func (c *call) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3448. list, err := cloneExpressionList(arg, c.arg)
  3449. if err != nil {
  3450. return nil, err
  3451. }
  3452. return &call{f: c.f, arg: list}, nil
  3453. }
  3454. func (c *call) isStatic() bool {
  3455. v := builtin[c.f]
  3456. if v.f == nil || !v.isStatic {
  3457. return false
  3458. }
  3459. for _, v := range c.arg {
  3460. if !v.isStatic() {
  3461. return false
  3462. }
  3463. }
  3464. return true
  3465. }
  3466. func (c *call) String() string {
  3467. a := []string{}
  3468. for _, v := range c.arg {
  3469. a = append(a, v.String())
  3470. }
  3471. return fmt.Sprintf("%s(%s)", c.f, strings.Join(a, ", "))
  3472. }
  3473. func (c *call) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3474. f, ok := builtin[c.f]
  3475. if !ok {
  3476. return nil, fmt.Errorf("unknown function %s", c.f)
  3477. }
  3478. isID := c.f == "id"
  3479. a := make([]interface{}, len(c.arg))
  3480. for i, arg := range c.arg {
  3481. if v, err = expand1(arg.eval(execCtx, ctx)); err != nil {
  3482. if !isID {
  3483. return nil, err
  3484. }
  3485. if _, ok := arg.(*ident); !ok {
  3486. return nil, err
  3487. }
  3488. a[i] = arg
  3489. continue
  3490. }
  3491. a[i] = v
  3492. }
  3493. if ctx != nil {
  3494. ctx["$fn"] = c
  3495. }
  3496. return f.f(a, ctx)
  3497. }
  3498. type parameter struct {
  3499. n int
  3500. }
  3501. func (p parameter) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3502. i := p.n - 1
  3503. if i < len(arg) {
  3504. return value{val: arg[i]}, nil
  3505. }
  3506. return nil, fmt.Errorf("missing %s", p)
  3507. }
  3508. func (parameter) isStatic() bool { return false }
  3509. func (p parameter) String() string { return fmt.Sprintf("$%d", p.n) }
  3510. func (p parameter) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3511. i := p.n - 1
  3512. if i < len(execCtx.arg) {
  3513. return execCtx.arg[i], nil
  3514. }
  3515. return nil, fmt.Errorf("missing %s", p)
  3516. }
  3517. //MAYBE make it an unary operation
  3518. type isNull struct {
  3519. expr expression
  3520. not bool
  3521. }
  3522. //LATER newIsNull
  3523. func (is *isNull) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3524. expr, err := is.expr.clone(arg, unqualify...)
  3525. if err != nil {
  3526. return nil, err
  3527. }
  3528. return &isNull{expr: expr, not: is.not}, nil
  3529. }
  3530. func (is *isNull) isStatic() bool { return is.expr.isStatic() }
  3531. func (is *isNull) String() string {
  3532. if is.not {
  3533. return fmt.Sprintf("%s IS NOT NULL", is.expr)
  3534. }
  3535. return fmt.Sprintf("%s IS NULL", is.expr)
  3536. }
  3537. func (is *isNull) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3538. val, err := is.expr.eval(execCtx, ctx)
  3539. if err != nil {
  3540. return
  3541. }
  3542. return val == nil != is.not, nil
  3543. }
  3544. type indexOp struct {
  3545. expr, x expression
  3546. }
  3547. func newIndex(sv, xv expression) (v expression, err error) {
  3548. s, fs, i := "", false, uint64(0)
  3549. x := indexOp{sv, xv}
  3550. if x.expr.isStatic() {
  3551. v, err := x.expr.eval(nil, nil)
  3552. if err != nil {
  3553. return nil, err
  3554. }
  3555. if v == nil {
  3556. return value{nil}, nil
  3557. }
  3558. if s, fs = v.(string); !fs {
  3559. return nil, invXOp(sv, xv)
  3560. }
  3561. x.expr = value{s}
  3562. }
  3563. if x.x.isStatic() {
  3564. v, err := x.x.eval(nil, nil)
  3565. if err != nil {
  3566. return nil, err
  3567. }
  3568. if v == nil {
  3569. return value{nil}, nil
  3570. }
  3571. var p *string
  3572. if fs {
  3573. p = &s
  3574. }
  3575. if i, err = indexExpr(p, v); err != nil {
  3576. return nil, err
  3577. }
  3578. x.x = value{i}
  3579. }
  3580. return &x, nil
  3581. }
  3582. func (x *indexOp) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3583. expr, err := x.expr.clone(arg, unqualify...)
  3584. if err != nil {
  3585. return nil, err
  3586. }
  3587. x2, err := x.x.clone(arg, unqualify...)
  3588. if err != nil {
  3589. return nil, err
  3590. }
  3591. return &indexOp{expr: expr, x: x2}, nil
  3592. }
  3593. func (x *indexOp) isStatic() bool {
  3594. return x.expr.isStatic() && x.x.isStatic()
  3595. }
  3596. func (x *indexOp) String() string { return fmt.Sprintf("%s[%s]", x.expr, x.x) }
  3597. func (x *indexOp) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3598. s0, err := x.expr.eval(execCtx, ctx)
  3599. if err != nil {
  3600. return nil, runErr(err)
  3601. }
  3602. s, ok := s0.(string)
  3603. if !ok {
  3604. return nil, runErr(invXOp(s0, x.x))
  3605. }
  3606. i0, err := x.x.eval(execCtx, ctx)
  3607. if err != nil {
  3608. return nil, runErr(err)
  3609. }
  3610. if i0 == nil {
  3611. return nil, nil
  3612. }
  3613. i, err := indexExpr(&s, i0)
  3614. if err != nil {
  3615. return nil, runErr(err)
  3616. }
  3617. return s[i], nil
  3618. }
  3619. type slice struct {
  3620. expr expression
  3621. lo, hi *expression
  3622. }
  3623. func newSlice(e expression, lo, hi *expression) (v expression, err error) {
  3624. y := slice{e, lo, hi}
  3625. var val interface{}
  3626. if e := y.expr; e.isStatic() {
  3627. if val, err = e.eval(nil, nil); err != nil {
  3628. return nil, err
  3629. }
  3630. if val == nil {
  3631. return value{nil}, nil
  3632. }
  3633. y.expr = value{val}
  3634. }
  3635. if p := y.lo; p != nil {
  3636. if e := expr(*p); e.isStatic() {
  3637. if val, err = e.eval(nil, nil); err != nil {
  3638. return nil, err
  3639. }
  3640. if val == nil {
  3641. return value{nil}, nil
  3642. }
  3643. v := expression(value{val})
  3644. y.lo = &v
  3645. }
  3646. }
  3647. if p := y.hi; p != nil {
  3648. if e := expr(*p); e.isStatic() {
  3649. if val, err = e.eval(nil, nil); err != nil {
  3650. return nil, err
  3651. }
  3652. if val == nil {
  3653. return value{nil}, nil
  3654. }
  3655. v := expression(value{val})
  3656. y.hi = &v
  3657. }
  3658. }
  3659. return &y, nil
  3660. }
  3661. func (s *slice) clone(arg []interface{}, unqualify ...string) (expression, error) {
  3662. expr, err := s.expr.clone(arg, unqualify...)
  3663. if err != nil {
  3664. return nil, err
  3665. }
  3666. r := &slice{expr: expr, lo: s.lo, hi: s.hi}
  3667. if s.lo != nil {
  3668. e, err := (*s.lo).clone(arg, unqualify...)
  3669. if err != nil {
  3670. return nil, err
  3671. }
  3672. r.lo = &e
  3673. }
  3674. if s.hi != nil {
  3675. e, err := (*s.hi).clone(arg, unqualify...)
  3676. if err != nil {
  3677. return nil, err
  3678. }
  3679. r.hi = &e
  3680. }
  3681. return r, nil
  3682. }
  3683. func (s *slice) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) {
  3684. s0, err := s.expr.eval(execCtx, ctx)
  3685. if err != nil {
  3686. return
  3687. }
  3688. if s0 == nil {
  3689. return
  3690. }
  3691. ss, ok := s0.(string)
  3692. if !ok {
  3693. return nil, runErr(invSOp(s0))
  3694. }
  3695. var iLo, iHi uint64
  3696. if s.lo != nil {
  3697. i, err := (*s.lo).eval(execCtx, ctx)
  3698. if err != nil {
  3699. return nil, err
  3700. }
  3701. if i == nil {
  3702. return nil, err
  3703. }
  3704. if iLo, err = sliceExpr(&ss, i, 0); err != nil {
  3705. return nil, err
  3706. }
  3707. }
  3708. iHi = uint64(len(ss))
  3709. if s.hi != nil {
  3710. i, err := (*s.hi).eval(execCtx, ctx)
  3711. if err != nil {
  3712. return nil, err
  3713. }
  3714. if i == nil {
  3715. return nil, err
  3716. }
  3717. if iHi, err = sliceExpr(&ss, i, 1); err != nil {
  3718. return nil, err
  3719. }
  3720. }
  3721. return ss[iLo:iHi], nil
  3722. }
  3723. func (s *slice) isStatic() bool {
  3724. if !s.expr.isStatic() {
  3725. return false
  3726. }
  3727. if p := s.lo; p != nil && !(*p).isStatic() {
  3728. return false
  3729. }
  3730. if p := s.hi; p != nil && !(*p).isStatic() {
  3731. return false
  3732. }
  3733. return false
  3734. }
  3735. func (s *slice) String() string {
  3736. switch {
  3737. case s.lo == nil && s.hi == nil:
  3738. return fmt.Sprintf("%v[:]", s.expr)
  3739. case s.lo == nil && s.hi != nil:
  3740. return fmt.Sprintf("%v[:%v]", s.expr, *s.hi)
  3741. case s.lo != nil && s.hi == nil:
  3742. return fmt.Sprintf("%v[%v:]", s.expr, *s.lo)
  3743. default: //case s.lo != nil && s.hi != nil:
  3744. return fmt.Sprintf("%v[%v:%v]", s.expr, *s.lo, *s.hi)
  3745. }
  3746. }