symbol-processor/src/map.ts
2024-01-04 17:00:19 -05:00

50 lines
1.3 KiB
TypeScript

import { Struct } from './struct';
import { ErrorOnLine, load } from './loader';
// Track Mode
let allowCpp = false;
export function isCppAllowed() {
return allowCpp;
}
export function setCppAllowed(newMode: boolean) {
clearStructures();
allowCpp = newMode;
}
// Store Loaded Structures
const structures: {[id: string]: Struct} = {};
// Get Or Load Structure
export function getStructure(name: string) {
if (name in structures) {
// Already Loaded
return structures[name]!;
} else {
// Load Structure
try {
// Create Structure
const target = new Struct(name);
structures[name] = target;
// Parse File
load(target, name, false);
// Return
return target;
} catch (e) {
let error = e;
let extra = '';
if (e instanceof ErrorOnLine) {
extra = `${e.file}:${e.line}: `;
error = e.error;
}
console.log(`Error Loading ${name}: ${extra}${error instanceof Error ? error.stack : error}`);
process.exit(1);
}
}
}
// Clear Loaded Structures
function clearStructures() {
for (const name in structures) {
delete structures[name];
}
}