@@ -9,6 +9,7 @@ import at.petrak.hexcasting.api.utils.NBTBuilder
99import  at.petrak.hexcasting.api.utils.getList 
1010import  at.petrak.hexcasting.api.utils.hasList 
1111import  at.petrak.hexcasting.api.utils.serializeToNBT 
12+ import  at.petrak.hexcasting.api.utils.Vec 
1213import  at.petrak.hexcasting.common.lib.hex.HexEvalSounds 
1314import  at.petrak.hexcasting.common.lib.hex.HexIotaTypes 
1415import  net.minecraft.nbt.CompoundTag 
@@ -28,14 +29,13 @@ data class FrameForEach(
2829    val  data :  SpellList ,
2930    val  code :  SpellList ,
3031    val  baseStack :  List <Iota >? ,
31-     val  acc :  MutableList <Iota >
32+     val  acc :  Vec <Iota >
3233) : ContinuationFrame {
3334
3435    /* * When halting, we add the stack state at halt to the stack accumulator, then return the original pre-Thoth stack, plus the accumulator. */ 
3536    override  fun  breakDownwards (stack :  List <Iota >): Pair <Boolean , List <Iota >> {
3637        val  newStack =  baseStack?.toMutableList() ? :  mutableListOf ()
37-         acc.addAll(stack)
38-         newStack.add(ListIota (acc))
38+         newStack.add(ListIota (acc.appendAll(stack).toList()))
3939        return  true  to newStack
4040    }
4141
@@ -46,27 +46,26 @@ data class FrameForEach(
4646        harness :  CastingVM 
4747    ): CastResult  {
4848        //  If this isn't the very first Thoth step (i.e. no Thoth computations run yet)...
49-         val  stack =  if  (baseStack ==  null ) {
49+         val  ( stack, nextAcc)  =  if  (baseStack ==  null ) {
5050            //  init stack to the harness stack...
51-             harness.image.stack.toList()
51+             harness.image.stack.toList() to acc 
5252        } else  {
5353            //  else save the stack to the accumulator and reuse the saved base stack.
54-             acc.addAll(harness.image.stack)
55-             baseStack
54+             baseStack to acc.appendAll(harness.image.stack)
5655        }
5756
5857        //  If we still have data to process...
5958        val  (stackTop, newImage, newCont) =  if  (data.nonEmpty) {
6059            //  push the next datum to the top of the stack,
6160            val  cont2 =  continuation
6261                //  put the next Thoth object back on the stack for the next Thoth cycle,
63-                 .pushFrame(FrameForEach (data.cdr, code, stack, acc ))
62+                 .pushFrame(FrameForEach (data.cdr, code, stack, nextAcc ))
6463                //  and prep the Thoth'd code block for evaluation.
6564                .pushFrame(FrameEvaluate (code, true ))
6665            Triple (data.car, harness.image.withUsedOp(), cont2)
6766        } else  {
6867            //  Else, dump our final list onto the stack.
69-             Triple (ListIota (acc), harness.image, continuation)
68+             Triple (ListIota (acc.toList() ), harness.image, continuation)
7069        }
7170        val  tStack =  stack.toMutableList()
7271        tStack.add(stackTop)
@@ -86,10 +85,10 @@ data class FrameForEach(
8685        " code" % =  code.serializeToNBT()
8786        if  (baseStack !=  null )
8887            " base" % =  baseStack.serializeToNBT()
89-         " accumulator" % =  acc.serializeToNBT()
88+         " accumulator" % =  acc.toList(). serializeToNBT()
9089    }
9190
92-     override  fun  size () =  data.size() +  code.size() +  acc.size  +  (baseStack?.size ? :  0 )
91+     override  fun  size () =  data.size() +  code.size() +  acc.length  +  (baseStack?.size ? :  0 )
9392
9493    override  val  type:  ContinuationFrame .Type <* > =  TYPE 
9594
@@ -104,10 +103,10 @@ data class FrameForEach(
104103                        HexIotaTypes .LIST .deserialize(tag.getList(" base" Tag .TAG_COMPOUND ), world)!! .list.toList()
105104                    else 
106105                        null ,
107-                     HexIotaTypes .LIST .deserialize(
106+                     Vec .ofIterable( HexIotaTypes .LIST .deserialize(
108107                        tag.getList(" accumulator" Tag .TAG_COMPOUND ),
109108                        world
110-                     )!! .list.toMutableList( )
109+                     )!! .list)
111110                )
112111            }
113112
0 commit comments