[WIP/MOD] Start allowing j2s to take an union type
This commit is contained in:
parent
fb8ec5f3ae
commit
3cdd227175
1 changed files with 34 additions and 4 deletions
38
tools/j2s.c
38
tools/j2s.c
|
@ -309,7 +309,7 @@ Main(Array * args)
|
|||
ArrayAdd(requiredTypes, StrDuplicate(type));
|
||||
}
|
||||
|
||||
if (StrEquals(typeType, "struct"))
|
||||
if (StrEquals(typeType, "struct") || StrEquals(typeType, "union"))
|
||||
{
|
||||
typeFieldsVal = HashMapGet(typeObj, "fields");
|
||||
if (JsonValueType(typeFieldsVal) != JSON_OBJECT)
|
||||
|
@ -481,6 +481,7 @@ Main(Array * args)
|
|||
HashMap *fields;
|
||||
|
||||
char *field;
|
||||
char *info;
|
||||
HashMap *fieldDesc;
|
||||
|
||||
if (!type)
|
||||
|
@ -497,9 +498,29 @@ Main(Array * args)
|
|||
|
||||
fields = JsonValueAsObject(JsonGet(types, 2, type, "fields"));
|
||||
|
||||
StreamPrintf(headerFile, "typedef %s %s\n{\n", typeType, type);
|
||||
|
||||
if (StrEquals(typeType, "struct"))
|
||||
if (StrEquals(typeType, "union"))
|
||||
{
|
||||
Array *keys = HashMapKeys(fields);
|
||||
|
||||
size_t j;
|
||||
|
||||
StreamPrintf(headerFile, "typedef enum %sType\n{\n", type);
|
||||
|
||||
for (j = 0; j < ArraySize(keys); j++)
|
||||
{
|
||||
char *key = ArrayGet(keys, j);
|
||||
char *comma = j == ArraySize(keys) - 1 ? "\n" : ",\n";
|
||||
StreamPrintf(headerFile, " %s_AS_%s%s", type, key, comma);
|
||||
}
|
||||
|
||||
Free(keys);
|
||||
StreamPrintf(headerFile, "} %sType;\n\n", type);
|
||||
}
|
||||
info = StrEquals(typeType, "union") ? "Union" : "";
|
||||
StreamPrintf(headerFile, "typedef %s %s%s\n{\n", typeType, type, info);
|
||||
|
||||
if (StrEquals(typeType, "struct") || StrEquals(typeType, "union"))
|
||||
{
|
||||
while (HashMapIterate(fields, &field, (void **) &fieldDesc))
|
||||
{
|
||||
|
@ -542,7 +563,16 @@ Main(Array * args)
|
|||
StreamPrintf(headerFile, " %s %s;\n", cType, field);
|
||||
}
|
||||
|
||||
StreamPrintf(headerFile, "} %s;\n\n", type);
|
||||
StreamPrintf(headerFile, "} %s%s;\n\n", type, info);
|
||||
|
||||
/* TODO: Add extra "type" enum or something*/
|
||||
if (StrEquals(typeType, "union"))
|
||||
{
|
||||
StreamPrintf(headerFile, "typedef struct %s {\n", type);
|
||||
StreamPrintf(headerFile, " %sType type;\n", type);
|
||||
StreamPrintf(headerFile, " %s%s value;\n", type, info);
|
||||
StreamPrintf(headerFile, "} %s;\n\n", type);
|
||||
}
|
||||
}
|
||||
else if (StrEquals(typeType, "enum"))
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue