vmExpr.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. package vm
  2. import (
  3. "reflect"
  4. "github.com/mattn/anko/ast"
  5. "github.com/mattn/anko/env"
  6. )
  7. // invokeExpr evaluates one expression.
  8. func (runInfo *runInfoStruct) invokeExpr() {
  9. switch expr := runInfo.expr.(type) {
  10. // OpExpr
  11. case *ast.OpExpr:
  12. runInfo.operator = expr.Op
  13. runInfo.invokeOperator()
  14. // IdentExpr
  15. case *ast.IdentExpr:
  16. runInfo.rv, runInfo.err = runInfo.env.GetValue(expr.Lit)
  17. if runInfo.err != nil {
  18. runInfo.err = newError(expr, runInfo.err)
  19. }
  20. // LiteralExpr
  21. case *ast.LiteralExpr:
  22. runInfo.rv = expr.Literal
  23. // ArrayExpr
  24. case *ast.ArrayExpr:
  25. if expr.TypeData == nil {
  26. slice := make([]interface{}, len(expr.Exprs))
  27. var i int
  28. for i, runInfo.expr = range expr.Exprs {
  29. runInfo.invokeExpr()
  30. if runInfo.err != nil {
  31. return
  32. }
  33. slice[i] = runInfo.rv.Interface()
  34. }
  35. runInfo.rv = reflect.ValueOf(slice)
  36. return
  37. }
  38. t := makeType(runInfo, expr.TypeData)
  39. if runInfo.err != nil {
  40. runInfo.rv = nilValue
  41. return
  42. }
  43. if t == nil {
  44. runInfo.err = newStringError(expr, "cannot make type nil")
  45. runInfo.rv = nilValue
  46. return
  47. }
  48. slice := reflect.MakeSlice(t, len(expr.Exprs), len(expr.Exprs))
  49. var i int
  50. valueType := t.Elem()
  51. for i, runInfo.expr = range expr.Exprs {
  52. runInfo.invokeExpr()
  53. if runInfo.err != nil {
  54. return
  55. }
  56. runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, valueType)
  57. if runInfo.err != nil {
  58. runInfo.err = newStringError(expr, "cannot use type "+runInfo.rv.Type().String()+" as type "+valueType.String()+" as slice value")
  59. runInfo.rv = nilValue
  60. return
  61. }
  62. slice.Index(i).Set(runInfo.rv)
  63. }
  64. runInfo.rv = slice
  65. // MapExpr
  66. case *ast.MapExpr:
  67. if expr.TypeData == nil {
  68. var i int
  69. var key reflect.Value
  70. m := make(map[interface{}]interface{}, len(expr.Keys))
  71. for i, runInfo.expr = range expr.Keys {
  72. runInfo.invokeExpr()
  73. if runInfo.err != nil {
  74. return
  75. }
  76. key = runInfo.rv
  77. runInfo.expr = expr.Values[i]
  78. runInfo.invokeExpr()
  79. if runInfo.err != nil {
  80. return
  81. }
  82. m[key.Interface()] = runInfo.rv.Interface()
  83. }
  84. runInfo.rv = reflect.ValueOf(m)
  85. return
  86. }
  87. t := makeType(runInfo, expr.TypeData)
  88. if runInfo.err != nil {
  89. runInfo.rv = nilValue
  90. return
  91. }
  92. if t == nil {
  93. runInfo.err = newStringError(expr, "cannot make type nil")
  94. runInfo.rv = nilValue
  95. return
  96. }
  97. runInfo.rv, runInfo.err = makeValue(t)
  98. if runInfo.err != nil {
  99. runInfo.rv = nilValue
  100. return
  101. }
  102. var i int
  103. var key reflect.Value
  104. m := runInfo.rv
  105. keyType := t.Key()
  106. valueType := t.Elem()
  107. for i, runInfo.expr = range expr.Keys {
  108. runInfo.invokeExpr()
  109. if runInfo.err != nil {
  110. return
  111. }
  112. key, runInfo.err = convertReflectValueToType(runInfo.rv, keyType)
  113. if runInfo.err != nil {
  114. runInfo.err = newStringError(expr, "cannot use type "+key.Type().String()+" as type "+keyType.String()+" as map key")
  115. runInfo.rv = nilValue
  116. return
  117. }
  118. runInfo.expr = expr.Values[i]
  119. runInfo.invokeExpr()
  120. if runInfo.err != nil {
  121. return
  122. }
  123. runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, valueType)
  124. if runInfo.err != nil {
  125. runInfo.err = newStringError(expr, "cannot use type "+runInfo.rv.Type().String()+" as type "+valueType.String()+" as map value")
  126. runInfo.rv = nilValue
  127. return
  128. }
  129. m.SetMapIndex(key, runInfo.rv)
  130. }
  131. runInfo.rv = m
  132. // DerefExpr
  133. case *ast.DerefExpr:
  134. runInfo.expr = expr.Expr
  135. runInfo.invokeExpr()
  136. if runInfo.err != nil {
  137. return
  138. }
  139. if runInfo.rv.Kind() != reflect.Ptr {
  140. runInfo.err = newStringError(expr.Expr, "cannot deference non-pointer")
  141. runInfo.rv = nilValue
  142. return
  143. }
  144. runInfo.rv = runInfo.rv.Elem()
  145. // AddrExpr
  146. case *ast.AddrExpr:
  147. runInfo.expr = expr.Expr
  148. runInfo.invokeExpr()
  149. if runInfo.err != nil {
  150. return
  151. }
  152. if runInfo.rv.CanAddr() {
  153. runInfo.rv = runInfo.rv.Addr()
  154. } else {
  155. i := runInfo.rv.Interface()
  156. runInfo.rv = reflect.ValueOf(&i)
  157. }
  158. // UnaryExpr
  159. case *ast.UnaryExpr:
  160. runInfo.expr = expr.Expr
  161. runInfo.invokeExpr()
  162. if runInfo.err != nil {
  163. return
  164. }
  165. switch expr.Operator {
  166. case "-":
  167. switch runInfo.rv.Kind() {
  168. case reflect.Int64:
  169. runInfo.rv = reflect.ValueOf(-runInfo.rv.Int())
  170. case reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int, reflect.Bool:
  171. runInfo.rv = reflect.ValueOf(-toInt64(runInfo.rv))
  172. case reflect.Float64:
  173. runInfo.rv = reflect.ValueOf(-runInfo.rv.Float())
  174. default:
  175. runInfo.rv = reflect.ValueOf(-toFloat64(runInfo.rv))
  176. }
  177. case "^":
  178. runInfo.rv = reflect.ValueOf(^toInt64(runInfo.rv))
  179. case "!":
  180. if toBool(runInfo.rv) {
  181. runInfo.rv = falseValue
  182. } else {
  183. runInfo.rv = trueValue
  184. }
  185. default:
  186. runInfo.err = newStringError(expr, "unknown operator")
  187. runInfo.rv = nilValue
  188. }
  189. // ParenExpr
  190. case *ast.ParenExpr:
  191. runInfo.expr = expr.SubExpr
  192. runInfo.invokeExpr()
  193. if runInfo.err != nil {
  194. return
  195. }
  196. // MemberExpr
  197. case *ast.MemberExpr:
  198. runInfo.expr = expr.Expr
  199. runInfo.invokeExpr()
  200. if runInfo.err != nil {
  201. return
  202. }
  203. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  204. runInfo.rv = runInfo.rv.Elem()
  205. }
  206. if env, ok := runInfo.rv.Interface().(*env.Env); ok {
  207. runInfo.rv, runInfo.err = env.GetValue(expr.Name)
  208. if runInfo.err != nil {
  209. runInfo.err = newError(expr, runInfo.err)
  210. runInfo.rv = nilValue
  211. }
  212. return
  213. }
  214. value := runInfo.rv.MethodByName(expr.Name)
  215. if value.IsValid() {
  216. runInfo.rv = value
  217. return
  218. }
  219. if runInfo.rv.Kind() == reflect.Ptr {
  220. runInfo.rv = runInfo.rv.Elem()
  221. }
  222. switch runInfo.rv.Kind() {
  223. case reflect.Struct:
  224. field, found := runInfo.rv.Type().FieldByName(expr.Name)
  225. if found {
  226. runInfo.rv = runInfo.rv.FieldByIndex(field.Index)
  227. return
  228. }
  229. if runInfo.rv.CanAddr() {
  230. runInfo.rv = runInfo.rv.Addr()
  231. method, found := runInfo.rv.Type().MethodByName(expr.Name)
  232. if found {
  233. runInfo.rv = runInfo.rv.Method(method.Index)
  234. return
  235. }
  236. } else {
  237. // Check wether method with pointer receiver is defined,
  238. // if yes, invoke it in the copied instance
  239. method, found := reflect.PtrTo(runInfo.rv.Type()).MethodByName(expr.Name)
  240. if found {
  241. // Create pointer value to given struct type which were passed by value
  242. cv := reflect.New(runInfo.rv.Type())
  243. cv.Elem().Set(runInfo.rv)
  244. runInfo.rv = cv.Method(method.Index)
  245. return
  246. }
  247. }
  248. runInfo.err = newStringError(expr, "no member named '"+expr.Name+"' for struct")
  249. runInfo.rv = nilValue
  250. case reflect.Map:
  251. runInfo.rv = getMapIndex(reflect.ValueOf(expr.Name), runInfo.rv)
  252. default:
  253. runInfo.err = newStringError(expr, "type "+runInfo.rv.Kind().String()+" does not support member operation")
  254. runInfo.rv = nilValue
  255. }
  256. // ItemExpr
  257. case *ast.ItemExpr:
  258. runInfo.expr = expr.Item
  259. runInfo.invokeExpr()
  260. if runInfo.err != nil {
  261. return
  262. }
  263. item := runInfo.rv
  264. runInfo.expr = expr.Index
  265. runInfo.invokeExpr()
  266. if runInfo.err != nil {
  267. return
  268. }
  269. if item.Kind() == reflect.Interface && !item.IsNil() {
  270. item = item.Elem()
  271. }
  272. switch item.Kind() {
  273. case reflect.String, reflect.Slice, reflect.Array:
  274. var index int
  275. index, runInfo.err = tryToInt(runInfo.rv)
  276. if runInfo.err != nil {
  277. runInfo.err = newStringError(expr, "index must be a number")
  278. runInfo.rv = nilValue
  279. return
  280. }
  281. if index < 0 || index >= item.Len() {
  282. runInfo.err = newStringError(expr, "index out of range")
  283. runInfo.rv = nilValue
  284. return
  285. }
  286. if item.Kind() != reflect.String {
  287. runInfo.rv = item.Index(index)
  288. } else {
  289. // String
  290. runInfo.rv = item.Index(index).Convert(stringType)
  291. }
  292. case reflect.Map:
  293. runInfo.rv = getMapIndex(runInfo.rv, item)
  294. default:
  295. runInfo.err = newStringError(expr, "type "+item.Kind().String()+" does not support index operation")
  296. runInfo.rv = nilValue
  297. }
  298. // SliceExpr
  299. case *ast.SliceExpr:
  300. runInfo.expr = expr.Item
  301. runInfo.invokeExpr()
  302. if runInfo.err != nil {
  303. return
  304. }
  305. item := runInfo.rv
  306. if item.Kind() == reflect.Interface && !item.IsNil() {
  307. item = item.Elem()
  308. }
  309. switch item.Kind() {
  310. case reflect.String, reflect.Slice, reflect.Array:
  311. var beginIndex int
  312. endIndex := item.Len()
  313. if expr.Begin != nil {
  314. runInfo.expr = expr.Begin
  315. runInfo.invokeExpr()
  316. if runInfo.err != nil {
  317. return
  318. }
  319. beginIndex, runInfo.err = tryToInt(runInfo.rv)
  320. if runInfo.err != nil {
  321. runInfo.err = newStringError(expr, "index must be a number")
  322. runInfo.rv = nilValue
  323. return
  324. }
  325. // (0 <= low) <= high <= len(a)
  326. if beginIndex < 0 {
  327. runInfo.err = newStringError(expr, "index out of range")
  328. runInfo.rv = nilValue
  329. return
  330. }
  331. }
  332. if expr.End != nil {
  333. runInfo.expr = expr.End
  334. runInfo.invokeExpr()
  335. if runInfo.err != nil {
  336. return
  337. }
  338. endIndex, runInfo.err = tryToInt(runInfo.rv)
  339. if runInfo.err != nil {
  340. runInfo.err = newStringError(expr, "index must be a number")
  341. runInfo.rv = nilValue
  342. return
  343. }
  344. // 0 <= low <= (high <= len(a))
  345. if endIndex > item.Len() {
  346. runInfo.err = newStringError(expr, "index out of range")
  347. runInfo.rv = nilValue
  348. return
  349. }
  350. }
  351. // 0 <= (low <= high) <= len(a)
  352. if beginIndex > endIndex {
  353. runInfo.err = newStringError(expr, "index out of range")
  354. runInfo.rv = nilValue
  355. return
  356. }
  357. if item.Kind() == reflect.String {
  358. if expr.Cap != nil {
  359. runInfo.err = newStringError(expr, "type string does not support cap")
  360. runInfo.rv = nilValue
  361. return
  362. }
  363. runInfo.rv = item.Slice(beginIndex, endIndex)
  364. return
  365. }
  366. sliceCap := item.Cap()
  367. if expr.Cap != nil {
  368. runInfo.expr = expr.Cap
  369. runInfo.invokeExpr()
  370. if runInfo.err != nil {
  371. return
  372. }
  373. sliceCap, runInfo.err = tryToInt(runInfo.rv)
  374. if runInfo.err != nil {
  375. runInfo.err = newStringError(expr, "cap must be a number")
  376. runInfo.rv = nilValue
  377. return
  378. }
  379. // 0 <= low <= (high <= max <= cap(a))
  380. if sliceCap < endIndex || sliceCap > item.Cap() {
  381. runInfo.err = newStringError(expr, "cap out of range")
  382. runInfo.rv = nilValue
  383. return
  384. }
  385. }
  386. runInfo.rv = item.Slice3(beginIndex, endIndex, sliceCap)
  387. default:
  388. runInfo.err = newStringError(expr, "type "+item.Kind().String()+" does not support slice operation")
  389. runInfo.rv = nilValue
  390. }
  391. // LetsExpr
  392. case *ast.LetsExpr:
  393. var i int
  394. for i, runInfo.expr = range expr.RHSS {
  395. runInfo.invokeExpr()
  396. if runInfo.err != nil {
  397. return
  398. }
  399. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  400. runInfo.rv = runInfo.rv.Elem()
  401. }
  402. if i < len(expr.LHSS) {
  403. runInfo.expr = expr.LHSS[i]
  404. runInfo.invokeLetExpr()
  405. if runInfo.err != nil {
  406. return
  407. }
  408. }
  409. }
  410. // TernaryOpExpr
  411. case *ast.TernaryOpExpr:
  412. runInfo.expr = expr.Expr
  413. runInfo.invokeExpr()
  414. if runInfo.err != nil {
  415. return
  416. }
  417. if toBool(runInfo.rv) {
  418. runInfo.expr = expr.LHS
  419. } else {
  420. runInfo.expr = expr.RHS
  421. }
  422. runInfo.invokeExpr()
  423. // NilCoalescingOpExpr
  424. case *ast.NilCoalescingOpExpr:
  425. // if left side has no error and is not nil, returns left side
  426. // otherwise returns right side
  427. runInfo.expr = expr.LHS
  428. runInfo.invokeExpr()
  429. if runInfo.err == nil {
  430. if !isNil(runInfo.rv) {
  431. return
  432. }
  433. } else {
  434. runInfo.err = nil
  435. }
  436. runInfo.expr = expr.RHS
  437. runInfo.invokeExpr()
  438. // LenExpr
  439. case *ast.LenExpr:
  440. runInfo.expr = expr.Expr
  441. runInfo.invokeExpr()
  442. if runInfo.err != nil {
  443. return
  444. }
  445. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  446. runInfo.rv = runInfo.rv.Elem()
  447. }
  448. switch runInfo.rv.Kind() {
  449. case reflect.Slice, reflect.Array, reflect.Map, reflect.String, reflect.Chan:
  450. runInfo.rv = reflect.ValueOf(int64(runInfo.rv.Len()))
  451. default:
  452. runInfo.err = newStringError(expr, "type "+runInfo.rv.Kind().String()+" does not support len operation")
  453. runInfo.rv = nilValue
  454. }
  455. // ImportExpr
  456. case *ast.ImportExpr:
  457. runInfo.expr = expr.Name
  458. runInfo.invokeExpr()
  459. if runInfo.err != nil {
  460. return
  461. }
  462. runInfo.rv, runInfo.err = convertReflectValueToType(runInfo.rv, stringType)
  463. if runInfo.err != nil {
  464. runInfo.rv = nilValue
  465. return
  466. }
  467. name := runInfo.rv.String()
  468. runInfo.rv = nilValue
  469. methods, ok := env.Packages[name]
  470. if !ok {
  471. runInfo.err = newStringError(expr, "package not found: "+name)
  472. return
  473. }
  474. var err error
  475. pack := runInfo.env.NewEnv()
  476. for methodName, methodValue := range methods {
  477. err = pack.DefineValue(methodName, methodValue)
  478. if err != nil {
  479. runInfo.err = newStringError(expr, "import DefineValue error: "+err.Error())
  480. return
  481. }
  482. }
  483. types, ok := env.PackageTypes[name]
  484. if ok {
  485. for typeName, typeValue := range types {
  486. err = pack.DefineReflectType(typeName, typeValue)
  487. if err != nil {
  488. runInfo.err = newStringError(expr, "import DefineReflectType error: "+err.Error())
  489. return
  490. }
  491. }
  492. }
  493. runInfo.rv = reflect.ValueOf(pack)
  494. // MakeExpr
  495. case *ast.MakeExpr:
  496. t := makeType(runInfo, expr.TypeData)
  497. if runInfo.err != nil {
  498. runInfo.rv = nilValue
  499. return
  500. }
  501. if t == nil {
  502. runInfo.err = newStringError(expr, "cannot make type nil")
  503. runInfo.rv = nilValue
  504. return
  505. }
  506. switch expr.TypeData.Kind {
  507. case ast.TypeSlice:
  508. aLen := 0
  509. if expr.LenExpr != nil {
  510. runInfo.expr = expr.LenExpr
  511. runInfo.invokeExpr()
  512. if runInfo.err != nil {
  513. return
  514. }
  515. aLen = toInt(runInfo.rv)
  516. }
  517. cap := aLen
  518. if expr.CapExpr != nil {
  519. runInfo.expr = expr.CapExpr
  520. runInfo.invokeExpr()
  521. if runInfo.err != nil {
  522. return
  523. }
  524. cap = toInt(runInfo.rv)
  525. }
  526. if aLen > cap {
  527. runInfo.err = newStringError(expr, "make slice len > cap")
  528. runInfo.rv = nilValue
  529. return
  530. }
  531. runInfo.rv = reflect.MakeSlice(t, aLen, cap)
  532. return
  533. case ast.TypeChan:
  534. aLen := 0
  535. if expr.LenExpr != nil {
  536. runInfo.expr = expr.LenExpr
  537. runInfo.invokeExpr()
  538. if runInfo.err != nil {
  539. return
  540. }
  541. aLen = toInt(runInfo.rv)
  542. }
  543. runInfo.rv = reflect.MakeChan(t, aLen)
  544. return
  545. }
  546. runInfo.rv, runInfo.err = makeValue(t)
  547. // MakeTypeExpr
  548. case *ast.MakeTypeExpr:
  549. runInfo.expr = expr.Type
  550. runInfo.invokeExpr()
  551. if runInfo.err != nil {
  552. return
  553. }
  554. // if expr.Name has a dot in it, it should give a syntax error, so no needs to check err
  555. runInfo.env.DefineReflectType(expr.Name, runInfo.rv.Type())
  556. runInfo.rv = reflect.ValueOf(runInfo.rv.Type())
  557. // ChanExpr
  558. case *ast.ChanExpr:
  559. runInfo.expr = expr.RHS
  560. runInfo.invokeExpr()
  561. if runInfo.err != nil {
  562. return
  563. }
  564. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  565. runInfo.rv = runInfo.rv.Elem()
  566. }
  567. var lhs reflect.Value
  568. rhs := runInfo.rv
  569. if expr.LHS == nil {
  570. // lhs is nil
  571. if rhs.Kind() != reflect.Chan {
  572. // rhs is not channel
  573. runInfo.err = newStringError(expr, "receive from non-chan type "+rhs.Kind().String())
  574. runInfo.rv = nilValue
  575. return
  576. }
  577. } else {
  578. // lhs is not nil
  579. runInfo.expr = expr.LHS
  580. runInfo.invokeExpr()
  581. if runInfo.err != nil {
  582. return
  583. }
  584. if runInfo.rv.Kind() == reflect.Interface && !runInfo.rv.IsNil() {
  585. runInfo.rv = runInfo.rv.Elem()
  586. }
  587. if runInfo.rv.Kind() != reflect.Chan {
  588. // lhs is not channel
  589. // lhs <- chan rhs or lhs <- rhs
  590. runInfo.err = newStringError(expr, "send to non-chan type "+runInfo.rv.Kind().String())
  591. runInfo.rv = nilValue
  592. return
  593. }
  594. lhs = runInfo.rv
  595. }
  596. var chosen int
  597. var ok bool
  598. if rhs.Kind() == reflect.Chan {
  599. // rhs is channel
  600. // receive from rhs channel
  601. cases := []reflect.SelectCase{{
  602. Dir: reflect.SelectRecv,
  603. Chan: reflect.ValueOf(runInfo.ctx.Done()),
  604. }, {
  605. Dir: reflect.SelectRecv,
  606. Chan: rhs,
  607. }}
  608. chosen, runInfo.rv, ok = reflect.Select(cases)
  609. if chosen == 0 {
  610. runInfo.err = ErrInterrupt
  611. runInfo.rv = nilValue
  612. return
  613. }
  614. if !ok {
  615. runInfo.rv = nilValue
  616. return
  617. }
  618. rhs = runInfo.rv
  619. }
  620. if expr.LHS == nil {
  621. // <- chan rhs is receive
  622. return
  623. }
  624. // chan lhs <- chan rhs is receive & send
  625. // or
  626. // chan lhs <- rhs is send
  627. runInfo.rv = nilValue
  628. rhs, runInfo.err = convertReflectValueToType(rhs, lhs.Type().Elem())
  629. if runInfo.err != nil {
  630. runInfo.err = newStringError(expr, "cannot use type "+rhs.Type().String()+" as type "+lhs.Type().Elem().String()+" to send to chan")
  631. return
  632. }
  633. // send rhs to lhs channel
  634. cases := []reflect.SelectCase{{
  635. Dir: reflect.SelectRecv,
  636. Chan: reflect.ValueOf(runInfo.ctx.Done()),
  637. }, {
  638. Dir: reflect.SelectSend,
  639. Chan: lhs,
  640. Send: rhs,
  641. }}
  642. if !runInfo.options.Debug {
  643. // captures panic
  644. defer recoverFunc(runInfo)
  645. }
  646. chosen, _, _ = reflect.Select(cases)
  647. if chosen == 0 {
  648. runInfo.err = ErrInterrupt
  649. }
  650. // FuncExpr
  651. case *ast.FuncExpr:
  652. runInfo.expr = expr
  653. runInfo.funcExpr()
  654. // AnonCallExpr
  655. case *ast.AnonCallExpr:
  656. runInfo.expr = expr
  657. runInfo.anonCallExpr()
  658. // CallExpr
  659. case *ast.CallExpr:
  660. runInfo.expr = expr
  661. runInfo.callExpr()
  662. // IncludeExpr
  663. case *ast.IncludeExpr:
  664. runInfo.expr = expr.ItemExpr
  665. runInfo.invokeExpr()
  666. if runInfo.err != nil {
  667. return
  668. }
  669. itemExpr := runInfo.rv
  670. runInfo.expr = expr.ListExpr
  671. runInfo.invokeExpr()
  672. if runInfo.err != nil {
  673. return
  674. }
  675. if runInfo.rv.Kind() != reflect.Slice && runInfo.rv.Kind() != reflect.Array {
  676. runInfo.err = newStringError(expr, "second argument must be slice or array; but have "+runInfo.rv.Kind().String())
  677. runInfo.rv = nilValue
  678. return
  679. }
  680. for i := 0; i < runInfo.rv.Len(); i++ {
  681. if equal(itemExpr, runInfo.rv.Index(i)) {
  682. runInfo.rv = trueValue
  683. return
  684. }
  685. }
  686. runInfo.rv = falseValue
  687. default:
  688. runInfo.err = newStringError(expr, "unknown expression")
  689. runInfo.rv = nilValue
  690. }
  691. }