From 0f1485f30b6f13a887ed12226ebd87660df54326 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 18 Aug 2019 16:39:08 +0200 Subject: [PATCH] added sorted, sgn, reverse to the AstVm --- compiler/res/version.txt | 2 +- compiler/src/prog8/CompilerMain.kt | 7 +++- compiler/src/prog8/vm/astvm/AstVm.kt | 18 ++++++++ compiler/src/prog8/vm/astvm/Expressions.kt | 9 +++- docs/source/building.rst | 16 +++---- examples/test.p8 | 49 ++++++++++++++++------ 6 files changed, 72 insertions(+), 29 deletions(-) diff --git a/compiler/res/version.txt b/compiler/res/version.txt index 491cd04e5..752a72c42 100644 --- a/compiler/res/version.txt +++ b/compiler/res/version.txt @@ -1,2 +1,2 @@ -1.60 +1.61 diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 49fadc35e..6a9beef3e 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -45,8 +45,11 @@ private fun compileMain(args: Array) { 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) diff --git a/compiler/src/prog8/vm/astvm/AstVm.kt b/compiler/src/prog8/vm/astvm/AstVm.kt index 7075412f7..7143a3f3d 100644 --- a/compiler/src/prog8/vm/astvm/AstVm.kt +++ b/compiler/src/prog8/vm/astvm/AstVm.kt @@ -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") } } diff --git a/compiler/src/prog8/vm/astvm/Expressions.kt b/compiler/src/prog8/vm/astvm/Expressions.kt index 7de4c5de1..14849e6eb 100644 --- a/compiler/src/prog8/vm/astvm/Expressions.kt +++ b/compiler/src/prog8/vm/astvm/Expressions.kt @@ -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) diff --git a/docs/source/building.rst b/docs/source/building.rst index 40dd5b28b..d1de6909f 100644 --- a/docs/source/building.rst +++ b/docs/source/building.rst @@ -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 - 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. diff --git a/examples/test.p8 b/examples/test.p8 index dcf6e7a61..150956125 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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') } + }