savi-lang / savi

A fast language for programmers who are passionate about their craft.
BSD 3-Clause "New" or "Revised" License
153 stars 12 forks source link

Allow array literals to include an element index #446

Open mneumann opened 1 year ago

mneumann commented 1 year ago

For example code like this:

:const _byte_class_table_data Array(U8)'val: [
    0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 00..0f
    0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 10..1f
    0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 20..2f
    0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 30..3f
    ...
]

could be written as:

:const _byte_class_table_data Array(U8)'val: [
    0x00: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    0x10: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    0x20: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    0x30: 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ...
]

Each item array element can be prefixed with the index in the array (e.g. 0x00), if none is given, the previous index is used and incremented by one. It's an error if indices are missing or are duplicate.

jemc commented 1 year ago

I wasn't a fan of this in your original suggestion in which it seemed like every element would need to be labelled with an index. But I like this suggestion a lot more - allowing some of them to be labelled for clarity and extrapolating the rest.