Skip to content

Commit

Permalink
Merge pull request #12 from TarCV/update
Browse files Browse the repository at this point in the history
Support writing to a file
  • Loading branch information
TarCV authored May 5, 2020
2 parents 067a7ee + 88420b4 commit bffd66d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import kotlin.math.max

@ExperimentalUnsignedTypes
class Decompiler {
fun print() {

fun print(printer: Printer) {
if (!alreadyParsed) {
throw IllegalStateException("print() can be called only after parsing")
}

println("#!botc 1.0.0")
println("#include \"debotc_defs.bts\"")
println()
printer.println("#!botc 1.0.0")
printer.println("#include \"debotc_defs.bts\"")
printer.println()

val globalVariables = HashSet<Int>()
val globalArrays = HashSet<Int>()
Expand Down Expand Up @@ -57,12 +58,12 @@ class Decompiler {
stateBuilder.appendLine()
}
.let {
printBlockForGlobal(globalVariables, globalArrays)
println()
printBlockForGlobal(printer, globalVariables, globalArrays)
printer.println()
it
}
.forEach {
println(it)
printer.println(it.toString())
}
}

Expand All @@ -76,16 +77,16 @@ class Decompiler {
.appendLine()
}

private fun printBlockForGlobal(globalVariables: Set<Int>, globalArrays: Set<Int>) {
private fun printBlockForGlobal(printer: Printer, globalVariables: Set<Int>, globalArrays: Set<Int>) {
globalVariables
.sorted()
.forEach {
println("var int \$global$it;")
printer.println("var int \$global$it;")
}
globalArrays
.sorted()
.forEach {
println("var int \$globalArray$it[];")
printer.println("var int \$globalArray$it[];")
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/commonMain/kotlin/com/github/tarcv/zandronum/debotc/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ expect fun StringBuilder.appendLine(line: StringBuilder): StringBuilder

expect fun StringBuilder.insertString(index: Int, string: String): StringBuilder

expect fun ByteArray.stringFromUtf8BytesOrThrow(start: Int = 0, size: Int = this.size): String
expect fun ByteArray.stringFromUtf8BytesOrThrow(start: Int = 0, size: Int = this.size): String

interface Printer {
fun println(msg: String = "")

fun close()
fun print(msg: String)
}
expect class FilePrinter(path: String): Printer
expect object consolePrinter: Printer
16 changes: 13 additions & 3 deletions src/jvmMain/kotlin/com/github/tarcv/zandronum/debotc/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import java.nio.file.Paths

@ExperimentalUnsignedTypes
fun main(args: Array<String>) {
if (args.size != 1) {
throw IllegalArgumentException("Only one argument - path to compiled script should be passed")
if (args.size != 1 && args.size != 2) {
throw IllegalArgumentException("Wrong arguments${System.lineSeparator()}debotc <script.o> [<script.botc>]")
}

val data = Files.readAllBytes(Paths.get(args[0]))
val data0 = data.toUByteArray()

val decompiler = Decompiler()
decompiler.parse(data0)
decompiler.print()

val printer = if (args.size == 2) {
FilePrinter(args[1])
} else {
consolePrinter
}
try {
decompiler.print(printer)
} finally {
printer.close()
}
}
31 changes: 31 additions & 0 deletions src/jvmMain/kotlin/com/github/tarcv/zandronum/debotc/UtilsJvm.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.tarcv.zandronum.debotc

import java.io.File
import java.nio.charset.StandardCharsets

/**
Expand Down Expand Up @@ -29,4 +30,34 @@ actual fun StringBuilder.insertString(index: Int, string: String): StringBuilder

actual fun assert(value: Boolean) {
kotlin.assert(value)
}

actual class FilePrinter actual constructor(path: String) : Printer {
private val writer = File(path).printWriter()

override fun close() {
writer.close()
}

override fun print(msg: String) {
writer.print(msg)
}

override fun println(msg: String) {
writer.println(msg)
}
}

actual object consolePrinter: Printer {
override fun close() {
// no op
}

override fun print(msg: String) {
kotlin.io.print(msg)
}

override fun println(msg: String) {
kotlin.io.println(msg)
}
}
24 changes: 19 additions & 5 deletions src/nativeMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import kotlinx.cinterop.*
import platform.posix.*
import com.github.tarcv.zandronum.debotc.Decompiler
import com.github.tarcv.zandronum.debotc.Printer
import com.github.tarcv.zandronum.debotc.FilePrinter
import com.github.tarcv.zandronum.debotc.consolePrinter
import com.github.tarcv.zandronum.debotc.lineSeparator

fun main(args: Array<String>) {
if (args.size != 1) {
throw IllegalArgumentException("Only one argument - path to compiled script should be passed")
if (args.size != 1 && args.size != 2) {
throw IllegalArgumentException("Wrong arguments${lineSeparator}debotc <script.o> [<script.botc>]")
}
var data0 = UByteArray(0)

Expand All @@ -25,7 +29,17 @@ fun main(args: Array<String>) {
fclose(file)
}

val decompiler = Decompiler()
decompiler.parse(data0)
decompiler.print()
val printer: Printer = if (args.size == 2) {
FilePrinter(args[1])
} else {
consolePrinter
}

try {
val decompiler = Decompiler()
decompiler.parse(data0)
decompiler.print(printer)
} finally {
printer.close()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.tarcv.zandronum.debotc

import platform.posix.*
import kotlinx.cinterop.*

/**
* Native implementations of platform-dependent helpers
*/
Expand Down Expand Up @@ -27,4 +30,35 @@ actual val lineSeparator: String = "\n" // TODO: get system line separator

actual fun assert(value: Boolean) {
kotlin.assert(value)
}

actual class FilePrinter actual constructor(private val path: String) : Printer {
private val file = fopen(path, "a")

override fun close() {
fclose(file)
}

override fun print(msg: String) {
fputs(msg, file)
}

override fun println(msg: String) {
fputs(msg, file)
fputs(lineSeparator, file)
}
}

actual object consolePrinter: Printer {
override fun close() {
// no op
}

override fun print(msg: String) {
kotlin.io.print(msg)
}

override fun println(msg: String) {
kotlin.io.println(msg)
}
}

0 comments on commit bffd66d

Please sign in to comment.