Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.casting.eval.ResolvedPatternType
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.iota.ListIota
import at.petrak.hexcasting.api.utils.NBTBuilder
import at.petrak.hexcasting.api.utils.TreeList
import at.petrak.hexcasting.api.utils.getList
import at.petrak.hexcasting.api.utils.hasList
import at.petrak.hexcasting.api.utils.serializeToNBT
Expand All @@ -22,20 +23,30 @@ import net.minecraft.server.level.ServerLevel
* @property data list of *remaining* datums to ForEach over
* @property code code to run per datum
* @property baseStack the stack state at Thoth entry
* @property acc concatenated list of final stack states after Thoth exit
* @property immutableAcc concatenated list of final stack states after Thoth exit
*/
data class FrameForEach(
val data: SpellList,
val code: SpellList,
val baseStack: List<Iota>?,
val acc: MutableList<Iota>
val immutableAcc: TreeList<Iota>
) : ContinuationFrame {

@Deprecated("use the primary constructor that accepts a TreeList instead")
constructor(
data: SpellList,
code: SpellList,
baseStack: List<Iota>?,
acc: MutableList<Iota>
) : this(data, code, baseStack, TreeList.from(acc))

@Deprecated("access immutableAcc instead")
val acc: MutableList<Iota> get() = immutableAcc.toMutableList()

/** 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(immutableAcc.appendedAll(stack)))
return true to newStack
}

Expand All @@ -46,27 +57,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) {
// init stack to the VM stack...
harness.image.stack.toList()
val (stack, newAcc) = if (baseStack == null) {
// init stack to the harness stack...
harness.image.stack.toList() to immutableAcc
} else {
// else save the stack to the accumulator and reuse the saved base stack.
acc.addAll(harness.image.stack)
baseStack
baseStack to immutableAcc.appendedAll(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, newAcc))
// 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(immutableAcc), harness.image, continuation)
}
val tStack = stack.toMutableList()
tStack.add(stackTop)
Expand All @@ -86,10 +96,10 @@ data class FrameForEach(
"code" %= code.serializeToNBT()
if (baseStack != null)
"base" %= baseStack.serializeToNBT()
"accumulator" %= acc.serializeToNBT()
"accumulator" %= immutableAcc.serializeToNBT()
}

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.petrak.hexcasting.api.casting.SpellList;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.api.utils.TreeList;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
import at.petrak.hexcasting.api.mod.HexConfig;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -37,6 +38,10 @@ public ListIota(@NotNull SpellList list) {
size = totalSize;
}

public ListIota(@NotNull TreeList<Iota> list) {
this(new SpellList.LList(list));
}

public ListIota(@NotNull List<Iota> list) {
this(new SpellList.LList(list));
}
Expand Down
Loading
Loading