expr.go 74 KB

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