[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC] Array Integrators
From: |
Mohammad-Reza Nabipoor |
Subject: |
[RFC] Array Integrators |
Date: |
Sat, 1 Jan 2022 23:15:43 +0330 |
Hi, Jose and people!
Happy New Year!
I think email is a better medium for this discussion than IRC, so I'm writing
this email.
I will write my understanding of array integrator as a bunch of
assertions, so, please tell me your comments on them.
One thing to consider is that I think integration should be endian-dependent.
Endians matters because we're dealing with "bytes" which will become an
integer. We're dealing with the "representation" of integers, not integers.
This is different from integral structs which are also integrals, which
endianness doesn't matter for them.
What about this syntax?
```poke
set_endian (ENDIAN_BIG);
assert ([0xabUB, 0x12UB] as uint16 == 0xab12);
assert ([0xabUB, 0x12UB] as big uint16 == 0xab12);
assert ([0xabUB, 0x12UB] as little uint16 == 0x12ab);
set_endian (ENDIAN_LITTLE);
assert ([0xabUB, 0x12UB] as uint16 == 0x12ab);
assert ([0xabUB, 0x12UB] as big uint16 == 0xab12);
assert ([0xabUB, 0x12UB] as little uint16 == 0x12ab);
```
One more thing, I'm using this predicate to see if a thing is integratable
or not (WDYT?):
```c
int
pkl_ast_type_integratable_p (pkl_ast_node type)
{
if (PKL_AST_TYPE_CODE (type) == PKL_TYPE_ARRAY)
{
pkl_ast_node etype = PKL_AST_TYPE_A_ETYPE (type);
switch (PKL_AST_TYPE_CODE (etype))
{
case PKL_TYPE_INTEGRAL:
case PKL_TYPE_OFFSET:
return 1;
case PKL_TYPE_ARRAY:
case PKL_TYPE_STRUCT:
return pkl_ast_type_integratable_p (etype);
default:
return 0;
}
}
/* Integral structs are integratable. */
if (PKL_AST_TYPE_CODE (type) == PKL_TYPE_STRUCT
&& PKL_AST_TYPE_S_ITYPE (type) != NULL)
return 1;
return 0;
}
```
Test cases:
```poke
set_endian (ENDIAN_BIG);
assert ([1UB] as byte = 1UB);
assert ([1UB, 2UB] as uint16 == 0x0102UH);
assert ([1UB, 2UB, 3UB, 4UB] as int32 == 0x01020304);
assert ([[1UB], [2UB]] as uint32 == 0x0000_0102U);
assert ([[1UB, 2UB], [3UB, 4UB]] as int32 == 0x01020304);
assert ([[[1UB], [2UB]], [[3UB], [4UB]]] as int32 == 0x01020304);
assert ([0xaabbccdd, 0xeeff0011] as uint64 == 0xaabbccddeeff0011UL);
assert ([0xaabbUH, 0xeeffUH] as uint64 == 0xaabbeeffUL);
/* Compilation error */
/* assert ([1, 2, 3] as uint64); */ /* 3 32-bit number is wider than 64 bits */
var a1 = [1, 2, 3];
assert (!(a1 as int64 ?! E_conv));
```
We can have similar tests for offsets and integral structs.
Regards,
Mohammad-Reza
- [RFC] Array Integrators,
Mohammad-Reza Nabipoor <=