From 3f15a249c83d1e21d46249b28e2b466c3e846fbb Mon Sep 17 00:00:00 2001 From: Mitchell Plamann Date: Fri, 20 Mar 2015 22:51:13 -0400 Subject: [PATCH] Added getBytes :: "filename.bc0" -> byte list --- src/bytecode-parser.js | 35 +++++++++++++++++++++++++++++++++++ src/index.js | 6 ++++++ test/test.bc0 | 27 +++++++++++++++++++++++++++ test/test.c0 | 6 ++++++ 4 files changed, 74 insertions(+) create mode 100644 src/bytecode-parser.js create mode 100644 src/index.js create mode 100644 test/test.bc0 create mode 100644 test/test.c0 diff --git a/src/bytecode-parser.js b/src/bytecode-parser.js new file mode 100644 index 0000000..76c3e92 --- /dev/null +++ b/src/bytecode-parser.js @@ -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; diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..475ce31 --- /dev/null +++ b/src/index.js @@ -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."); diff --git a/test/test.bc0 b/test/test.bc0 new file mode 100644 index 0000000..575ac0e --- /dev/null +++ b/test/test.bc0 @@ -0,0 +1,27 @@ +C0 C0 FF EE # magic number +00 0D # version 6, arch = 1 (64 bits) + +00 00 # int pool count +# int pool + +00 27 # string pool total size +# string pool +48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 2E 0A 59 6F 75 20 64 6F 6E 27 74 20 6C 6F 6F 6B 20 73 6F 20 67 6F 6F 64 2E 0A 00 # "Hello, world.\nYou don\'t look so good.\n" + +00 01 # function count +# function_pool + +#
+00 00 # number of arguments = 0 +00 00 # number of local variables = 0 +00 0A # code length = 10 bytes +14 00 00 # aldc 0 # s[0] = "Hello, world.\nYou don\'t look so good.\n" +B7 00 00 # invokenative 0 # print("Hello, world.\nYou don\'t look so good.\n") +57 # pop # (ignore result) +10 00 # bipush 0 # 0 +B0 # return # + +00 01 # native count +# native pool +00 01 00 10 # print + diff --git a/test/test.c0 b/test/test.c0 new file mode 100644 index 0000000..7bfe6a2 --- /dev/null +++ b/test/test.c0 @@ -0,0 +1,6 @@ +#use + +int main() { + print("Hello, world.\nYou don't look so good.\n"); + return 0; +}