Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialising an associative array of struct values and string keys

(for the "D" programming language)

I've been struggling trying to initialise an associative array that has struct elements and should be index-able by a string. I would import it as a module from a separate file.

This is what I want to achieve (and it doesn't work --- I don't know if this is even 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 fine for arrays indexable by integers.

Optimally, I would like this to be evaluated at compile-time, as I won't be changing it. However, if it's not possible, I would be glad if you told me of the best way to build such an array at/before immediate run-time.

I need this because I'm writing an assembler.

I have searched SO and the internets for an answer, but could only find examples with integers, and other things I didn't understand or couldn't make to work.

I really like D so far but it seems hard to learn due to there not being many tutorials online.

Thanks!

On a side note: is it possible to use Tuples for associative array elements instead of a custom struct?

Edit

There is one way I found so far, but it's pretty ugly:

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. */
}
like image 516
Matej Avatar asked Dec 10 '11 14:12

Matej


1 Answers

In D, built-in associative array literals are always created in runtime, so initializing a global associative array by assigning it some value at declaration place is currently impossible.

As you found yourself, you can workaround that by assigning a value to associative array in module constructor.

The other problem in your code is struct initialization literals. You should prefer D-style struct initializers to C-style ones.

Example:

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);
}
like image 56
Nekuromento Avatar answered Dec 17 '22 00:12

Nekuromento