Skip to content

Commit

Permalink
Fix FrameForEach to use Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwinfy authored and vgskye committed Dec 4, 2024
1 parent 533753e commit 9d5b762
Showing 1 changed file with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import at.petrak.hexcasting.api.utils.NBTBuilder
import at.petrak.hexcasting.api.utils.getList
import at.petrak.hexcasting.api.utils.hasList
import at.petrak.hexcasting.api.utils.serializeToNBT
import at.petrak.hexcasting.api.utils.Vec
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
import net.minecraft.nbt.CompoundTag
Expand All @@ -28,14 +29,13 @@ data class FrameForEach(
val data: SpellList,
val code: SpellList,
val baseStack: List<Iota>?,
val acc: MutableList<Iota>
val acc: Vec<Iota>
) : ContinuationFrame {

/** When halting, we add the stack state at halt to the stack accumulator, then return the original pre-Thoth stack, plus the accumulator. */
override fun breakDownwards(stack: List<Iota>): Pair<Boolean, List<Iota>> {
val newStack = baseStack?.toMutableList() ?: mutableListOf()
acc.addAll(stack)
newStack.add(ListIota(acc))
newStack.add(ListIota(acc.appendAll(stack).toList()))
return true to newStack
}

Expand All @@ -46,27 +46,26 @@ data class FrameForEach(
harness: CastingVM
): CastResult {
// If this isn't the very first Thoth step (i.e. no Thoth computations run yet)...
val stack = if (baseStack == null) {
val (stack, nextAcc) = if (baseStack == null) {
// init stack to the harness stack...
harness.image.stack.toList()
harness.image.stack.toList() to acc
} else {
// else save the stack to the accumulator and reuse the saved base stack.
acc.addAll(harness.image.stack)
baseStack
baseStack to acc.appendAll(harness.image.stack)
}

// If we still have data to process...
val (stackTop, newImage, newCont) = if (data.nonEmpty) {
// push the next datum to the top of the stack,
val cont2 = continuation
// put the next Thoth object back on the stack for the next Thoth cycle,
.pushFrame(FrameForEach(data.cdr, code, stack, acc))
.pushFrame(FrameForEach(data.cdr, code, stack, nextAcc))
// and prep the Thoth'd code block for evaluation.
.pushFrame(FrameEvaluate(code, true))
Triple(data.car, harness.image.withUsedOp(), cont2)
} else {
// Else, dump our final list onto the stack.
Triple(ListIota(acc), harness.image, continuation)
Triple(ListIota(acc.toList()), harness.image, continuation)
}
val tStack = stack.toMutableList()
tStack.add(stackTop)
Expand All @@ -86,10 +85,10 @@ data class FrameForEach(
"code" %= code.serializeToNBT()
if (baseStack != null)
"base" %= baseStack.serializeToNBT()
"accumulator" %= acc.serializeToNBT()
"accumulator" %= acc.toList().serializeToNBT()
}

override fun size() = data.size() + code.size() + acc.size + (baseStack?.size ?: 0)
override fun size() = data.size() + code.size() + acc.length + (baseStack?.size ?: 0)

override val type: ContinuationFrame.Type<*> = TYPE

Expand All @@ -104,10 +103,10 @@ data class FrameForEach(
HexIotaTypes.LIST.deserialize(tag.getList("base", Tag.TAG_COMPOUND), world)!!.list.toList()
else
null,
HexIotaTypes.LIST.deserialize(
Vec.ofIterable(HexIotaTypes.LIST.deserialize(
tag.getList("accumulator", Tag.TAG_COMPOUND),
world
)!!.list.toMutableList()
)!!.list)
)
}

Expand Down

0 comments on commit 9d5b762

Please sign in to comment.