diff --git a/befunge/src/main/kotlin/befide/befunge/core/Funge.kt b/befunge/src/main/kotlin/befide/befunge/core/Funge.kt index f265d10..c8e3264 100644 --- a/befunge/src/main/kotlin/befide/befunge/core/Funge.kt +++ b/befunge/src/main/kotlin/befide/befunge/core/Funge.kt @@ -12,21 +12,36 @@ interface Funge { operator fun get(vec: Vec): Value operator fun set(vec: Vec, value: Value) + /** + * Get the next position to be executed starting at [vec] and stepping by [delta] + */ fun nextVec(vec: Vec, delta: Vec): Vec + /** + * Set the contents of the funge based on a string, handling newlines appropriately + */ fun setString(data: String) + /** + * @return An iterable of executable positions, starting at [vec] and stepping by [delta] + */ fun nextVecs(vec: Vec, delta: Vec): Iterable { return generateSequence(vec) { nextVec(it, delta) }.asIterable() } + /** + * Starting at [vec] and stepping by [delta], fill those positions with values in [data] + */ fun setMany(vec: Vec, delta: Vec, data: List) { for ((e, v) in data.zip(nextVecs(vec, delta))) set(v, e) } + /** + * @return An iterable of those values at the executable positions starting at [vec] and stepping by [delta]. + */ fun getMany(vec: Vec, delta: Vec, count: Int): Iterable { return nextVecs(vec, delta).take(count).map(this::get).asIterable() } diff --git a/befunge/src/main/kotlin/befide/befunge/core/Interpreter.kt b/befunge/src/main/kotlin/befide/befunge/core/Interpreter.kt index c4cf8d7..15e435d 100644 --- a/befunge/src/main/kotlin/befide/befunge/core/Interpreter.kt +++ b/befunge/src/main/kotlin/befide/befunge/core/Interpreter.kt @@ -7,6 +7,13 @@ import befide.befunge.events.StackEvent import befide.befunge.state.Value import java.util.* +/** + * Interface for a Befunge interpreter + * + * @property fungeChanged Invoked whenever the [funge] is modified + * @property stackChanged Invoked whenever the [stack] is modified + * @property ipChanged Invoked whenever the [ip] is modified + */ interface Interpreter { val funge: Funge val stack: Stack @@ -16,7 +23,14 @@ interface Interpreter { val stackChanged: Event val ipChanged: Event + /** + * @return If [ip] is inactive after this step, indicating execution has halted, then return `false` + */ fun step(): Boolean + + /** + * Reset the state of the interpreter to the last state before [step] was executed - that is, reset [funge] and [ip] to the state which was manually set, before any interpretation via [step] + */ fun reset() } diff --git a/befunge/src/main/kotlin/befide/befunge/core/Pointer.kt b/befunge/src/main/kotlin/befide/befunge/core/Pointer.kt index 654f80c..efb17a8 100644 --- a/befunge/src/main/kotlin/befide/befunge/core/Pointer.kt +++ b/befunge/src/main/kotlin/befide/befunge/core/Pointer.kt @@ -5,6 +5,11 @@ import befide.befunge.events.IpEvent import befide.befunge.state.IpMode import befide.befunge.state.Vec +/** + * Represents a Befunge Instruction Pointer + * + * Note that [Vec] need not be a concrete type, but is simply to be easier to implement 2d Befunge + */ interface Pointer { val pos: Vec val delta: Vec diff --git a/befunge/src/main/kotlin/befide/befunge/events/Event.kt b/befunge/src/main/kotlin/befide/befunge/events/Event.kt index 729b987..4e084f9 100644 --- a/befunge/src/main/kotlin/befide/befunge/events/Event.kt +++ b/befunge/src/main/kotlin/befide/befunge/events/Event.kt @@ -1,15 +1,38 @@ package befide.befunge.events -/** - * Source is pulled unmodified from https://github.com/notiocide/kotlin-events, under the MIT licence - */ - import java.util.function.Consumer typealias Handler = Consumer internal operator fun Handler.invoke(t: T) = accept(t) +/** + * Source is pulled unmodified from https://github.com/notiocide/kotlin-events, under the MIT licence + * + * Sample usage: + * + * ``` + * data class ServerEvent(val joined: Boolean, val user: String) + + * fun main(args: Array) { + * val event = Event() + * var userCount = 0 + * + * event += { (joined, user) -> println("$user ${if (joined) "joined" else "left"}") } + * event += { userCount += if (it.joined) 1 else -1 } + * + * event(ServerEvent(true, "Alice")) + * event(ServerEvent(true, "Bob")) + * event(ServerEvent(true, "Charles")) + * + * println("Users: $userCount") + * + * event(ServerEvent(false, "Bob")) + * + * println("Users: $userCount") + * } + * ``` + */ class Event : Iterable>> { private val list = LinkedHashMap>() diff --git a/befunge/src/main/kotlin/befide/befunge/events/FungeEvent.kt b/befunge/src/main/kotlin/befide/befunge/events/FungeEvent.kt index 8005103..86b45df 100644 --- a/befunge/src/main/kotlin/befide/befunge/events/FungeEvent.kt +++ b/befunge/src/main/kotlin/befide/befunge/events/FungeEvent.kt @@ -3,4 +3,6 @@ package befide.befunge.events import befide.befunge.state.Value import befide.befunge.state.Vec -data class FungeEvent(val vec: Vec, val from: Value, val to: Value) \ No newline at end of file +data class FungeChange(val vec: Vec, val from: Value, val to: Value) + +data class FungeEvent(val changes: List) \ No newline at end of file diff --git a/ide/src/main/kotlin/befide/ide/Sample.kt b/ide/src/main/kotlin/befide/ide/Sample.kt index bd5e68b..eb66c91 100644 --- a/ide/src/main/kotlin/befide/ide/Sample.kt +++ b/ide/src/main/kotlin/befide/ide/Sample.kt @@ -7,6 +7,7 @@ import javafx.scene.Parent import javafx.scene.control.Label import tornadofx.* + class SampleView : View() { private val controller: SampleController by inject() @@ -43,4 +44,8 @@ class SampleController : Controller() { class SampleApp : App() { override val primaryView = SampleView::class +} + +fun main(args: Array) { + launch(args) } \ No newline at end of file