header generator: output structures for script

This commit is contained in:
nakst 2022-02-11 21:26:55 +00:00
parent c7daea3dc5
commit f65c768292
3 changed files with 102 additions and 4 deletions

View File

@ -1251,14 +1251,14 @@ struct EsRectangle {
struct EsSpinlock {
volatile uint8_t state;
};
} @opaque();
struct EsMutex {
EsHandle event;
EsSpinlock spinlock;
volatile uint8_t state;
volatile uint32_t queued;
};
} @opaque();
struct EsCrashReason {
EsFatalError errorCode;

View File

@ -1540,6 +1540,104 @@ void OutputScript(Entry *root) {
}
FilePrintFormat(output, ") #extcall;\n");
} else if (entry->type == ENTRY_STRUCT) {
for (int i = 0; i < arrlen(entry->annotations); i++) {
Entry *annotation = entry->annotations + i;
if (0 == strcmp(annotation->name, "opaque")) {
goto skipRootEntry;
}
}
FilePrintFormat(output, "struct %s {\n", entry->name);
for (int i = 0; i < arrlen(entry->children); i++) {
Entry e = entry->children[i];
assert(e.type == ENTRY_VARIABLE);
bool isArray = false;
bool isArrayLength = false;
for (int i = 0; i < arrlen(entry->annotations); i++) {
if (0 == strcmp(entry->annotations[i].name, "array_in")
&& 0 == strcmp(entry->annotations[i].children[0].name, e.name)) {
isArray = true;
assert(e.variable.pointer);
e.variable.pointer--;
break;
} else if (0 == strcmp(entry->annotations[i].name, "array_in")
&& 0 == strcmp(entry->annotations[i].children[1].name, e.name)) {
isArrayLength = true;
break;
}
}
if (isArrayLength) {
continue;
}
FilePrintFormat(output, "\t");
bool foundType = false;
if (e.variable.pointer == 0 && ScriptWriteBasicType(e.variable.type)) {
foundType = true;
}
if (!foundType) {
for (int k = 0; k < arrlen(root->children); k++) {
if (!root->children[k].name) continue;
if (strcmp(e.variable.type, root->children[k].name)) continue;
if (root->children[k].type == ENTRY_TYPE_NAME && e.variable.pointer == 0) {
if (ScriptWriteBasicType(root->children[k].variable.type)) {
foundType = true;
break;
}
} else if (root->children[k].type == ENTRY_API_TYPE && e.variable.pointer == 1) {
FilePrintFormat(output, "%s", root->children[k].name);
foundType = true;
break;
} else if (root->children[k].type == ENTRY_ENUM && e.variable.pointer == 0) {
FilePrintFormat(output, "%s", root->children[k].name);
foundType = true;
break;
} else if (root->children[k].type == ENTRY_STRUCT && e.variable.pointer == 0) {
FilePrintFormat(output, "%s", root->children[k].name);
foundType = true;
break;
} else if (root->children[k].type == ENTRY_STRUCT && e.variable.pointer == 1) {
bool isOpaque = false;
for (int i = 0; i < arrlen(root->children[k].annotations); i++) {
if (0 == strcmp(root->children[k].annotations[i].name, "opaque")) {
isOpaque = true;
break;
}
}
if (isOpaque) {
FilePrintFormat(output, "%s", root->children[k].name);
foundType = true;
break;
}
}
}
}
if (!foundType) {
FilePrintFormat(output, "??");
}
if (e.variable.isArray) FilePrintFormat(output, "[]");
if (isArray) FilePrintFormat(output, "[]");
assert(!e.variable.isVolatile);
FilePrintFormat(output, " %s;\n", e.name);
}
FilePrintFormat(output, "};\n\n");
}
skipRootEntry:;

View File

@ -1,3 +1,5 @@
// TODO Bug: Structure fields cannot have names the same as global variables or as in other structures.
// TODO Basic missing features:
// - Other list operations: insert_many, delete_many.
// - Maps: map[int, T], map[str, T].
@ -36,8 +38,6 @@
#include <stddef.h>
#include <stdbool.h>
// #define TRACE_COMMAND_EXECUTION
#define FUNCTION_MAX_ARGUMENTS (20) // Also the maximum number of return values in a tuple.
#define T_ERROR (0)