Skip to content

Commit

Permalink
fix: SLEEP should be interrupted by kbd event
Browse files Browse the repository at this point in the history
fixes #75
  • Loading branch information
ianatha committed Nov 11, 2023
1 parent b889d91 commit cfe2522
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class AndroidSystemInAndOut(private val context: Activity) : BBUIFile {
return result.toString()
}

override fun peekHasChar(): Boolean {
return bufIn.available > 0
}

private fun takeInputCharBlocking(): Char {
val mask1Byte = 0b10000000
val mask2Byte = 0b11000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class SystemInputOutputFile(
return readLine()
}

override fun peekHasChar(): Boolean {
return `in`.ready()
}

override fun takeInputChar(): Char? {
return null
}
Expand Down
1 change: 1 addition & 0 deletions libbababasic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
dependencies {
antlr("org.antlr:antlr4:4.13.1")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.antlr:antlr4-runtime:4.13.1")
implementation("com.google.guava:guava:29.0-jre")
implementation("org.apache.commons:commons-csv:1.7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.atha.libbababasic.file

interface BBUIFile : BBFile {
fun inputDialog(prompt: String): String?
fun peekHasChar(): Boolean
fun takeInputChar(): Char?
fun outputText(text: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class SystemInputOutputFile(
return readLine()
}

override fun peekHasChar(): Boolean {
return `in`.ready()
}

override fun takeInputChar(): Char? {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class BBRuntime(
OpCode.RESTORE -> readData!!.restore()
OpCode.READ -> read(readData!!, ir.symbolTable, instruction)
OpCode.ENVIRONDLR -> environdlr(env, ir.symbolTable, instruction)
OpCode.SLEEP -> sleep(ir.symbolTable, instruction)
OpCode.SLEEP -> sleep(files!!, ir.symbolTable, instruction)
OpCode.SCREEN -> {
if (params!!.size != 3) {
throw InternalError("Expected 1 param, but found: $params")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import io.atha.libbababasic.file.BBUIFile
import io.atha.libbababasic.parser.IR
import io.atha.libbababasic.runtime.Formatter.FormatterCache
import io.atha.libbababasic.runtime.Types.assertBothStringOrNumeric
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord
Expand All @@ -27,21 +30,50 @@ import java.util.Random
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.LockSupport
import kotlin.math.roundToLong
import kotlinx.coroutines.selects.select

object Statements {
@JvmStatic
fun sleep(
files: BBFiles,
symbolTable: SymbolTable,
instruction: IR.Instruction
) {
val channel = kotlinx.coroutines.channels.Channel<Unit>()
val sleepArg = symbolTable[instruction.op1]!!.value!!.float64
if (sleepArg < 0) {
throw RuntimeError(
RuntimeError.ErrorCode.DATA_OUT_OF_RANGE,
"Sleep time seconds cannot be less than 0."
)
}
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos((sleepArg * 1000L).roundToLong()))

val readJob = GlobalScope.launch {
while (true) {
if (files.sys.peekHasChar()) {
channel.close()
break
}
}
}

val timerJob = GlobalScope.launch {
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos((sleepArg * 1000L).roundToLong()))
channel.close()
}

return try {
runBlocking {
select<Unit> {
channel.onReceiveCatching {
it
}
}
}
} finally {
readJob.cancel()
timerJob.cancel()
}
}

@JvmStatic
Expand Down

0 comments on commit cfe2522

Please sign in to comment.