add light docs describing the befunge interface

This commit is contained in:
2018-11-04 23:56:47 -05:00
parent b25400db3c
commit c352d921b0
6 changed files with 69 additions and 5 deletions

View File

@@ -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<Vec> {
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<Value>) {
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<Value> {
return nextVecs(vec, delta).take(count).map(this::get).asIterable()
}

View File

@@ -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<Value>
@@ -16,7 +23,14 @@ interface Interpreter {
val stackChanged: Event<StackEvent>
val ipChanged: Event<IpEvent>
/**
* @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()
}

View File

@@ -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

View File

@@ -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<EventType> = Consumer<EventType>
internal operator fun <T> Handler<T>.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<String>) {
* val event = Event<ServerEvent>()
* 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<T> : Iterable<MutableMap.MutableEntry<String, Handler<T>>> {
private val list = LinkedHashMap<String, Handler<T>>()

View File

@@ -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)
data class FungeChange(val vec: Vec, val from: Value, val to: Value)
data class FungeEvent(val changes: List<FungeChange>)

View File

@@ -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<String>) {
launch<SampleApp>(args)
}