Initializing an associative array of structure values ​​and string keys

(for programming language D)

I am trying to initialize an associative array with structure elements and should be indexed line by line. I would import it as a module from a separate file.

This is what I want to achieve (and it does not work --- I do not know if this is possible):

mnemonic_info[string] mnemonic_table = [
        /* name,         format,          opcode */
        "ADD": {mnemonic_format.Format3M, 0x18},
        ...

        /* NOTE: mnemonic_format is an enum type. */
        /* mnemonic_info is a struct with a mnemonic_format and an ubyte */
];

Note that this works great for arrays indexed by integers.

Optimally, I would like this to be evaluated at compile time, since I won't change it. However, if this is not possible, I would be happy if you would tell me about the best way to build such an array in / before the immediate runtime.

I need this because I am writing assembler.

SO , , , .

D , , , - , .

!

: Tuples ?

Edit

, , :

mnemonic_info[string] mnemonic_table;
static this() { // Not idea what this does.
        mnemonic_info entry;

        entry.format = mnemonic_format.Format3M;
        entry.opcode = 0x18;
        mnemonic_table["ADD"] = entry;

        /* ... for all entries. */
}
+5
1

D , .

, , .

. D- C-.

:

struct Foo {
    int a;
    string b;
}

Foo[string] global;

static this() {
    global = [
        "foo" : Foo(1, "hurr"),
        "bar" : Foo(2, "durr")
    ];
}

void main() {
    assert(global["foo"].a == 1);
}
+6

All Articles