Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions src/compiler/header-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class HeaderBuilder {
const DEFINE = options.headerGuard === undefined ?
`INCLUDE_${PREFIX}_H_` : options.headerGuard;

// ifdef
res += `#ifndef ${DEFINE}\n`;
res += `#define ${DEFINE}\n`;
res += '#ifdef __cplusplus\n';
Expand All @@ -25,51 +26,78 @@ export class HeaderBuilder {
res += '#include <stdint.h>\n';
res += '\n';

// Structure
res += `typedef struct ${options.prefix}_s ${options.prefix}_t;\n`;
res += `struct ${options.prefix}_s {\n`;
res += ' int32_t _index;\n';
// Structure fields
const fields: Array<{ name: string, size: number, type: string }> = [];
fields.push({ name: '_index', size: 4, type: 'int32_t' });

for (const [ index, field ] of options.spans.entries()) {
res += ` void* _span_pos${index};\n`;
fields.push({ name: `_span_pos${index}`, size: -1, type: 'void*' });
if (field.callbacks.length > 1) {
res += ` void* _span_cb${index};\n`;
fields.push({ name: `_span_cb${index}`, size: -1, type: 'void*' });
}
}

res += ' int32_t error;\n';
res += ' const char* reason;\n';
res += ' const char* error_pos;\n';
res += ' void* data;\n';
res += ' void* _current;\n';

for (const prop of options.properties) {
let ty: string;
if (prop.ty === 'i8') {
ty = 'uint8_t';
} else if (prop.ty === 'i16') {
ty = 'uint16_t';
} else if (prop.ty === 'i32') {
ty = 'uint32_t';
} else if (prop.ty === 'i64') {
ty = 'uint64_t';
} else if (prop.ty === 'ptr') {
ty = 'void*';
fields.push({ name: 'error', size: 4, type: 'int32_t' });
fields.push({ name: 'reason', size: -1, type: 'const char*' });
fields.push({ name: 'error_pos', size: -1, type: 'const char*' });
fields.push({ name: 'data', size: -1, type: 'void*' });
fields.push({ name: '_current', size: -1, type: 'void*' });

for (const { name, ty } of options.properties) {
if (ty === 'i8') {
fields.push({ name, size: 1, type: 'uint8_t' });
} else if (ty === 'i16') {
fields.push({ name, size: 2, type: 'uint16_t' });
} else if (ty === 'i32') {
fields.push({ name, size: 4, type: 'uint32_t' });
} else if (ty === 'i64') {
fields.push({ name, size: 8, type: 'uint64_t' });
} else if (ty === 'ptr') {
fields.push({ name, size: -1, type: 'void*' });
} else {
throw new Error(
`Unknown state property type: "${prop.ty}"`);
throw new Error(`Unknown state property type: "${ty}"`);
}
res += ` ${ty} ${prop.name};\n`;
}
res += '};\n';

// Sort fields from bigger size to lower.
// Pointers are special, they should be after 8-bytes, but before 4-bytes.
// In this case align work for 32-bit and 64-bit.
fields.sort((a, b) => {
if (a.name === b.name) {
throw new Error(`Two fields with same name: "${a.name}"`);
}

if (a.size === b.size) {
return a.name < b.name ? -1 : 1;
}

if (a.size === -1) {
return b.size >= 8 ? 1 : -1;
}

if (b.size === -1) {
return a.size >= 8 ? -1 : 1;
}

return b.size - a.size;
});

// Structure
res += `typedef struct ${options.prefix}_s ${options.prefix}_t;\n`;
res += `struct ${options.prefix}_s {\n`;
for (const { name, type } of fields) {
res += ` ${type} ${name};\n`;
}
res += '};\n';
res += '\n';

// Functions: init, execute
res += `int ${options.prefix}_init(${options.prefix}_t* s);\n`;
res += `int ${options.prefix}_execute(${options.prefix}_t* s, ` +
'const char* p, const char* endp);\n';

res += '\n';

// endif
res += '#ifdef __cplusplus\n';
res += '} /* extern "C" *\/\n';
res += '#endif\n';
Expand Down