mirror of
https://github.com/irmen/prog8.git
synced 2024-11-26 11:49:22 +00:00
astvm eval of 'when'
This commit is contained in:
parent
f4f113da7b
commit
158fe7596b
@ -63,7 +63,8 @@ private fun compileMain(args: Array<String>) {
|
||||
|
||||
val filepath = Paths.get(moduleFile).normalize()
|
||||
|
||||
val (programAst, programName) = compileProgram(filepath, optimize, optimizeInlining, writeVmCode, writeAssembly)
|
||||
val (programAst, programName) = compileProgram(filepath, optimize, optimizeInlining,
|
||||
!launchAstVm, writeVmCode, writeAssembly)
|
||||
|
||||
if(launchAstVm) {
|
||||
println("\nLaunching AST-based vm...")
|
||||
|
@ -24,7 +24,8 @@ import kotlin.system.measureTimeMillis
|
||||
|
||||
fun compileProgram(filepath: Path,
|
||||
optimize: Boolean, optimizeInlining: Boolean,
|
||||
writeVmCode: Boolean, writeAssembly: Boolean): Pair<Program, String?> {
|
||||
generateVmCode: Boolean, writeVmCode: Boolean,
|
||||
writeAssembly: Boolean): Pair<Program, String?> {
|
||||
lateinit var programAst: Program
|
||||
var programName: String? = null
|
||||
|
||||
@ -89,6 +90,7 @@ fun compileProgram(filepath: Path,
|
||||
// printAst(programAst)
|
||||
// namespace.debugPrint()
|
||||
|
||||
if(generateVmCode) {
|
||||
// compile the syntax tree into stackvmProg form, and optimize that
|
||||
val compiler = Compiler(programAst)
|
||||
val intermediate = compiler.compile(compilerOptions)
|
||||
@ -111,6 +113,7 @@ fun compileProgram(filepath: Path,
|
||||
programName = assembly.name
|
||||
}
|
||||
}
|
||||
}
|
||||
println("\nTotal compilation+assemble time: ${totalTime / 1000.0} sec.")
|
||||
|
||||
} catch (px: ParsingFailedError) {
|
||||
|
@ -442,6 +442,23 @@ class AstVm(val program: Program) {
|
||||
}
|
||||
} while (!condition.asBoolean)
|
||||
}
|
||||
is WhenStatement -> {
|
||||
val condition=evaluate(stmt.condition, evalCtx)
|
||||
for(choice in stmt.choices) {
|
||||
if(choice.value==null) {
|
||||
// the 'else' choice
|
||||
executeAnonymousScope(choice.statements)
|
||||
break
|
||||
} else {
|
||||
val value = choice.value.constValue(evalCtx.program) ?: throw VmExecutionException("can only use const values in when choices ${choice.position}")
|
||||
val rtval = RuntimeValue.from(value, evalCtx.program.heap)
|
||||
if(condition==rtval) {
|
||||
executeAnonymousScope(choice.statements)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
TODO("implement $stmt")
|
||||
}
|
||||
|
@ -16,8 +16,8 @@
|
||||
4 -> {
|
||||
Y=7
|
||||
}
|
||||
5 -> Y=5
|
||||
else -> A=99
|
||||
5 -> Y=5 ; @todo error; else already seen
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user