Skip to content

Commit

Permalink
Create binary.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mock committed Jul 11, 2018
1 parent 7aab6ff commit dc593f7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

// Run me with Node to see my output!

let util = require("util");
let P = require("..");
let B = P.Binary;

///////////////////////////////////////////////////////////////////////

let Lang = P.createLanguage({
File: p => p.MagicNumber.then(p.Int32Array),
MagicNumber: () => P.seq(B.byte(0x13), B.byte(0x37)),
Int32: () => B.int32BE,
Uint32: () => B.uint32BE,
Int32Array: p => p.Uint32.chain(n => p.Int32.times(n))
});

///////////////////////////////////////////////////////////////////////

// File format:
// - Magic number: hexadecimal "1337" byte sequence
// - Array length: 32-bit unsigned big-endian integer
// - 32-bit signed integers up to the array length
// - End of file

let magic = [0x13, 0x37];
let length = [0x00, 0x00, 0x00, 0x02];
let n512 = [0x00, 0x00, 0x02, 0x00];
let n513 = [0x00, 0x00, 0x02, 0x01];
let bytes = [].concat(magic, length, n512, n513);
let input = Buffer.from(bytes);

function prettyPrint(x) {
let opts = { depth: null, colors: "auto" };
let s = util.inspect(x, opts);
console.log(s);
}

let ast = Lang.File.tryParse(input);
prettyPrint(ast);

0 comments on commit dc593f7

Please sign in to comment.