forked from minecraft-pi-reborn/symbol-processor
123 lines
3.4 KiB
TypeScript
123 lines
3.4 KiB
TypeScript
import * as fs from 'node:fs';
|
|
|
|
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 = name.substring(1);
|
|
if (!type.endsWith('*')) {
|
|
type += ' ';
|
|
}
|
|
type += '*';
|
|
}
|
|
// 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 += ' ';
|
|
}
|
|
return type;
|
|
}
|
|
export const COMMENT = '//';
|
|
export function toHex(x: number) {
|
|
return '0x' + x.toString(16);
|
|
}
|
|
export function assertSize(name: string, size: number) {
|
|
let out = '';
|
|
// Define Size Macro
|
|
const macro = toUpperSnakeCase(name) + '_SIZE';
|
|
out += `#define ${macro} ${toHex(size)}\n`;
|
|
// Check Size
|
|
out += `static_assert(sizeof(${name}) == ${macro}, "Invalid Size");\n`;
|
|
// Return
|
|
return out;
|
|
}
|
|
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 getArgNames(args: string) {
|
|
// Remove Parentheses
|
|
args = args.substring(1, args.length - 1);
|
|
if (args.length === 0) {
|
|
return [];
|
|
}
|
|
// Split
|
|
const argsList = args.split(',');
|
|
// Parse
|
|
const out = [];
|
|
for (let arg of argsList) {
|
|
arg = arg.trim();
|
|
// Remove Type
|
|
const nameStart = Math.max(arg.lastIndexOf(' '), arg.lastIndexOf('*')) + 1;
|
|
arg = arg.substring(nameStart);
|
|
// Collect
|
|
out.push(arg);
|
|
}
|
|
// Return
|
|
return out;
|
|
}
|
|
export function prependArg(args: string, arg: string) {
|
|
if (args !== '()') {
|
|
arg += ', ';
|
|
}
|
|
return '(' + arg + args.substring(1);
|
|
}
|
|
export function removeFirstArg(args: string) {
|
|
let index = args.indexOf(',');
|
|
if (index === -1) {
|
|
index = args.indexOf(')');
|
|
} else {
|
|
index++;
|
|
}
|
|
return '(' + args.substring(index).trim();
|
|
} |