1. # The [...] term creates an Array.
  2. proto circumfix:<[ ]>(|) { * }
  3. multi circumfix:<[ ]>() {
  4. nqp::create(Array)
  5. }
  6. multi circumfix:<[ ]>(Iterable:D \iterable) {
  7. my $reified;
  8. nqp::if(
  9. nqp::iscont(iterable),
  10. nqp::p6bindattrinvres(
  11. nqp::create(Array),List,'$!reified',
  12. nqp::stmts(
  13. nqp::push(
  14. ($reified := nqp::create(IterationBuffer)),
  15. nqp::assign(nqp::p6scalarfromdesc(nqp::null),iterable)
  16. ),
  17. $reified
  18. )
  19. ),
  20. nqp::if(
  21. nqp::eqaddr(iterable.WHAT,List),
  22. nqp::if(
  23. iterable.is-lazy,
  24. Array.from-iterator(iterable.iterator),
  25. nqp::stmts( # immutable List
  26. (my int $elems = iterable.elems), # reifies
  27. (my $params := nqp::getattr(iterable,List,'$!reified')),
  28. (my int $i = -1),
  29. ($reified := nqp::setelems(nqp::create(IterationBuffer),$elems)),
  30. nqp::while(
  31. nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
  32. nqp::bindpos($reified,$i,nqp::assign(
  33. nqp::p6scalarfromdesc(nqp::null),nqp::atpos($params,$i))
  34. )
  35. ),
  36. nqp::p6bindattrinvres(nqp::create(Array),List,'$!reified',$reified)
  37. ),
  38. ),
  39. Array.from-iterator(iterable.iterator)
  40. )
  41. )
  42. }
  43. multi circumfix:<[ ]>(Mu \x) { # really only for [$foo]
  44. nqp::p6bindattrinvres(
  45. nqp::create(Array),List,'$!reified',
  46. nqp::stmts(
  47. nqp::push(
  48. (my $reified := nqp::create(IterationBuffer)),
  49. nqp::assign(nqp::p6scalarfromdesc(nqp::null),x)
  50. ),
  51. $reified
  52. )
  53. )
  54. }
  55. proto sub pop(@) {*}
  56. multi sub pop(@a) { @a.pop }
  57. proto sub shift(@) {*}
  58. multi sub shift(@a) { @a.shift }
  59. sub push (\a, |elems) { a.push: |elems }
  60. sub append (\a, |elems) { a.append: |elems }
  61. sub unshift(\a, |elems) { a.unshift: |elems }
  62. sub prepend(\a, |elems) { a.prepend: |elems }
  63. sub splice(@arr, |c) { @arr.splice(|c) }