Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,51 @@
// version 1.1.2
class Brainf__k(val prog: String, memSize: Int) {
private val mem = IntArray(memSize)
private var ip = 0
private var dp = 0
private val memVal get() = mem.getOrElse(dp) { 0 }
fun execute() {
while (ip < prog.length) {
when (prog[ip++]) {
'>' -> dp++
'<' -> dp--
'+' -> mem[dp] = memVal + 1
'-' -> mem[dp] = memVal - 1
',' -> mem[dp] = System.`in`.read()
'.' -> print(memVal.toChar())
'[' -> handleLoopStart()
']' -> handleLoopEnd()
}
}
}
private fun handleLoopStart() {
if (memVal != 0) return
var depth = 1
while (ip < prog.length) {
when (prog[ip++]) {
'[' -> depth++
']' -> if (--depth == 0) return
}
}
throw IllegalStateException("Could not find matching end bracket")
}
private fun handleLoopEnd() {
var depth = 0
while (ip >= 0) {
when (prog[--ip]) {
']' -> depth++
'[' -> if (--depth == 0) return
}
}
throw IllegalStateException("Could not find matching start bracket")
}
}
fun main(args: Array<String>) {
val prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
Brainf__k(prog, 10).execute()
}