basic interpreter wip

This commit is contained in:
minjaesong
2020-05-20 05:37:36 +09:00
parent a1e053c53e
commit 904c080731
10 changed files with 205 additions and 46 deletions

View File

@@ -47,6 +47,35 @@ class GraphicsJSR223Delegate(val vm: VM) {
}
}
fun getPixelDimension(): IntArray {
getFirstGPU()?.let { return intArrayOf(it.framebuffer.width, it.framebuffer.height) }
return intArrayOf(-1, -1)
}
fun getTermDimension(): IntArray {
getFirstGPU()?.let { return intArrayOf(it.TEXT_ROWS, it.TEXT_COLS) }
return intArrayOf(-1, -1)
}
fun getCursorYX(): IntArray {
getFirstGPU()?.let {
val (cx, cy) = it.getCursorPos()
return intArrayOf(cy + 1, cx + 1)
}
return intArrayOf(-1, -1)
}
/**
* prints a char as-is; won't interpret them as an escape sequence
*/
fun putSymbol(char: Byte) {
getFirstGPU()?.let {
val (cx, cy) = it.getCursorPos()
it.putChar(cx, cy, char)
it.setCursorPos(cx + 1, cy)
}
}
private fun GraphicsAdapter._loadbulk(fromAddr: Int, toAddr: Int, length: Int) {
UnsafeHelper.memcpy(
vm.usermem.ptr + fromAddr,

View File

@@ -54,7 +54,7 @@ import kotlin.random.Random
*/
class VM(
_memsize: Int
_memsize: Long
) {
val id = java.util.Random().nextInt()

View File

@@ -16,7 +16,7 @@ import java.io.StringReader
class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter() {
val vm = VM(8192)
val vm = VM(1024.kB())
lateinit var gpu: GraphicsAdapter
lateinit var batch: SpriteBatch
@@ -52,7 +52,8 @@ class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter()
// TEST PRG
//val fr = FileReader("./assets/tvdos/command.js")
val fr = FileReader("./assets/jscon.js")
//val fr = FileReader("./assets/jscon.js")
val fr = FileReader("./assets/tbas/basic.js")
val prg = fr.readText()
fr.close()

View File

@@ -50,11 +50,35 @@ class VMJSR223Delegate(val vm: VM) {
in 0x20..0x7E -> sb.append(key.toChar())
}
} while (key != 13 && key != 10)
this.println()
this.print("\n") // printout \n
inputStream.close()
return sb.toString()
}
/**
* Read series of key inputs until Enter/Return key is pressed. Backspace will work but any other non-printable
* characters (e.g. arrow keys) won't work.
*/
fun readNoEcho(): String {
val inputStream = vm.getInputStream()
val sb = StringBuilder()
var key: Int
do {
key = inputStream.read()
when (key) {
8 -> if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)
in 0x20..0x7E -> sb.append(key.toChar())
}
} while (key != 13 && key != 10)
this.println() // printout \n
inputStream.close()
return sb.toString()
}
}
class VMSerialDebugger(val vm: VM) {

View File

@@ -1,5 +1,6 @@
package net.torvald.tsvm
import java.io.FileInputStream
import java.io.FileReader
import javax.script.Compilable
import javax.script.ScriptContext
@@ -56,13 +57,16 @@ object VMRunnerFactory {
bind.put("serial", VMSerialDebugger(vm))
if (extension == "js") {
engine.eval(toSingleLine(JS_INIT), context)
val fr = FileReader("./assets/JS_INIT.js")
val prg = fr.readText()
fr.close()
engine.eval(toSingleLine(prg), context)
}
}
override suspend fun executeCommand(command: String) {
//(engine as Compilable).compile(command).eval(context) // compiling does not work with bindings in kts
engine.eval(command, context)
engine.eval("\"use strict\";{$command}", context)
}
}
}
@@ -70,20 +74,6 @@ object VMRunnerFactory {
}
}
private val JS_INIT = """
function print(s) {
vm.print(s);
}
function println(s) {
if (typeof s == "undefined")
vm.print("\n");
else
vm.println(s);
}
function read() {
return vm.read();
}
"""
}
fun toSingleLine(code: String) = code.replace(Regex("//[^\\n]*"), "").replace('\n', ' ')

View File

@@ -336,6 +336,18 @@ class GraphicsAdapter(val vm: VM, val lcdMode: Boolean = false) : GlassTty(Compa
}
override fun sgrTwoArg(arg1: Int, arg2: Int) {
/*
Color table for default palette
Black 240
Red 160
Green 30
Yellow 230
Blue 19
Magenta 199
Cyan 74
White 254
*/
TODO("Not yet implemented")
}
@@ -347,7 +359,7 @@ class GraphicsAdapter(val vm: VM, val lcdMode: Boolean = false) : GlassTty(Compa
* @param arg1 y-position (row)
* @param arg2 x-position (column) */
override fun cursorXY(arg1: Int, arg2: Int) {
setCursorPos(arg2, arg1)
setCursorPos(arg2 - 1, arg1 - 1)
}
override fun ringBell() {