Added getBytes :: "filename.bc0" -> byte list

This commit is contained in:
Mitchell Plamann
2015-03-20 22:51:13 -04:00
parent e845be4576
commit 3f15a249c8
4 changed files with 74 additions and 0 deletions

35
src/bytecode-parser.js Normal file
View File

@@ -0,0 +1,35 @@
fs = require("fs");
// This is a simple, kinda hacky bytecode parser for .bc0 files
function getBytes(filename) {
data = fs.readFileSync(filename);
if (data == null) {
if (err["code"] === "ENOENT")
console.log("Error: file " + filename + " does not exist.");
else
console.log("Error: " + err);
return;
}
// Data contains our file, but we want it as a string
string_data = data.toString();
// Strip all the comments for easier parsing
without_comments = string_data.replace(new RegExp("#.*", "gi"), "");
// Each byte should now be a pair of two hex digits.
// Put all these in an array.
bytes = [];
without_comments.replace(
new RegExp("([0123456789ABCDEF][0123456789ABCDEF])", "gi"),
function(next_byte) {
bytes.push(parseInt(next_byte, 16));
});
// We now have an array of bytes. That's probably everything we need, right?
return bytes;
}
exports.getBytes = getBytes;

6
src/index.js Normal file
View File

@@ -0,0 +1,6 @@
parser = require("./bytecode-parser");
console.log("Reading in sample bytecode file:");
console.log(parser.getBytes("../test/test.bc0"));
console.log("That was the sample bytecode file" +
" -- it probably took up your whole terminal screen.");