107 lines
3.3 KiB
TypeScript
107 lines
3.3 KiB
TypeScript
import * as fs from 'node:fs';
|
|
import * as path from 'node:path';
|
|
|
|
export const INDENT = ' ';
|
|
export const POINTER_SIZE = 4;
|
|
export const RTTI_SIZE = POINTER_SIZE;
|
|
export const EXTENSION = '.def';
|
|
export const STRUCTURE_FILES: {[id: string]: string} = {};
|
|
export function readDefinition(name: string) {
|
|
if (!STRUCTURE_FILES[name]) {
|
|
throw new Error(`Missing Definition File: ${name}${EXTENSION}`);
|
|
}
|
|
return fs.readFileSync(STRUCTURE_FILES[name]!, {encoding: 'utf8'});
|
|
}
|
|
export function syntaxError(message?: string) {
|
|
throw new Error('Syntax Error' + (message ? `: ${message}` : ''));
|
|
}
|
|
export function parseTypeAndName(piece: string) {
|
|
// Split On Last Space
|
|
const index = piece.lastIndexOf(' ');
|
|
if (index === -1) {
|
|
syntaxError('Unable To Find Name/Type Divider');
|
|
}
|
|
let name = piece.substring(index + 1)!;
|
|
let type = piece.substring(0, index)!;
|
|
// Move Asterisks From Name To Type
|
|
while (name.startsWith('*') || name.startsWith('&')) {
|
|
const x = name.substring(0, 1);
|
|
name = name.substring(1);
|
|
if (!type.endsWith('*') && !type.endsWith('&')) {
|
|
type += ' ';
|
|
}
|
|
type += x;
|
|
}
|
|
// Return
|
|
return {type, name};
|
|
}
|
|
export function toUpperSnakeCase(str: string) {
|
|
let wasUpper = false;
|
|
let nextIsUpper = false;
|
|
let out = '';
|
|
for (let i = 0; i < str.length; i++) {
|
|
let character = str.charAt(i);
|
|
if (character === '_') {
|
|
wasUpper = false;
|
|
nextIsUpper = true;
|
|
continue;
|
|
}
|
|
const isUpper = character === character.toUpperCase() || nextIsUpper;
|
|
nextIsUpper = false;
|
|
character = character.toUpperCase();
|
|
if (isUpper && i > 0 && !wasUpper) {
|
|
out += '_';
|
|
}
|
|
out += character;
|
|
wasUpper = isUpper;
|
|
}
|
|
return out;
|
|
}
|
|
export function formatType(type: string) {
|
|
if (!type.endsWith('*') && !type.endsWith('&')) {
|
|
type += ' ';
|
|
}
|
|
return type;
|
|
}
|
|
export const COMMENT = '//';
|
|
export function toHex(x: number) {
|
|
return '0x' + x.toString(16);
|
|
}
|
|
export function assertSize(name: string, size: number) {
|
|
return `static_assert(sizeof(${name}) == ${toHex(size)}, "Invalid Size");\n`;
|
|
}
|
|
export function safeParseInt(str: string) {
|
|
const x = parseInt(str);
|
|
if (isNaN(x)) {
|
|
throw new Error('Invalid Integer: ' + str);
|
|
}
|
|
return x;
|
|
}
|
|
export function getSelfArg(type: string) {
|
|
return `${type} *self`;
|
|
}
|
|
export function prependArg(args: string, arg: string) {
|
|
if (args !== '()') {
|
|
arg += ', ';
|
|
}
|
|
return '(' + arg + args.substring(1);
|
|
}
|
|
export function getDataDir() {
|
|
return path.join(__dirname, '..', 'data');
|
|
}
|
|
export function formatFile(file: string, options: {[key: string]: string}) {
|
|
file = path.join(getDataDir(), file);
|
|
let data = fs.readFileSync(file, {encoding: 'utf8'});
|
|
for (const key in options) {
|
|
data = data.replace(`{{ ${key} }}`, options[key]!);
|
|
}
|
|
return data.trim() + '\n';
|
|
}
|
|
export const INTERNAL = '__';
|
|
export function preventConstruction(self: string) {
|
|
let out = '';
|
|
out += `${INDENT}${INTERNAL}PREVENT_CONSTRUCTION(${self});\n`;
|
|
out += `${INDENT}${INTERNAL}PREVENT_COPY(${self});\n`;
|
|
return out;
|
|
}
|
|
export const LEAN_HEADER_GUARD = '#ifndef LEAN_SYMBOLS_HEADER\n'; |