-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the capitalisation of classes 🏛️.
- Loading branch information
Showing
21 changed files
with
187 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
class Parser { | ||
#view; | ||
#ptr = 0; | ||
#characters = {}; | ||
|
||
constructor(buffer, characters = {}) { | ||
this.#view = new DataView(buffer); | ||
this.#characters = characters; | ||
} | ||
|
||
getUint8() { | ||
return this.#view.getUint8(this.#ptr++); | ||
} | ||
|
||
getUint16() { | ||
const val = this.#view.getUint16(this.#ptr, true); | ||
this.#ptr += 2; | ||
return val; | ||
} | ||
|
||
getChar() { | ||
const charCode = this.getUint8(); | ||
|
||
if (charCode === 0x00) { | ||
return 0x00; | ||
} | ||
|
||
const char = String.fromCodePoint(charCode); | ||
|
||
if (typeof this.#characters[char] !== 'undefined') { | ||
return this.#characters[char]; | ||
} | ||
|
||
return char; | ||
} | ||
|
||
getString() { | ||
let str = ''; | ||
let charCode; | ||
do { | ||
charCode = this.getUint8(); | ||
|
||
if (charCode === 0x00) { | ||
break; | ||
} | ||
|
||
const flag = (charCode & 0x80) !== 0; | ||
charCode &= 0x7f; | ||
|
||
if (charCode < 8) { | ||
switch (charCode) { | ||
case 1: | ||
// Line break | ||
str += '<01>\n'; | ||
break; | ||
case 2: | ||
// Do not clear the text with the next `printEgo` call? | ||
str += '<02>'; | ||
break; | ||
case 3: | ||
// New line | ||
str += '<03>\n'; | ||
break; | ||
case 4: | ||
// Used only in the address sign. | ||
str += `<Var[${this.getUint8()}]>`; | ||
break; | ||
case 0: | ||
case 5: | ||
case 6: | ||
case 7: | ||
throw new Error(`Unknown escape code ${charCode}.`); | ||
default: | ||
break; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
let char = String.fromCodePoint(charCode); | ||
|
||
if (typeof this.#characters[char] === 'string') { | ||
char = this.#characters[char]; | ||
} | ||
|
||
str += char; | ||
|
||
if (flag) { | ||
str += ' '; | ||
} | ||
} while (true); | ||
|
||
return str; | ||
} | ||
|
||
peekUint8() { | ||
return this.#view.getUint8(this.#ptr); | ||
} | ||
|
||
// Return the position of the next byte to read. | ||
get pointer() { | ||
return this.#ptr; | ||
} | ||
|
||
get length() { | ||
return this.#view.byteLength; | ||
} | ||
} | ||
|
||
export default Parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Parser from './parser.js'; | ||
import Parser from './Parser.js'; | ||
|
||
const assert = console.assert; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Parser from './parser.js'; | ||
import Parser from './Parser.js'; | ||
|
||
const assert = console.assert; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Parser from '../parser.js'; | ||
import Parser from '../Parser.js'; | ||
|
||
const assert = console.assert; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Parser from '../parser.js'; | ||
import Parser from '../Parser.js'; | ||
|
||
const assert = console.assert; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Parser from '../parser.js'; | ||
import Parser from '../Parser.js'; | ||
|
||
const assert = console.assert; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Parser from '../parser.js'; | ||
import Parser from '../Parser.js'; | ||
|
||
const assert = console.assert; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const maxByteLength = 16384; // The largest resource is sprdata. | ||
|
||
class Serialiser { | ||
#view; | ||
#ptr = 0; | ||
#characters = {}; | ||
|
||
constructor(characters = {}) { | ||
const buffer = new ArrayBuffer(0, { maxByteLength }); | ||
this.#view = new DataView(buffer); | ||
// Swap keys for values. | ||
this.#characters = Object.fromEntries( | ||
Object.entries(characters).map(([key, value]) => [value, key]), | ||
); | ||
} | ||
|
||
setUint8(value = 0x00) { | ||
this.#resize(this.#ptr + 1); | ||
this.#view.setUint8(this.#ptr++, value); | ||
} | ||
|
||
setUint16(value = 0x00) { | ||
this.#resize(this.#ptr + 2); | ||
this.#view.setUint16(this.#ptr, value, true); | ||
this.#ptr += 2; | ||
} | ||
|
||
setString(str) { | ||
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt#looping_with_codepointat | ||
for (let codePoint of str) { | ||
if (this.#characters[codePoint] !== undefined) { | ||
codePoint = this.#characters[codePoint]; | ||
} | ||
this.setUint8(codePoint.codePointAt(0)); | ||
} | ||
} | ||
|
||
// Firefox doesn't support ArrayBuffer#resize. | ||
#resize(newByteLength) { | ||
if (ArrayBuffer.prototype.resize) { | ||
this.#view.buffer.resize(newByteLength); | ||
return; | ||
} | ||
|
||
const newArrayBuffer = new ArrayBuffer(newByteLength); | ||
const view = new DataView(newArrayBuffer); | ||
for (let i = 0; i < this.#view.buffer.byteLength; i++) { | ||
view.setUint8(i, this.#view.getUint8(i)); | ||
} | ||
this.#view = view; | ||
} | ||
|
||
get buffer() { | ||
return this.#view.buffer; | ||
} | ||
} | ||
|
||
export default Serialiser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters