Added getBytes :: "filename.bc0" -> byte list
This commit is contained in:
35
src/bytecode-parser.js
Normal file
35
src/bytecode-parser.js
Normal 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
6
src/index.js
Normal 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.");
|
||||
Reference in New Issue
Block a user