Skip to content

Commit

Permalink
Implement subIotas for ContinuationIota
Browse files Browse the repository at this point in the history
  • Loading branch information
vgskye committed Nov 28, 2024
1 parent 73cc6f2 commit 6c5798b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface ContinuationFrame {
*/
fun size(): Int
fun depth(): Int
fun subIotas(): Iterable<Iota>?

val type: Type<*>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ data class FrameEvaluate(val list: SpellList, val isMetacasting: Boolean) : Cont

override fun size() = size
override fun depth() = depth
override fun subIotas(): Iterable<Iota> = list

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object FrameFinishEval : ContinuationFrame {

override fun size() = 0
override fun depth() = 0
override fun subIotas(): Iterable<Iota>? = null

@JvmField
val TYPE: ContinuationFrame.Type<FrameFinishEval> = object : ContinuationFrame.Type<FrameFinishEval> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ data class FrameForEach(

override fun size() = size
override fun depth() = depth
override fun subIotas(): Iterable<Iota> =
if (baseStack != null)
listOf(data, code, acc, baseStack).flatten()
else
listOf(data, code, acc).flatten()

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.petrak.hexcasting.api.casting.eval.vm

import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.utils.NBTBuilder
import at.petrak.hexcasting.api.utils.getList
import net.minecraft.nbt.CompoundTag
Expand All @@ -14,15 +15,28 @@ sealed interface SpellContinuation {
object Done : SpellContinuation {
override fun size(): Int = 0
override fun depth(): Int = 0
override fun subIotas(): Iterable<Iota>? = null
}

data class NotDone(val frame: ContinuationFrame, val next: SpellContinuation) : SpellContinuation {
override fun size(): Int = frame.size() + next.size()
override fun depth(): Int = max(frame.depth(), next.depth())
override fun subIotas(): Iterable<Iota> {
val list: MutableList<Iterable<Iota>> = mutableListOf()
var current: SpellContinuation = this
while (current is NotDone) {
val subIotas = current.frame.subIotas()
if (subIotas != null)
list.add(subIotas)
current = current.next
}
return list.flatten()
}
}

fun pushFrame(frame: ContinuationFrame): SpellContinuation = NotDone(frame, this)

fun subIotas(): Iterable<Iota>?
fun size(): Int
fun depth(): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

Expand Down Expand Up @@ -56,6 +57,11 @@ public boolean executable() {
return true;
}

@Override
public @Nullable Iterable<Iota> subIotas() {
return this.getContinuation().subIotas();
}

@Override
public int size() {
return Math.min(this.getContinuation().size(), 1);
Expand Down

0 comments on commit 6c5798b

Please sign in to comment.