From 80c69d83c601c241e706f2158343555c4511fb1b Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 6 Dec 2018 01:25:06 +0100 Subject: [PATCH] random number stuff --- compiler/examples/test.p8 | 47 +++++++++++++++++-- .../src/prog8/ast/AstIdentifiersChecker.kt | 2 +- .../src/prog8/compiler/target/c64/AsmGen.kt | 2 +- compiler/src/prog8/stackvm/Program.kt | 4 +- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index 2beaa8756..c2e40e219 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -1,11 +1,50 @@ -%output raw -%launcher none %import c64utils +%import mathlib +%option enable_floats ~ main { - memory ubyte[40*25] Screen = $0400 sub start() { - A, Y = c64flt.GETADRAY() + + ;math.randseed($5566) ; if you want the exact same number sequence every restart of the program + ;math.randseedr() + + c64flt.print_float_ln(rndf()) + c64flt.print_float_ln(rndf()) + c64flt.print_float_ln(rndf()) + return + + + ubyte loop + c64scr.print_string("random bytes:\n") + for loop in 1 to 5 { + rsave() ; @todo automatic based on clobbers declaration + c64scr.print_byte_decimal(loop) + c64.CHROUT(':') + c64.CHROUT(' ') + ubyte ubr = rnd() + c64scr.print_byte_decimal(ubr) + rrestore() + c64.CHROUT('\n') + } + c64scr.print_string("\nrandom words:\n") + for loop in 1 to 5 { + rsave() ; @todo automatic based on clobbers declaration + c64scr.print_byte_decimal(loop) + c64.CHROUT(':') + c64.CHROUT(' ') + uword uwr = rndw() + c64scr.print_word_decimal(uwr) + rrestore() + c64.CHROUT('\n') + } + c64scr.print_string("\nrandom floats:\n") + for loop in 1 to 5 { + c64scr.print_byte_decimal(loop) + c64.CHROUT(':') + c64.CHROUT(' ') + float f = rndf() + c64flt.print_float_ln(f) + } } } diff --git a/compiler/src/prog8/ast/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/AstIdentifiersChecker.kt index 85021f3e9..2528ee6e6 100644 --- a/compiler/src/prog8/ast/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/AstIdentifiersChecker.kt @@ -161,7 +161,7 @@ class AstIdentifiersChecker(val heap: HeapValues) : IAstProcessor { } } null -> {} - else -> checkResult.add(SyntaxError("loop variables can only be a byte or word", forLoop.position)) + else -> checkResult.add(SyntaxError("loop variables can only be an unsigned byte or unsigned word", forLoop.position)) // TODO loops over signed values } } return super.process(forLoop) diff --git a/compiler/src/prog8/compiler/target/c64/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/AsmGen.kt index 523efb36b..b7a30b34d 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmGen.kt @@ -561,7 +561,7 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, " inx | lda ${ESTACK_LO.toHex()},x | ldy ${ESTACK_HI.toHex()},x | sta ${ins.callLabel} | sty ${ins.callLabel}+1" } Opcode.POP_VAR_FLOAT -> { - " lda #<${ins.callLabel} | ldy #>${ins.callLabel} | jsr prog8_lib.pop_var_float" + " lda #<${ins.callLabel} | ldy #>${ins.callLabel} | jsr prog8_lib.pop_float" } Opcode.INC_VAR_UB, Opcode.INC_VAR_B -> { diff --git a/compiler/src/prog8/stackvm/Program.kt b/compiler/src/prog8/stackvm/Program.kt index 311843dcf..3d13bacc8 100644 --- a/compiler/src/prog8/stackvm/Program.kt +++ b/compiler/src/prog8/stackvm/Program.kt @@ -1,6 +1,8 @@ package prog8.stackvm import prog8.ast.DataType +import prog8.ast.Position +import prog8.ast.unescape import prog8.compiler.HeapValues import prog8.compiler.intermediate.* import java.io.File @@ -89,7 +91,7 @@ class Program (val name: String, DataType.STR, DataType.STR_P, DataType.STR_S, - DataType.STR_PS -> heap.add(it.second, it.third.substring(1, it.third.length-1)) + DataType.STR_PS -> heap.add(it.second, unescape(it.third.substring(1, it.third.length-1), Position("", 0, 0, 0))) DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W -> { val numbers = it.third.substring(1, it.third.length-1).split(',')