Skip to content

Commit

Permalink
Fix array wrong initializer syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jan 29, 2024
1 parent eb0546b commit e452d3b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Binary file modified docs/ctod.wasm
Binary file not shown.
9 changes: 7 additions & 2 deletions source/ctod/cdeclaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ bool ctodTryInitializer(ref CtodCtx ctx, ref Node node) {
return true;
case Sym.initializer_list:
auto t = ctx.inDeclType;
bool arrayInit = t.isCArray || t.isStaticArray;

bool arrayInit = false;
if (t.isCArray || t.isStaticArray) {
arrayInit = true;
ctx.inDeclType = t.next[0];
}
// In D, the initializer braces depend on whether it's a struct {} or array []
// The current type check is rather primitive, it doesn't look up type aliases and
// doesn't consider nested types (e.g. struct of array of struct of ...)
Expand All @@ -167,7 +170,9 @@ bool ctodTryInitializer(ref CtodCtx ctx, ref Node node) {
}
}
}
ctx.translateNode(c);
}
ctx.inDeclType = t;

if (arrayInit) {
if (auto c = node.firstChildType(Sym.anon_LBRACE)) {
Expand Down
17 changes: 17 additions & 0 deletions source/ctod/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,23 @@ S a = {
fieldA: 1,
fieldB: 2,
};
");

test("
struct S { double x; int y; }
Sarray[2] = {
{1.5, 2},
{2.5, 3}
};
struct S1 { struct { int y; }; } Sarray[1][1] = {{ {{2}} }};
", "
struct S { double x = 0; int y; }S[2] Sarray = [
{1.5, 2},
{2.5, 3}
];
struct S1 { struct { int y; }; }S1[1][1] Sarray = [[ {{2}} ]];
");
}

Expand Down

0 comments on commit e452d3b

Please sign in to comment.