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 @@ -290,15 +290,17 @@ fun List<Iota>.getLongOrList(idx: Int, argc: Int = 0): Either<Long, TreeList<Iot
)
}

fun evaluatable(datum: Iota, reverseIdx: Int): Either<Iota, TreeList<Iota>> =
when (datum) {
fun List<Iota>.getEvaluatable(idx: Int, argc: Int = 0): Either<Iota, TreeList<Iota>> {
val datum = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
return when (datum) {
is ListIota -> Either.right(datum.list)
else -> if (datum.executable()) Either.left(datum) else throw MishapInvalidIota(
else -> if (datum.executable()) Either.left(datum) else throw MishapInvalidIota.of(
datum,
reverseIdx,
"hexcasting.mishap.invalid_value.evaluatable".asTranslatedComponent
if (argc == 0) idx else argc - (idx + 1),
"evaluatable"
)
}
}

fun Iota?.orNull() = this ?: NullIota()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.FrameEvaluate
import at.petrak.hexcasting.api.casting.eval.vm.FrameFinishEval
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.evaluatable
import at.petrak.hexcasting.api.casting.getEvaluatable
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.api.utils.TreeList
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
import com.mojang.datafixers.util.Either

object OpEval : Action {
override fun operate(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation): OperationResult {
val stack = image.stack
val iota = stack.lastOrNull() ?: throw MishapNotEnoughArgs(1, 0)
return exec(env, image, continuation, stack.init(), iota)

if (stack.isEmpty())
throw MishapNotEnoughArgs(1, 0)

val instrs = stack.getEvaluatable(stack.lastIndex, stack.size)

return exec(env, image, continuation, stack.init(), instrs)
}

fun exec(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation, newStack: TreeList<Iota>, iota: Iota): OperationResult {
fun exec(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation, newStack: TreeList<Iota>, instrs: Either<Iota, TreeList<Iota>>): OperationResult {
// also, never make a break boundary when evaluating just one pattern
val instrs = evaluatable(iota, 0)
val newCont =
if (instrs.left().isPresent || (continuation is SpellContinuation.NotDone && continuation.frame is FrameFinishEval)) {
continuation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
import at.petrak.hexcasting.api.casting.eval.OperationResult
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.getEvaluatable
import at.petrak.hexcasting.api.casting.iota.ContinuationIota
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs

Expand All @@ -13,7 +14,13 @@ object OpEvalBreakable : Action {
image: CastingImage,
continuation: SpellContinuation): OperationResult {
val stack = image.stack
val iota = stack.lastOrNull() ?: throw MishapNotEnoughArgs(1, 0)
return OpEval.exec(env, image, continuation, stack.init().appended(ContinuationIota(continuation)), iota)

if (stack.isEmpty())
throw MishapNotEnoughArgs(1, 0)

val instrs = stack.getEvaluatable(stack.lastIndex, stack.size)

val newStack = stack.init().appended(ContinuationIota(continuation))
return OpEval.exec(env, image, continuation, newStack, instrs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import at.petrak.hexcasting.api.casting.eval.OperationResult
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.FrameForEach
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.evaluatable
import at.petrak.hexcasting.api.casting.getEvaluatable
import at.petrak.hexcasting.api.casting.getList
import at.petrak.hexcasting.api.casting.math.HexPattern
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
Expand Down Expand Up @@ -39,7 +39,7 @@ class SpecialHandlerForEach(val n: Int) : SpecialHandler {
throw MishapNotEnoughArgs(2 + n, stack.size)

val datums = stack.getList(stack.lastIndex - 1, stack.size)
val instrs = evaluatable(stack[stack.lastIndex], 0)
val instrs = stack.getEvaluatable(stack.lastIndex, stack.size)
stack = stack.dropRight(2)

val instrList = instrs.map({ TreeList.from(listOf(it)) }, { it })
Expand Down
Loading