make sizeof(float) work, so you don't have to use sys.SIZEOF_FLOAT anymore etc.

define sys.SIZEOF_FLOAT in terms of sizeof(float)
This commit is contained in:
Irmen de Jong
2025-05-29 02:37:00 +02:00
parent 368387e1a7
commit 3b1b0985c1
18 changed files with 173 additions and 102 deletions

View File

@@ -262,12 +262,13 @@ class TestMemory: FunSpec({
shouldThrow<IllegalArgumentException> {
target.memorySize(BaseDataType.UNDEFINED)
}
shouldThrow<IllegalArgumentException> {
target.memorySize(BaseDataType.LONG)
shouldThrow<NoSuchElementException> {
target.memorySize(DataType.arrayFor(BaseDataType.LONG), 10)
}
target.memorySize(BaseDataType.BOOL) shouldBe 1
target.memorySize(BaseDataType.BYTE) shouldBe 1
target.memorySize(BaseDataType.WORD) shouldBe 2
target.memorySize(BaseDataType.LONG) shouldBe 4
target.memorySize(BaseDataType.FLOAT) shouldBe target.FLOAT_MEM_SIZE
target.memorySize(DataType.BOOL, null) shouldBe 1
@@ -283,13 +284,14 @@ class TestMemory: FunSpec({
target.memorySize(DataType.arrayFor(BaseDataType.BOOL), 10) shouldBe 10
target.memorySize(DataType.arrayFor(BaseDataType.BYTE), 10) shouldBe 10
target.memorySize(DataType.arrayFor(BaseDataType.WORD), 10) shouldBe 20
target.memorySize(DataType.arrayFor(BaseDataType.WORD), 10) shouldBe 20
target.memorySize(DataType.arrayFor(BaseDataType.UWORD), 10) shouldBe 20
target.memorySize(DataType.arrayFor(BaseDataType.FLOAT), 10) shouldBe 10*target.FLOAT_MEM_SIZE
target.memorySize(DataType.arrayFor(BaseDataType.WORD, true), 10) shouldBe 20
target.memorySize(DataType.arrayFor(BaseDataType.UWORD, true), 10) shouldBe 20
target.memorySize(DataType.BOOL, 10) shouldBe 10
target.memorySize(DataType.UWORD, 10) shouldBe 20
target.memorySize(DataType.LONG, 10) shouldBe 40
target.memorySize(DataType.FLOAT, 10) shouldBe 10*target.FLOAT_MEM_SIZE
}
}

View File

@@ -1062,4 +1062,47 @@ main {
st.size shouldBe 3
}
test("allow type name as argument for sizeof()") {
val src="""
%option enable_floats
main {
sub start() {
bool @shared b
float @shared f
word @shared w
const long ll = 9999999
ubyte @shared ub1, ub2
ub1 = sys.SIZEOF_BOOL
ub2 = sys.SIZEOF_WORD
ub1 = sys.SIZEOF_LONG
ub2 = sys.SIZEOF_FLOAT
ub1 = sizeof(true)
ub2 = sizeof(1234)
ub1 = sizeof(12345678)
ub2 = sizeof(9.999)
ub1 = sizeof(b)
ub2 = sizeof(w)
ub1 = sizeof(ll)
ub2 = sizeof(f)
ub1 = sizeof(bool)
ub2 = sizeof(word)
ub1 = sizeof(long)
ub2 = sizeof(float)
}
}"""
val result = compileText(VMTarget(), false, src, outputDir, writeAssembly = false)!!
val st = result.compilerAst.entrypoint.statements
st.size shouldBe 27
val assignments = st.dropLast(1).takeLast(16)
assignments.forEach { a ->
(a as Assignment).value shouldBe instanceOf<NumericLiteral>()
}
}
})