初始化结构体值和字符串键的关联数组

发布于 2024-12-20 08:01:53 字数 1073 浏览 0 评论 0原文

(对于“D”编程语言)

我一直在努力初始化一个具有结构元素并且应该可以通过字符串索引的关联数组。我会将其作为模块从单独的文件导入。

这就是我想要实现的目标(但它不起作用——我不知道这是否可能):

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 */
];

请注意,这对于可通过整数索引的数组来说效果很好。

最好,我希望在编译时对其进行评估,因为我不会更改它。但是,如果不可能,如果您告诉我在立即运行时/之前构建此类数组的最佳方法,我会很高兴。

我需要这个,因为我正在编写汇编程序。

我已经在 SO 和互联网上搜索了答案,但只能找到带有整数的示例,以及其他我不理解或无法工作的东西。

到目前为止我真的很喜欢D,但由于网上没有太多教程,它似乎很难学习。

谢谢!

附带说明:是否可以使用元组作为关联数组元素而不是自定义结构?

编辑

到目前为止我发现了一种方法,但它非常丑陋:

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. */
}

(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. */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2024-12-27 08:01:54

在 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文