read() function to get string input, vm.readKey() to read a key

This commit is contained in:
minjaesong
2020-05-15 12:43:33 +09:00
parent 73ae5ec252
commit e486d8dce4
5 changed files with 36 additions and 6 deletions

View File

@@ -267,7 +267,8 @@ println("");
print("C:\\\\>");
while (true) {
print(String.fromCharCode(vm.readKey()));
var s = read();
println("String read: " + s + "@");
}
""".trimIndent()

View File

@@ -21,8 +21,32 @@ class VMJSR223Delegate(val vm: VM) {
//println("[Nashorn] $s")
vm.printStream.write((s + '\n').toByteArray())
}
fun println() = print('\n')
fun readKey() = vm.inputStream.read()
/**
* Read series of key inputs until Enter/Return key is pressed
*/
fun read(): String {
val sb = StringBuilder()
var key: Int
do {
key = readKey()
if ((key == 8 && sb.isNotEmpty()) || key in 0x20..0x7E) {
this.print("${key.toChar()}")
}
when (key) {
8 -> if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)
in 0x20..0x7E -> sb.append(key.toChar())
}
} while (key != 13 && key != 10)
this.println()
return sb.toString()
}
}
class VMSerialDebugger(val vm: VM) {

View File

@@ -72,12 +72,15 @@ object VMRunnerFactory {
private val JS_INIT = """
function print(s) {
vm.print(s)
return vm.print(s)
}
function println(s) {
vm.println(s)
return vm.println(s)
}
"""
function read() {
return vm.read()
}
"""
}
fun toSingleLine(code: String) = code.replace(Regex("//[^\\n]*"), "").replace('\n', ' ')

View File

@@ -81,6 +81,8 @@ abstract class GlassTty(val TEXT_ROWS: Int, val TEXT_COLS: Int) {
ttyEscArguments.push(ttyEscArguments.pop() * 10 + (newnum.toInt() - 0x30))
}
//println("[tty] accepting char $char, state: $ttyEscState")
when (ttyEscState) {
TTY_ESC_STATE.INITIAL -> {
when (char) {

View File

@@ -362,8 +362,8 @@ class GraphicsAdapter(val vm: VM, val lcdMode: Boolean = false) : GlassTty(Compa
override fun backspace() {
val (x, y) = getCursorPos()
putChar(x, y, 0x20.toByte())
setCursorPos(x - 1, y)
putChar(x - 1, y, 0x20.toByte())
}
private lateinit var PRINTSTREAM_INSTANCE: OutputStream
@@ -427,7 +427,7 @@ class GraphicsAdapter(val vm: VM, val lcdMode: Boolean = false) : GlassTty(Compa
key = vm.getIO().mmio_read(37L)!!
} while (key == (-1).toByte())
println("[stdin] key = $key")
//println("[stdin] key = $key")
return key.toInt().and(255)
}
}