Add compiler

This commit is contained in:
Asraelite 2024-05-17 18:37:51 +02:00
parent 476972f85a
commit 3f3125ef43
31 changed files with 2625 additions and 3 deletions

View file

@ -1,4 +1,4 @@
export function toBitString(n: number, bits: number, signed: boolean = false): string {
export function toBitString(n: number, bits: number, signed: boolean = false, littleEndianByteSize: null | number = null): string {
const minimum = -(2 ** (bits - 1));
const maximum = signed ? (2 ** (bits - 1)) - 1 : 2 ** bits - 1;
@ -18,6 +18,15 @@ export function toBitString(n: number, bits: number, signed: boolean = false): s
if (n !== 0) {
throw new Error(`Internal error: ${n} not zero after conversion to ${bits}-bit ${signed ? 'signed' : 'unsigned'} integer`);
}
if (littleEndianByteSize !== null) {
const bytes = [];
for (let i = 0; i < result.length / littleEndianByteSize; i++) {
bytes.push(result.substring(i * littleEndianByteSize, (i + 1) * littleEndianByteSize));
}
result = bytes.reverse().join('');
}
return result;
}