rudimentary instruction set composition
This commit is contained in:
@@ -7,19 +7,19 @@ import befide.befunge.core.InstructionSet
|
||||
import befide.befunge.core.MutableInterpreter
|
||||
import befide.befunge.core.util.chooseOne
|
||||
|
||||
class Befunge93Instructions : InstructionSet<Vec2, LongData, PointerMode> {
|
||||
override fun MutableInterpreter<Vec2, LongData, PointerMode>.handle() {
|
||||
class B93Instructions : InstructionSet<Vec2, LongData, PointerMode> {
|
||||
override fun MutableInterpreter<Vec2, LongData, PointerMode>.handle(): Boolean {
|
||||
when (mode) {
|
||||
PointerMode.Terminated -> return
|
||||
PointerMode.Terminated -> return true
|
||||
PointerMode.String -> when (instr.char) {
|
||||
null -> Unit
|
||||
null -> return false
|
||||
'"' -> mode = PointerMode.Normal
|
||||
else -> push(instr)
|
||||
}
|
||||
PointerMode.Normal -> when (instr.char) {
|
||||
null -> Unit
|
||||
null -> return false
|
||||
|
||||
in "0123456789abcdef" -> push(LongData(instr.char.toString().toLong(16)))
|
||||
in "0123456789" -> push(LongData(instr.char.toString().toLong(16)))
|
||||
|
||||
'+' -> pop(2).let { (b, a) -> push(a + b) }
|
||||
'-' -> pop(2).let { (b, a) -> push(a - b) }
|
||||
@@ -61,7 +61,31 @@ class Befunge93Instructions : InstructionSet<Vec2, LongData, PointerMode> {
|
||||
}
|
||||
|
||||
'@' -> mode = PointerMode.Terminated
|
||||
|
||||
else -> return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
class B93Extras : InstructionSet<Vec2, LongData, PointerMode> {
|
||||
override fun MutableInterpreter<Vec2, LongData, PointerMode>.handle(): Boolean {
|
||||
when (mode) {
|
||||
PointerMode.Terminated -> return true
|
||||
PointerMode.String -> return false
|
||||
PointerMode.Normal -> when (instr.char) {
|
||||
null -> return false
|
||||
|
||||
in "0123456789abcdef" -> push(LongData(instr.char.toString().toLong(16)))
|
||||
|
||||
'\'' -> {
|
||||
move()
|
||||
push(instr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class Interpreter93(stdinSrc: PipedWriter, stdoutDest: PipedReader)
|
||||
override val stdin: PipedReader = PipedReader(stdinSrc)
|
||||
override val stdout: PipedWriter = PipedWriter(stdoutDest)
|
||||
|
||||
override val instructionSet = Befunge93Instructions()
|
||||
override val instructionSet = B93Instructions() + B93Extras()
|
||||
|
||||
override fun reset() {
|
||||
stdin.readAll()
|
||||
@@ -44,10 +44,12 @@ fun main(args: Array<String>) {
|
||||
println(it.to.pos)
|
||||
}
|
||||
|
||||
int.funge.src = """2>:3g" "-!v\ g30 <
|
||||
|!`"O":+1_:.:03p>03g+:"O"`|
|
||||
@ ^ p3\" ":<
|
||||
2 234567890123456789012345678901234567890123456789012345678901234567890123456789"""
|
||||
// int.funge.src = """2>:3g" "-!v\ g30 <
|
||||
// |!`"O":+1_:.:03p>03g+:"O"`|
|
||||
// @ ^ p3\" ":<
|
||||
//2 234567890123456789012345678901234567890123456789012345678901234567890123456789"""
|
||||
|
||||
int.funge.src = """048ce.....@"""
|
||||
|
||||
while (int.ip.mode != PointerMode.Terminated) {
|
||||
int.step()
|
||||
|
||||
@@ -3,7 +3,31 @@ package befide.befunge.core
|
||||
import befide.befunge.core.state.Data
|
||||
|
||||
interface InstructionSet<V, D : Data, M : Enum<M>> {
|
||||
fun MutableInterpreter<V, D, M>.handle()
|
||||
/**
|
||||
* @return whether this instruction set handled the instruction. If true, the step will be completed
|
||||
*/
|
||||
fun MutableInterpreter<V, D, M>.handle(): Boolean
|
||||
|
||||
fun step(inter: MutableInterpreter<V, D, M>) = inter.handle()
|
||||
fun step(inter: MutableInterpreter<V, D, M>): Boolean = inter.handle()
|
||||
|
||||
operator fun plus(o: InstructionSet<V, D, M>): MultiInstructionSet<V, D, M> =
|
||||
MultiInstructionSet(this, o)
|
||||
|
||||
operator fun plus(o: MultiInstructionSet<V, D, M>): MultiInstructionSet<V, D, M> =
|
||||
MultiInstructionSet(listOf(this) + o.sets)
|
||||
}
|
||||
|
||||
class MultiInstructionSet<V, D : Data, M : Enum<M>>
|
||||
(vararg val sets: InstructionSet<V, D, M>)
|
||||
: InstructionSet<V, D, M> {
|
||||
|
||||
constructor(sets: List<InstructionSet<V, D, M>>) : this(*sets.toTypedArray())
|
||||
|
||||
override fun MutableInterpreter<V, D, M>.handle(): Boolean {
|
||||
for (set in sets) if (set.step(this)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
override operator fun plus(o: InstructionSet<V, D, M>): MultiInstructionSet<V, D, M> =
|
||||
MultiInstructionSet(sets.toList() + listOf(o))
|
||||
}
|
||||
Reference in New Issue
Block a user