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.");
|
||||||
27
test/test.bc0
Normal file
27
test/test.bc0
Normal file
@@ -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
|
||||||
|
|
||||||
|
#<main>
|
||||||
|
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
|
||||||
|
|
||||||
6
test/test.c0
Normal file
6
test/test.c0
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#use <conio>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
print("Hello, world.\nYou don't look so good.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user