Refactor execution flow

This commit is contained in:
Shadowfacts 2019-08-11 18:53:02 -04:00
parent 233a83f368
commit 2ffda7b4e3
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 21 additions and 14 deletions

View File

@ -7,7 +7,7 @@ import net.minecraft.util.Identifier
*/ */
abstract class ExecutableBlock(identifier: Identifier): ProgramBlock(identifier) { abstract class ExecutableBlock(identifier: Identifier): ProgramBlock(identifier) {
open val executionFlow = ExecutionFlow(this) abstract val executionFlow: ExecutionFlow
abstract fun execute() abstract fun execute()

View File

@ -3,14 +3,20 @@ package net.shadowfacts.asmr.program
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
open class ExecutionFlow(val block: ExecutableBlock) { interface ExecutionFlow {
val block: ExecutableBlock
open var next: ExecutableBlock? = null val next: ExecutableBlock?
open var prev: ExecutableBlock? = null val prev: ExecutableBlock?
}
class DirectExecutionFlow(override val block: ExecutableBlock): ExecutionFlow {
override var next: ExecutableBlock? = null
override var prev: ExecutableBlock? = null
fun link(next: ExecutableBlock) { fun link(next: ExecutableBlock) {
this.next = next this.next = next
next.executionFlow.prev = block (next.executionFlow as? DirectExecutionFlow)?.let {
it.prev = block
}
} }
} }

View File

@ -19,6 +19,8 @@ class PrintBlock<Type: Any>(
override val inputs: Array<ProgramBlockInput<*>> = arrayOf(input) override val inputs: Array<ProgramBlockInput<*>> = arrayOf(input)
override val outputs: Array<ProgramBlockOutput<*>> = arrayOf() override val outputs: Array<ProgramBlockOutput<*>> = arrayOf()
override val executionFlow = DirectExecutionFlow(this)
override fun execute() { override fun execute() {
println(input.value) println(input.value)
} }

View File

@ -2,9 +2,7 @@ package net.shadowfacts.asmr.program.blocks
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.shadowfacts.asmr.ASMR import net.shadowfacts.asmr.ASMR
import net.shadowfacts.asmr.program.ExecutableBlock import net.shadowfacts.asmr.program.*
import net.shadowfacts.asmr.program.ProgramBlockInput
import net.shadowfacts.asmr.program.ProgramBlockOutput
/** /**
* @author shadowfacts * @author shadowfacts
@ -16,6 +14,8 @@ class StartBlock: ExecutableBlock(
override val inputs: Array<ProgramBlockInput<*>> = arrayOf() override val inputs: Array<ProgramBlockInput<*>> = arrayOf()
override val outputs: Array<ProgramBlockOutput<*>> = arrayOf() override val outputs: Array<ProgramBlockOutput<*>> = arrayOf()
override val executionFlow = DirectExecutionFlow(this)
override fun execute() { override fun execute() {
} }

View File

@ -2,10 +2,7 @@ package net.shadowfacts.asmr.program.blocks.math
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.shadowfacts.asmr.ASMR import net.shadowfacts.asmr.ASMR
import net.shadowfacts.asmr.program.ExecutableBlock import net.shadowfacts.asmr.program.*
import net.shadowfacts.asmr.program.ProgramBlockInput
import net.shadowfacts.asmr.program.ProgramBlockOutput
import net.shadowfacts.asmr.program.ProgramType
import java.lang.RuntimeException import java.lang.RuntimeException
/** /**
@ -32,6 +29,8 @@ class BinaryOperatorBlock<Type: Any>(
override val inputs: Array<ProgramBlockInput<*>> = arrayOf(left, right) override val inputs: Array<ProgramBlockInput<*>> = arrayOf(left, right)
override val outputs: Array<ProgramBlockOutput<*>> = arrayOf(output) override val outputs: Array<ProgramBlockOutput<*>> = arrayOf(output)
override val executionFlow = DirectExecutionFlow(this)
override fun execute() { override fun execute() {
if (type == ProgramType.INT) { if (type == ProgramType.INT) {
val left = left.value as Int val left = left.value as Int