symbol-processor/src/map.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-01-04 20:27:02 +00:00
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 (Object.prototype.hasOwnProperty.call(structures, name)) {
// 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) {
if (Object.prototype.hasOwnProperty.call(structures, name)) {
delete structures[name];
}
}
}