Is there any way to emulate designated initializers?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Soph
Posts: 9
Joined: Mon May 30, 2022 1:40 am

Is there any way to emulate designated initializers?

Post by Soph »

Hello!

In C (99) I can use designated initializers to map array members to designated positions:

Code: Select all

enum Fruit_t {
    APPLES,
    ORANGES,
    STRAWBERRIES = 8
};

    static const int price_lookup[] = {
        [APPLES] = 6,
        [ORANGES] = 10,
        [STRAWBERRIES] = 55
    };
That's vital for when you want to make sure that adding one more element in the middle doesn't mess with the values.
I completely understand that ca65 isn't C compiler. But sometimes we meet the same problem - to map "arrays" to designated enums:

Code: Select all

.scope loader
    noarg = $0000
.endscope

.scope stage
    .scope init
        screen  = $00
        vars    = $01
    .endscope

    .scope field
        draw    = $02
    .endscope

    .scope gameplay
        select  = $03
        put     = $04
    .endscope

    .scope animation
        headup  = $05
        nohead  = $06
    .endscope

    .scope system
        end     = $07
    .endscope
.endscope

stage_storage:
    .word loader_text,          scene_game    ; stage::init::screen
    .word utility_init,         loader::noarg ; stage::init::vars

    .word utility_field,        loader::noarg ; stage::field::draw

    .word gameplay_select,      loader::noarg ; stage::gameplay::select
    .word gameplay_put,         loader::noarg ; stage::gameplay::put

    .word animation_headup,     loader::noarg ; stage::animation::headup
    .word animation_nohead,     loader::noarg ; stage::animation::nohead

    .word loader_dummy,         loader::noarg ; stage::system::end
Thus, messing with the enum values/order will break the whole array order/values. Is there any hack/code that can help me keeping values synced?
I know that this kind of tasks is better solved with external tools/preprocessors, but maybe there's something within ca65.

Thanks in advance.
Post Reply