diff --git a/tools/j2s.c b/tools/j2s.c index 57a8b6e..c3cbdd1 100644 --- a/tools/j2s.c +++ b/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")) {