This commit is contained in:
TheBrokenRail 2024-01-05 15:47:53 -05:00
parent ddbbbcb8e3
commit 92bb2b8191
5 changed files with 1726 additions and 22 deletions

3
.gitignore vendored
View File

@ -1,5 +1,2 @@
/build /build
/package-lock.json
/node_modules /node_modules
/out
/symbols

1684
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,13 +10,12 @@
"author": "TheBrokenRail", "author": "TheBrokenRail",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@tsconfig/node-lts": "^18.12.4", "@tsconfig/node-lts": "^18.12.5",
"@tsconfig/strictest": "^2.0.1", "@tsconfig/strictest": "^2.0.2",
"@types/node": "^20.5.6", "@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.4.1", "@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.4.1", "@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.48.0", "eslint": "^8.56.0",
"openapi-types": "^12.1.3", "typescript": "^5.3.3"
"typescript": "^5.2.2"
} }
} }

View File

@ -1,10 +1,11 @@
import * as fs from 'node:fs'; import * as fs from 'node:fs';
import { STRUCTURE_FILES, EXTENSION, INDENT } from './common'; import { STRUCTURE_FILES, EXTENSION, INDENT } from './common';
import { getStructure, setCppAllowed } from './map'; import { getStructure, setCppAllowed } from './map';
import { Struct } from './struct';
// Arguments // Arguments
if (process.argv.length < 5) { if (process.argv.length < 5) {
console.log('USAGE: npm run <Source Output File> <Header Output File> <Input Files...>'); console.log('USAGE: npm start -- <Source Output File> <Header Output File> <Input Files...>');
process.exit(1); process.exit(1);
} }
process.argv.shift(); process.argv.shift();
@ -60,16 +61,15 @@ function loadSymbols() {
return structureObjects; return structureObjects;
} }
// Generate Part Of Header // Sort Structures By Dependency
function makeHeaderPart(allowCpp: boolean) { function dependencySort(structureObjects: Struct[]) {
// Set Mode let loops = 0;
setCppAllowed(allowCpp); const MAX_LOOPS = 100;
// Load Symbols
const structureObjects = loadSymbols();
// Sort Structures By Dependency
while (true) { while (true) {
if (loops > MAX_LOOPS) {
throw new Error('Unable To Sort Dependencies');
}
loops++;
let valid = true; let valid = true;
// Loop Through Structures // Loop Through Structures
for (const structure of structureObjects) { for (const structure of structureObjects) {
@ -97,6 +97,18 @@ function makeHeaderPart(allowCpp: boolean) {
break; break;
} }
} }
}
// Generate Part Of Header
function makeHeaderPart(allowCpp: boolean) {
// Set Mode
setCppAllowed(allowCpp);
// Load Symbols
const structureObjects = loadSymbols();
// Sort Structures By Dependency
dependencySort(structureObjects);
// Generate Code // Generate Code
let structures = ''; let structures = '';
@ -125,6 +137,10 @@ function makeHeaderPart(allowCpp: boolean) {
function makeMainHeader(output: string) { function makeMainHeader(output: string) {
let result = ''; let result = '';
result += '#pragma once\n'; result += '#pragma once\n';
result += '\n// Check Architecture\n';
result += '#ifndef __arm__\n';
result += '#error "Symbols Are ARM-Only"\n';
result += '#endif\n';
result += '\n// Suppress Warnings\n'; result += '\n// Suppress Warnings\n';
result += '#pragma GCC diagnostic push\n'; result += '#pragma GCC diagnostic push\n';
result += '#pragma GCC diagnostic ignored "-Wunused-variable"\n'; result += '#pragma GCC diagnostic ignored "-Wunused-variable"\n';
@ -137,13 +153,15 @@ function makeMainHeader(output: string) {
result += '\n// Forward Declarations\n'; result += '\n// Forward Declarations\n';
for (const name in STRUCTURE_FILES) { for (const name in STRUCTURE_FILES) {
result += `typedef struct ${name} ${name};\n`; result += `typedef struct ${name} ${name};\n`;
}result += '\n// Extra Headers\n'; }
result += '\n// Extra Headers\n';
for (const file of extraHeaders) { for (const file of extraHeaders) {
result += fs.readFileSync(file, {encoding: 'utf8'}); result += fs.readFileSync(file, {encoding: 'utf8'});
} }
result += '\n// Switch Mode\n'; result += '\n// Switch Mode\n';
result += '#ifndef __cplusplus\n'; result += '#ifndef __cplusplus\n';
result += '\n// C Mode\n'; result += '\n// C Mode\n';
result += '#include <stddef.h>\n';
result += 'typedef uchar bool;\n\n'; result += 'typedef uchar bool;\n\n';
result += makeHeaderPart(false); result += makeHeaderPart(false);
result += '\n// End C Mode\n'; result += '\n// End C Mode\n';
@ -151,6 +169,7 @@ function makeMainHeader(output: string) {
result += '\n// C++ Mode\n'; result += '\n// C++ Mode\n';
result += '#include <string>\n'; result += '#include <string>\n';
result += '#include <vector>\n'; result += '#include <vector>\n';
result += '#include <cstddef>\n';
result += 'extern "C" {\n\n'; result += 'extern "C" {\n\n';
result += makeHeaderPart(true); result += makeHeaderPart(true);
result += '\n// End C++ Mode\n'; result += '\n// End C++ Mode\n';

View File

@ -157,6 +157,11 @@ export class Struct {
out += `extern ${method.generateDefinition()}`; out += `extern ${method.generateDefinition()}`;
} }
// Early Exit For Undefined Structures
if (this.#properties.length === 0 && this.#size === null) {
return out;
}
// VTable // VTable
if (this.#vtable !== null) { if (this.#vtable !== null) {
out += this.#vtable.generate(); out += this.#vtable.generate();