moved floats.MIN/MAX to sys.MIN_FLOAT/MAX_FLOAT

added txt.print_f as alias to floats.print
This commit is contained in:
Irmen de Jong 2024-11-22 00:44:00 +01:00
parent 2360625927
commit 4958463e75
13 changed files with 107 additions and 23 deletions

View File

@ -37,7 +37,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if imported
asmsub reset_system() {

View File

@ -432,7 +432,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if imported
sub disable_runstop_and_charsetswitch() {

View File

@ -448,7 +448,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if imported
sub disable_runstop_and_charsetswitch() {

View File

@ -1388,7 +1388,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if imported
asmsub set_irq(uword handler @AY) clobbers(A) {

View File

@ -29,7 +29,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if importec
asmsub reset_system() {

View File

@ -112,7 +112,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if imported
asmsub reset_system() {

View File

@ -1,3 +1,17 @@
sys {
%option merge ; add some constants to sys
const float MAX_FLOAT = 1.7014118345e+38 ; bytes: 255,127,255,255,255
const float MIN_FLOAT = -1.7014118345e+38 ; bytes: 255,255,255,255,255
}
txt {
%option merge ; add function to txt
alias print_f = floats.print
}
floats {
; the floating point functions shared across compiler targets
%option merge, no_symbol_prefixing, ignore_unused
@ -7,9 +21,6 @@ floats {
const float TWOPI = 2*π
const float E = 2.718281828459045
const float EPSILON = 2.938735878e-39 ; bytes: 1,0,0,0,0
const float MAX = 1.7014118345e+38 ; bytes: 255,127,255,255,255
const float MIN = -1.7014118345e+38 ; bytes: 255,255,255,255,255
const ubyte SIZEOF = 5
asmsub print(float value @FAC1) clobbers(A,X,Y) {

View File

@ -2,6 +2,20 @@
%option enable_floats, ignore_unused
sys {
%option merge ; add some constants to sys
const float MAX_FLOAT = 1.7976931348623157e+308
const float MIN_FLOAT = -1.7976931348623157e+308
}
txt {
%option merge ; add function to txt
alias print_f = floats.print
}
floats {
const float π = 3.141592653589793
@ -9,9 +23,6 @@ floats {
const float TWOPI = 2*π
const float E = 2.718281828459045
const float EPSILON = 4.9E-324
const float MAX = 1.7976931348623157e+308
const float MIN = -1.7976931348623157e+308
const ubyte SIZEOF = 8
sub print(float value) {

View File

@ -21,7 +21,7 @@ sys {
const word MAX_WORD = 32767
const uword MIN_UWORD = 0
const uword MAX_UWORD = 65535
; MIN_FLOAT and MAX_FLOAT are defined in the floats module as MIN and MAX
; MIN_FLOAT and MAX_FLOAT are defined in the floats module if imported
sub reset_system() {

View File

@ -3,7 +3,6 @@ package prog8tests.ast
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.collections.shouldBeIn
import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
@ -15,11 +14,11 @@ import prog8.ast.Module
import prog8.ast.Program
import prog8.ast.statements.Block
import prog8.code.ast.PtBlock
import prog8.code.ast.PtBool
import prog8.code.core.Position
import prog8.code.core.SourceCode
import prog8.code.core.internedStringsModuleName
import prog8.code.target.C64Target
import prog8.code.target.Cx16Target
import prog8.code.target.VMTarget
import prog8tests.helpers.*
@ -200,6 +199,62 @@ txt {
errors.errors.size shouldBe 1
errors.errors[0] shouldContain "name conflict"
}
test("merge of float stuff into sys and txt - import order 1") {
val src="""
%import textio
%import floats
main {
sub start() {
txt.print_b(sys.MIN_BYTE)
txt.print_b(sys.MAX_BYTE)
txt.print_ub(sys.MIN_UBYTE)
txt.print_ub(sys.MAX_UBYTE)
txt.print_w(sys.MIN_WORD)
txt.print_w(sys.MAX_WORD)
txt.print_uw(sys.MIN_UWORD)
txt.print_uw(sys.MAX_UWORD)
txt.print_f(floats.EPSILON)
txt.print_f(sys.MIN_FLOAT)
txt.print_f(sys.MAX_FLOAT)
txt.print_f(floats.E)
txt.print_ub(sys.SIZEOF_FLOAT)
}
}"""
compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null
compileText(Cx16Target(), optimize=false, src, writeAssembly=false) shouldNotBe null
}
test("merge of float stuff into sys and txt - import order 2") {
val src="""
%import floats
%import textio
main {
sub start() {
txt.print_b(sys.MIN_BYTE)
txt.print_b(sys.MAX_BYTE)
txt.print_ub(sys.MIN_UBYTE)
txt.print_ub(sys.MAX_UBYTE)
txt.print_w(sys.MIN_WORD)
txt.print_w(sys.MAX_WORD)
txt.print_uw(sys.MIN_UWORD)
txt.print_uw(sys.MAX_UWORD)
txt.print_f(floats.EPSILON)
txt.print_f(sys.MIN_FLOAT)
txt.print_f(sys.MAX_FLOAT)
txt.print_f(floats.E)
txt.print_ub(sys.SIZEOF_FLOAT)
}
}"""
compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null
compileText(Cx16Target(), optimize=false, src, writeAssembly=false) shouldNotBe null
}
}
test("block sort order") {

View File

@ -6,6 +6,15 @@ const byte MAX_BYTE = 128 ; actual value ends up as -128
const word MAX_WORD = 32768 ; actual value ends up as -32768
(how are vars behaving? what if you explicitly cast - that should work?)
import is now order dependent due to merges, you must use this order:
%import textio
%import floats
needs to be fixed. It should be an error for the same block to be declared twice without either
declaration having %option merge, but as long as at least all but one declaration includes the option,
it shouldn't matter where in the list the one without it falls. See unit test breaking in TestProgram
...

View File

@ -1,5 +1,5 @@
%import floats
%import textio
%import floats
%zeropage basicsafe
; Note: this program can be compiled for multiple target systems.

View File

@ -1,5 +1,5 @@
%import floats
%import textio
%import floats
%option no_sysinit
%zeropage basicsafe
@ -23,15 +23,13 @@ main {
txt.nl()
txt.nl()
floats.print(floats.EPSILON)
txt.print_f(floats.EPSILON)
txt.nl()
floats.print(floats.MIN)
txt.print_f(sys.MIN_FLOAT)
txt.nl()
floats.print(floats.MAX)
txt.print_f(sys.MAX_FLOAT)
txt.nl()
floats.print(floats.E)
txt.nl()
txt.print_ub(floats.SIZEOF)
txt.print_f(floats.E)
txt.nl()
txt.print_ub(sys.SIZEOF_FLOAT)
txt.nl()