added sorted, sgn, reverse to the AstVm

This commit is contained in:
Irmen de Jong 2019-08-18 16:39:08 +02:00
parent eb94c678bd
commit 0f1485f30b
6 changed files with 72 additions and 29 deletions

View File

@ -1,2 +1,2 @@
1.60
1.61

View File

@ -45,8 +45,11 @@ private fun compileMain(args: Array<String>) {
writeAssembly = false
else if(arg=="-noopt")
optimize = false
else if(arg=="-avm")
else if(arg=="-sim") {
launchAstVm = true
writeAssembly = false
emulatorToStart = ""
}
else if(arg=="-watch")
watchMode = true
else if(!arg.startsWith("-"))
@ -130,7 +133,7 @@ private fun usage() {
System.err.println(" [-noopt] don't perform any optimizations")
System.err.println(" [-emu] auto-start the 'x64' C-64 emulator after successful compilation")
System.err.println(" [-emu2] auto-start the 'x64sc' C-64 emulator after successful compilation")
System.err.println(" [-avm] launch the prog8 ast-based virtual machine after compilation")
System.err.println(" [-sim] launch the prog8 virtual machine/simulator after compilation")
System.err.println(" [-watch] continuous compilation mode (watches for file changes)")
System.err.println(" modulefile main module file to compile")
exitProcess(1)

View File

@ -955,6 +955,24 @@ class AstVm(val program: Program) {
runtimeVariables.set(program.namespace, Register.Y.name, registerYsave.pop())
null
}
"sort" -> {
val array=args.single()
array.array!!.sort()
null
}
"reverse" -> {
val array=args.single()
array.array!!.reverse()
null
}
"sgn" -> {
val value = args.single().numericValue().toDouble()
when {
value<0.0 -> RuntimeValue(DataType.BYTE, -1)
value==0.0 -> RuntimeValue(DataType.BYTE, 0)
else -> RuntimeValue(DataType.BYTE, 1)
}
}
else -> TODO("builtin function $name")
}
}

View File

@ -79,8 +79,13 @@ fun evaluate(expr: Expression, ctx: EvalContext): RuntimeValue {
is ArrayIndexedExpression -> {
val array = evaluate(expr.identifier, ctx)
val index = evaluate(expr.arrayspec.index, ctx)
val value = array.array!![index.integerValue()]
return RuntimeValue(ArrayElementTypes.getValue(array.type), value)
return if(array.array!=null) {
val value = array.array!![index.integerValue()]
RuntimeValue(ArrayElementTypes.getValue(array.type), value)
} else {
val value = array.str!![index.integerValue()]
RuntimeValue(ArrayElementTypes.getValue(array.type), value.toShort())
}
}
is TypecastExpression -> {
return evaluate(expr.expression, ctx).cast(expr.type)

View File

@ -163,22 +163,16 @@ or::
Virtual Machine
---------------
Virtual Machine / Simulator
---------------------------
You may have noticed the ``-avm`` and ``-vm`` command line options for the compiler:
You may have noticed the ``-sim`` command line option for the compiler:
-avm
Launches the "AST virtual machine" that directly executes the parsed program.
-sim
Launches the "AST virtual machine Simulator" that directly executes the parsed program.
No compilation steps will be performed.
Allows for very fast testing and debugging before actually compiling programs
to machine code.
It simulates a bare minimum of features from the target platform, so most stuff
that calls ROM routines or writes into hardware registers won't work. But basic
system routines are emulated.
-vm <vm bytecode file>
Launches the "intermediate code VM"
it interprets the intermediate code that the compiler can write when using the ``-writevm``
option. This is the code that will be fed to the assembly code generator,
so you'll skip that last step.

View File

@ -1,23 +1,46 @@
%import c64lib
%import c64utils
%import c64flt
%zeropage basicsafe
main {
sub start() {
const ubyte i = 33
i=33
i++
const ubyte q=33
for q in [1,3,5,99] {
A=i
}
while q<10 {
q++
}
byte bb
word ww
float fl
bb=-1
c64scr.print_b(sgn(bb))
c64.CHROUT('\n')
bb=0
c64scr.print_b(sgn(bb))
c64.CHROUT('\n')
bb=1
c64scr.print_b(sgn(bb))
c64.CHROUT('\n')
c64.CHROUT('\n')
ww=-1
c64scr.print_b(sgn(ww))
c64.CHROUT('\n')
ww=0
c64scr.print_b(sgn(ww))
c64.CHROUT('\n')
ww=1
c64scr.print_b(sgn(ww))
c64.CHROUT('\n')
c64.CHROUT('\n')
fl=-1.1
c64scr.print_b(sgn(fl))
c64.CHROUT('\n')
fl=0.0
c64scr.print_b(sgn(fl))
c64.CHROUT('\n')
fl=1.0
c64scr.print_b(sgn(fl))
c64.CHROUT('\n')
c64.CHROUT('\n')
}
}