Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Kotlin bf2. #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 45 additions & 46 deletions brainfuck2/bf2.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
@file:JvmName("BfKotlin")

import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
import java.io.PrintStream

sealed class Op() {
class Inc(val v: Int): Op()
class Move(val v: Int): Op()
class Loop(val loop: Array<Op>): Op()
object Print: Op()
sealed class Op {
object Inc : Op()
object Dec : Op()
object MoveUp : Op()
object MoveDown : Op()
class Loop(val loop: Array<Op>) : Op()
class Print(val out: PrintStream) : Op()
}

class Tape {
private var tape: IntArray = IntArray(1)
class KtTape {
private var tape: IntArray = IntArray(16)
private var pos: Int = 0


val state: IntArray get() = tape.copyOf()

fun get(): Int {
return tape[pos]
}

fun inc(x: Int) {
tape[pos] += x
}

fun move(x: Int) {
pos += x
while (pos >= tape.size) {
val tape = IntArray(this.tape.size * 2)
System.arraycopy(this.tape, 0, tape, 0, this.tape.size)
this.tape.copyInto(tape)
this.tape = tape
}
}
}
class Program(code: String) {

class KtProgram(code: String, out: PrintStream) {
private val ops: Array<Op>
private val printer = Op.Print(out)

init {
val it = code.iterator()
Expand All @@ -43,54 +50,46 @@ class Program(code: String) {
val res = arrayListOf<Op>()
while (it.hasNext()) {
when (it.next()) {
'+' -> res.add(Op.Inc(1))
'-' -> res.add(Op.Inc(-1))
'>' -> res.add(Op.Move(1))
'<' -> res.add(Op.Move(-1))
'.' -> res.add(Op.Print)
'+' -> res.add(Op.Inc)
'-' -> res.add(Op.Dec)
'>' -> res.add(Op.MoveUp)
'<' -> res.add(Op.MoveDown)
'.' -> res.add(printer)
'[' -> res.add(Op.Loop(parse(it)))
']' -> return res.toTypedArray()
}
}
return res.toTypedArray()
}

fun run() {
_run(ops, Tape())
fun run(): KtTape {
val tape = KtTape()
_run(ops, tape)
return tape
}
private fun _run(program: Array<Op>, tape: Tape) {

private fun _run(program: Array<Op>, tape: KtTape) {
for (op in program) {
when (op) {
is Op.Inc -> tape.inc(op.v)
is Op.Move -> tape.move(op.v)
is Op.Loop -> while (tape.get() > 0) {
_run(op.loop, tape)
}
is Op.Print -> print(tape.get().toChar())
is Op.Inc -> tape.inc(1)
is Op.Dec -> tape.inc(-1)
is Op.MoveUp -> tape.move(1)
is Op.MoveDown -> tape.move(-1)
is Op.Loop -> while (tape.get() > 0) _run(op.loop, tape)
is Op.Print -> op.out.print(tape.get().toChar())
}
}
}
}

fun warming() {
val WARM_PROGRAM = ">++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++[>++++++++<-]>[-]<<>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++"

System.err.println("warming")
val start_time = System.currentTimeMillis()
Program(WARM_PROGRAM).run()
System.err.println("time: ${(System.currentTimeMillis() - start_time) / 1e3}s")
fun runWithTiming(runnable: () -> Any?) {
val startTime = System.currentTimeMillis()
runnable()
System.err.printf("time: %.3fs\n", (System.currentTimeMillis() - startTime) / 1e3)
}

@Throws(IOException::class)
fun main(args: Array<String>) {
val code = String(Files.readAllBytes(Paths.get(args[0])))

warming()

System.err.println("run")
val start_time = System.currentTimeMillis()
val program = Program(code)
program.run()
System.err.println("time: ${(System.currentTimeMillis() - start_time) / 1e3}s")
val code = File(args[0]).readText(Charsets.US_ASCII)
runWithTiming(KtProgram(code, System.out)::run)
}