Compare commits
10 Commits
shyam_ragh
...
suhaasr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c742d5ed74 | ||
| 609b6e17d4 | |||
| 81a08cb9f8 | |||
| f4b3c06f79 | |||
| 88b006cbcc | |||
| ddf1d00f1b | |||
| 739005289e | |||
|
|
349fd6ff78 | ||
|
|
6138821c3a | ||
|
|
d39df5314d |
BIN
instructions/Thumbs.db
Normal file
BIN
instructions/Thumbs.db
Normal file
Binary file not shown.
@@ -1,4 +1,7 @@
|
||||
FILES = c0vm.js bytecode-parser.js byte-stream.js opcodes.js index.js
|
||||
FILES = c0vm.js bytecode-parser.js byte-stream.js opcodes.js c0ffi.js index.js
|
||||
|
||||
all: $(FILES)
|
||||
browserify $(FILES) -o vm.js
|
||||
|
||||
clean:
|
||||
rm -f vm.js
|
||||
|
||||
@@ -90,6 +90,7 @@ var Bc0File = function (filename) {
|
||||
this.function_pool = [];
|
||||
for (var i = 0; i < this.function_count; i++) {
|
||||
this.function_pool.push(new FunctionInfo(stream));
|
||||
this.function_pool[i].function_id = i;
|
||||
}
|
||||
|
||||
this.native_count = stream.get_u2();
|
||||
|
||||
@@ -108,3 +108,46 @@ exports.NATIVE_STRING_TERMINATED = 92
|
||||
exports.NATIVE_STRING_TO_CHARARRAY = 93
|
||||
exports.NATIVE_STRING_TOLOWER = 94
|
||||
|
||||
callbacks = {};
|
||||
callbacks[exports.NATIVE_STRING_LENGTH] =
|
||||
function(args) {
|
||||
return args[0].length;
|
||||
};
|
||||
|
||||
callbacks[exports.NATIVE_STRING_TO_CHARARRAY] =
|
||||
function(args, vm) {
|
||||
var address = vm.heap.length;
|
||||
vm.heap.push(args[0].length+1);
|
||||
vm.heap.push(1);
|
||||
for (var i = 0; i < args[0].length; i++) {
|
||||
vm.heap.push(args[0][i]);
|
||||
}
|
||||
vm.heap.push(0);
|
||||
return address;
|
||||
};
|
||||
|
||||
|
||||
callbacks[exports.NATIVE_STRING_FROM_CHARARRAY] =
|
||||
function(args, vm) {
|
||||
var i = args[0] + 2;
|
||||
var result = "";
|
||||
while (vm.heap[i] !== 0) {
|
||||
result += vm.heap[i];
|
||||
i++;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
callbacks[exports.NATIVE_CHAR_CHR] =
|
||||
function(args) {
|
||||
return String.fromCharCode(args[0]);
|
||||
};
|
||||
|
||||
callbacks[exports.NATIVE_CHAR_ORD] =
|
||||
function(args) {
|
||||
if (typeof args[0] == "string")
|
||||
return args[0].charCodeAt(0);
|
||||
return args[0];
|
||||
};
|
||||
|
||||
exports.default_callbacks = callbacks;
|
||||
|
||||
@@ -42,6 +42,7 @@ var StackFrame = function(file, f) {
|
||||
this.stack = [];
|
||||
this.pc = 0;
|
||||
this.program = f.code;
|
||||
this.function_id = f.function_id;
|
||||
this.variables = [];
|
||||
for (var i = 0; i < f.num_vars; i++)
|
||||
this.variables.push(0);
|
||||
@@ -69,6 +70,8 @@ var ProgramState = function(parsed_file, callback_dict) {
|
||||
}
|
||||
}
|
||||
|
||||
this.breakpoints = [];
|
||||
|
||||
// Memory is just a big array of bytes, right?
|
||||
// "Allocation" is appending onto this array
|
||||
// A pointer to memory is an index into this array.
|
||||
@@ -341,7 +344,7 @@ ProgramState.prototype.step = function() {
|
||||
}
|
||||
log("Calling native function with index " + index + " with arguments " +
|
||||
arg_array);
|
||||
this.push(native_function(arg_array));
|
||||
this.push(native_function(arg_array, this));
|
||||
break;
|
||||
|
||||
// Memory allocation operations:
|
||||
@@ -470,8 +473,41 @@ ProgramState.prototype.step = function() {
|
||||
}
|
||||
}
|
||||
|
||||
// Takes in a parsed .bc0 file and runs it
|
||||
function execute(file, callbacks, v) {
|
||||
ProgramState.prototype.remove_breakpoint = function(opcode_index) {
|
||||
if (opcode_index < 0){
|
||||
console.log("Error: negative opcode_index" + opcode_index.toString());
|
||||
throw "Error - negative opcode index";
|
||||
}
|
||||
else if (opcode_index >= this.program.length) {
|
||||
console.log("Error: opcode index larger than max index:"
|
||||
+ opcode_index.toString());
|
||||
throw "Error - opcode index too large";
|
||||
}
|
||||
for (i = 0; i < this.breakpoints.length; i++) {
|
||||
if (this.breakpoints[i][1] == opcode_index){
|
||||
var index = i;
|
||||
array.splice(index,1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log("Error: non-breakpoint opcode index" + opcode_index.toString());
|
||||
throw "Error - non-breakpoint opcode index";
|
||||
}
|
||||
|
||||
ProgramState.prototype.set_breakpoint = function(function_index, opcode_index) {
|
||||
if (opcode_index < 0) {
|
||||
console.log("Error: negative opcode_index" + opcode_index.toString());
|
||||
throw "Error - negative opcode index";
|
||||
}
|
||||
else if (opcode_index >= this.program.length) {
|
||||
console.log("Error: opcode index larger than max index:"
|
||||
+ opcode_index.toString());
|
||||
throw "Error - opcode index too large";
|
||||
}
|
||||
this.breakpoints.push([function_index, opcode_index]);
|
||||
}
|
||||
|
||||
function initialize_vm(file, callbacks, v) {
|
||||
verbose = typeof v !== 'undefined' ? v : true;
|
||||
log("Initializing with file " + file);
|
||||
|
||||
@@ -481,25 +517,44 @@ function execute(file, callbacks, v) {
|
||||
|
||||
log("Beginning execution");
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
function run_vm(vm) {
|
||||
while (true) {
|
||||
var val = state.step();
|
||||
for (breakpoint in vm.breakpoints) {
|
||||
if (vm.frame.function_id == breakpoint[0] &&
|
||||
vm.frame.pc == breakpoint[1]) {
|
||||
console.log("Breakpoint reached!");
|
||||
return vm;
|
||||
}
|
||||
}
|
||||
|
||||
var val = vm.step();
|
||||
if (val !== undefined) return val;
|
||||
|
||||
if (verbose) {
|
||||
console.log("Machine state:");
|
||||
console.log("Machine vm:");
|
||||
console.log(" Current Stack Frame:");
|
||||
console.log(" Stack: " + state.frame.stack);
|
||||
console.log(" PC: " + state.frame.pc);
|
||||
console.log(" Vars: " + state.frame.variables);
|
||||
// console.log(" Code: " + state.frame.program);
|
||||
console.log(" Heap: " + state.heap);
|
||||
console.log(" Stack: " + vm.frame.stack);
|
||||
console.log(" PC: " + vm.frame.pc);
|
||||
console.log(" Vars: " + vm.frame.variables);
|
||||
// console.log(" Code: " + vm.frame.program);
|
||||
console.log(" Heap: " + vm.heap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (at_breakpoint) {
|
||||
// save state (maybe in a global in this file?)
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
// Takes in a parsed .bc0 file and runs it
|
||||
function execute(file, callbacks, v) {
|
||||
var state = initialize_vm(file, callbacks, v);
|
||||
return run_vm(state);
|
||||
}
|
||||
|
||||
exports.execute = execute;
|
||||
exports.initialize_vm = initialize_vm;
|
||||
exports.run_vm = run_vm;
|
||||
|
||||
@@ -16,7 +16,7 @@ function print(arg) {
|
||||
$("#output").append(arg);
|
||||
}
|
||||
|
||||
callbacks = {};
|
||||
callbacks = c0ffi.default_callbacks;
|
||||
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
||||
print(args[0]);
|
||||
print("<br />");
|
||||
@@ -33,6 +33,9 @@ console.log(callbacks);
|
||||
|
||||
$("#run").click(function() {
|
||||
var input = $("#bytecode").html().replace(/(\r\n|\n|\r)/gm,"");
|
||||
|
||||
$("#output").text("");
|
||||
|
||||
var file = parser.parse($("#bytecode").text());
|
||||
print("<br />");
|
||||
print(c0vm.execute(file, callbacks));
|
||||
|
||||
113
public/vm/vm.js
113
public/vm/vm.js
@@ -131,6 +131,7 @@ var Bc0File = function (filename) {
|
||||
this.function_pool = [];
|
||||
for (var i = 0; i < this.function_count; i++) {
|
||||
this.function_pool.push(new FunctionInfo(stream));
|
||||
this.function_pool[i].function_id = i;
|
||||
}
|
||||
|
||||
this.native_count = stream.get_u2();
|
||||
@@ -267,6 +268,49 @@ exports.NATIVE_STRING_TERMINATED = 92
|
||||
exports.NATIVE_STRING_TO_CHARARRAY = 93
|
||||
exports.NATIVE_STRING_TOLOWER = 94
|
||||
|
||||
callbacks = {};
|
||||
callbacks[exports.NATIVE_STRING_LENGTH] =
|
||||
function(args) {
|
||||
return args[0].length;
|
||||
};
|
||||
|
||||
callbacks[exports.NATIVE_STRING_TO_CHARARRAY] =
|
||||
function(args, vm) {
|
||||
var address = vm.heap.length;
|
||||
vm.heap.push(args[0].length+1);
|
||||
vm.heap.push(1);
|
||||
for (var i = 0; i < args[0].length; i++) {
|
||||
vm.heap.push(args[0][i]);
|
||||
}
|
||||
vm.heap.push(0);
|
||||
return address;
|
||||
};
|
||||
|
||||
|
||||
callbacks[exports.NATIVE_STRING_FROM_CHARARRAY] =
|
||||
function(args, vm) {
|
||||
var i = args[0] + 2;
|
||||
var result = "";
|
||||
while (vm.heap[i] !== 0) {
|
||||
result += vm.heap[i];
|
||||
i++;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
callbacks[exports.NATIVE_CHAR_CHR] =
|
||||
function(args) {
|
||||
return String.fromCharCode(args[0]);
|
||||
};
|
||||
|
||||
callbacks[exports.NATIVE_CHAR_ORD] =
|
||||
function(args) {
|
||||
if (typeof args[0] == "string")
|
||||
return args[0].charCodeAt(0);
|
||||
return args[0];
|
||||
};
|
||||
|
||||
exports.default_callbacks = callbacks;
|
||||
|
||||
},{}],4:[function(require,module,exports){
|
||||
op = require("./opcodes");
|
||||
@@ -299,7 +343,6 @@ function i32_to_array(i32) {
|
||||
((i32 >> 8) & 0xFF),
|
||||
((i32 >> 16) & 0xFF),
|
||||
((i32 >> 24) & 0xFF)];
|
||||
|
||||
}
|
||||
|
||||
function array_to_i32(array) {
|
||||
@@ -314,6 +357,7 @@ var StackFrame = function(file, f) {
|
||||
this.stack = [];
|
||||
this.pc = 0;
|
||||
this.program = f.code;
|
||||
this.function_id = f.function_id;
|
||||
this.variables = [];
|
||||
for (var i = 0; i < f.num_vars; i++)
|
||||
this.variables.push(0);
|
||||
@@ -334,13 +378,15 @@ var ProgramState = function(parsed_file, callback_dict) {
|
||||
try {
|
||||
this.natives[i] = callback_dict[i];
|
||||
} catch (key_not_found) {
|
||||
this.natives[i] = function (arg) {
|
||||
this.natives[i] = function (arg) {
|
||||
console.log("Native function " + name + " called, ran method stub.");
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.breakpoints = [];
|
||||
|
||||
// Memory is just a big array of bytes, right?
|
||||
// "Allocation" is appending onto this array
|
||||
// A pointer to memory is an index into this array.
|
||||
@@ -409,7 +455,7 @@ ProgramState.prototype.step = function() {
|
||||
case op.BIPUSH:
|
||||
this.frame.pc += 2;
|
||||
var val = this.frame.program[this.frame.pc-1];
|
||||
|
||||
|
||||
// Do sign extension if necessary
|
||||
if ((val & 0x80) != 0)
|
||||
val = -0x80 + (val & 0x7F);
|
||||
@@ -611,9 +657,9 @@ ProgramState.prototype.step = function() {
|
||||
};
|
||||
console.log("Unknown native function index " + f.function_table_index);
|
||||
}
|
||||
log("Calling native function with index " + index + " with arguments " +
|
||||
log("Calling native function with index " + index + " with arguments " +
|
||||
arg_array);
|
||||
this.push(native_function(arg_array));
|
||||
this.push(native_function(arg_array, this));
|
||||
break;
|
||||
|
||||
// Memory allocation operations:
|
||||
@@ -623,7 +669,7 @@ ProgramState.prototype.step = function() {
|
||||
var address = this.heap.length;
|
||||
|
||||
for (var i = 0; i < size; i++) this.heap.push(0);
|
||||
|
||||
|
||||
this.push(address);
|
||||
this.frame.pc += 2;
|
||||
break;
|
||||
@@ -636,7 +682,7 @@ ProgramState.prototype.step = function() {
|
||||
|
||||
this.heap.push(num_elements);
|
||||
this.heap.push(size);
|
||||
|
||||
|
||||
for (var i = 0; i < num_elements; i++) {
|
||||
for (var j = 0; j < size; j++)
|
||||
this.heap.push(0);
|
||||
@@ -742,8 +788,11 @@ ProgramState.prototype.step = function() {
|
||||
}
|
||||
}
|
||||
|
||||
// Takes in a parsed .bc0 file and runs it
|
||||
function execute(file, callbacks, v) {
|
||||
ProgramState.prototype.set_breakpoint = function(function_index, opcode_index) {
|
||||
this.breakpoints.push([function_index, opcode_index]);
|
||||
}
|
||||
|
||||
function initialize_vm(file, callbacks, v) {
|
||||
verbose = typeof v !== 'undefined' ? v : true;
|
||||
log("Initializing with file " + file);
|
||||
|
||||
@@ -752,29 +801,48 @@ function execute(file, callbacks, v) {
|
||||
if (verbose) log(file);
|
||||
|
||||
log("Beginning execution");
|
||||
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
function run_vm(vm) {
|
||||
while (true) {
|
||||
var val = state.step();
|
||||
for (breakpoint in vm.breakpoints) {
|
||||
if (vm.frame.function_id == breakpoint[0] &&
|
||||
vm.frame.pc == breakpoint[1]) {
|
||||
console.log("Breakpoint reached!");
|
||||
return vm;
|
||||
}
|
||||
}
|
||||
|
||||
var val = vm.step();
|
||||
if (val !== undefined) return val;
|
||||
|
||||
if (verbose) {
|
||||
console.log("Machine state:");
|
||||
console.log("Machine vm:");
|
||||
console.log(" Current Stack Frame:");
|
||||
console.log(" Stack: " + state.frame.stack);
|
||||
console.log(" PC: " + state.frame.pc);
|
||||
console.log(" Vars: " + state.frame.variables);
|
||||
// console.log(" Code: " + state.frame.program);
|
||||
console.log(" Heap: " + state.heap);
|
||||
console.log(" Stack: " + vm.frame.stack);
|
||||
console.log(" PC: " + vm.frame.pc);
|
||||
console.log(" Vars: " + vm.frame.variables);
|
||||
// console.log(" Code: " + vm.frame.program);
|
||||
console.log(" Heap: " + vm.heap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (at_breakpoint) {
|
||||
// save state (maybe in a global in this file?)
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
// Takes in a parsed .bc0 file and runs it
|
||||
function execute(file, callbacks, v) {
|
||||
var state = initialize_vm(file, callbacks, v);
|
||||
return run_vm(state);
|
||||
}
|
||||
|
||||
exports.execute = execute;
|
||||
exports.initialize_vm = initialize_vm;
|
||||
exports.run_vm = run_vm;
|
||||
|
||||
},{"./opcodes":6}],5:[function(require,module,exports){
|
||||
parser = require("./bytecode-parser");
|
||||
@@ -795,7 +863,7 @@ function print(arg) {
|
||||
$("#output").append(arg);
|
||||
}
|
||||
|
||||
callbacks = {};
|
||||
callbacks = c0ffi.default_callbacks;
|
||||
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
||||
print(args[0]);
|
||||
print("<br />");
|
||||
@@ -812,6 +880,9 @@ console.log(callbacks);
|
||||
|
||||
$("#run").click(function() {
|
||||
var input = $("#bytecode").html().replace(/(\r\n|\n|\r)/gm,"");
|
||||
|
||||
$("#output").text("");
|
||||
|
||||
var file = parser.parse($("#bytecode").text());
|
||||
print("<br />");
|
||||
print(c0vm.execute(file, callbacks));
|
||||
@@ -926,4 +997,4 @@ exports.lookup_table = {
|
||||
|
||||
},{}],7:[function(require,module,exports){
|
||||
|
||||
},{}]},{},[4,2,1,6,5]);
|
||||
},{}]},{},[4,2,1,6,3,5]);
|
||||
|
||||
110
src/c0ffi.js
110
src/c0ffi.js
@@ -1,110 +0,0 @@
|
||||
exports.NATIVE_FADD = 0
|
||||
exports.NATIVE_FDIV = 1
|
||||
exports.NATIVE_FLESS = 2
|
||||
exports.NATIVE_FMUL = 3
|
||||
exports.NATIVE_FSUB = 4
|
||||
exports.NATIVE_FTOI = 5
|
||||
exports.NATIVE_ITOF = 6
|
||||
exports.NATIVE_PRINT_FPT = 7
|
||||
exports.NATIVE_PRINT_HEX = 8
|
||||
exports.NATIVE_PRINT_INT = 9
|
||||
|
||||
/* args */
|
||||
exports.NATIVE_ARGS_FLAG = 10
|
||||
exports.NATIVE_ARGS_INT = 11
|
||||
exports.NATIVE_ARGS_PARSE = 12
|
||||
exports.NATIVE_ARGS_STRING = 13
|
||||
|
||||
/* conio */
|
||||
exports.NATIVE_EOF = 14
|
||||
exports.NATIVE_FLUSH = 15
|
||||
exports.NATIVE_PRINT = 16
|
||||
exports.NATIVE_PRINTBOOL = 17
|
||||
exports.NATIVE_PRINTCHAR = 18
|
||||
exports.NATIVE_PRINTINT = 19
|
||||
exports.NATIVE_PRINTLN = 20
|
||||
exports.NATIVE_READLINE = 21
|
||||
|
||||
/* curses */
|
||||
exports.NATIVE_C_ADDCH = 22
|
||||
exports.NATIVE_C_CBREAK = 23
|
||||
exports.NATIVE_C_CURS_SET = 24
|
||||
exports.NATIVE_C_DELCH = 25
|
||||
exports.NATIVE_C_ENDWIN = 26
|
||||
exports.NATIVE_C_ERASE = 27
|
||||
exports.NATIVE_C_GETCH = 28
|
||||
exports.NATIVE_C_INITSCR = 29
|
||||
exports.NATIVE_C_KEYPAD = 30
|
||||
exports.NATIVE_C_MOVE = 31
|
||||
exports.NATIVE_C_NOECHO = 32
|
||||
exports.NATIVE_C_REFRESH = 33
|
||||
exports.NATIVE_C_SUBWIN = 34
|
||||
exports.NATIVE_C_WADDCH = 35
|
||||
exports.NATIVE_C_WADDSTR = 36
|
||||
exports.NATIVE_C_WCLEAR = 37
|
||||
exports.NATIVE_C_WERASE = 38
|
||||
exports.NATIVE_C_WMOVE = 39
|
||||
exports.NATIVE_C_WREFRESH = 40
|
||||
exports.NATIVE_C_WSTANDEND = 41
|
||||
exports.NATIVE_C_WSTANDOUT = 42
|
||||
exports.NATIVE_CC_GETBEGX = 43
|
||||
exports.NATIVE_CC_GETBEGY = 44
|
||||
exports.NATIVE_CC_GETMAXX = 45
|
||||
exports.NATIVE_CC_GETMAXY = 46
|
||||
exports.NATIVE_CC_GETX = 47
|
||||
exports.NATIVE_CC_GETY = 48
|
||||
exports.NATIVE_CC_HIGHLIGHT = 49
|
||||
exports.NATIVE_CC_KEY_IS_BACKSPACE = 50
|
||||
exports.NATIVE_CC_KEY_IS_DOWN = 51
|
||||
exports.NATIVE_CC_KEY_IS_ENTER = 52
|
||||
exports.NATIVE_CC_KEY_IS_LEFT = 53
|
||||
exports.NATIVE_CC_KEY_IS_RIGHT = 54
|
||||
exports.NATIVE_CC_KEY_IS_UP = 55
|
||||
exports.NATIVE_CC_WBOLDOFF = 56
|
||||
exports.NATIVE_CC_WBOLDON = 57
|
||||
exports.NATIVE_CC_WDIMOFF = 58
|
||||
exports.NATIVE_CC_WDIMON = 59
|
||||
exports.NATIVE_CC_WREVERSEOFF = 60
|
||||
exports.NATIVE_CC_WREVERSEON = 61
|
||||
exports.NATIVE_CC_WUNDEROFF = 62
|
||||
exports.NATIVE_CC_WUNDERON = 63
|
||||
|
||||
/* file */
|
||||
exports.NATIVE_FILE_CLOSE = 64
|
||||
exports.NATIVE_FILE_CLOSED = 65
|
||||
exports.NATIVE_FILE_EOF = 66
|
||||
exports.NATIVE_FILE_READ = 67
|
||||
exports.NATIVE_FILE_READLINE = 68
|
||||
|
||||
/* img */
|
||||
exports.NATIVE_IMAGE_CLONE = 69
|
||||
exports.NATIVE_IMAGE_CREATE = 70
|
||||
exports.NATIVE_IMAGE_DATA = 71
|
||||
exports.NATIVE_IMAGE_DESTROY = 72
|
||||
exports.NATIVE_IMAGE_HEIGHT = 73
|
||||
exports.NATIVE_IMAGE_LOAD = 74
|
||||
exports.NATIVE_IMAGE_SAVE = 75
|
||||
exports.NATIVE_IMAGE_SUBIMAGE = 76
|
||||
exports.NATIVE_IMAGE_WIDTH = 77
|
||||
|
||||
/* parse */
|
||||
exports.NATIVE_PARSE_BOOL = 78
|
||||
exports.NATIVE_PARSE_INT = 79
|
||||
|
||||
/* string */
|
||||
exports.NATIVE_CHAR_CHR = 80
|
||||
exports.NATIVE_CHAR_ORD = 81
|
||||
exports.NATIVE_STRING_CHARAT = 82
|
||||
exports.NATIVE_STRING_COMPARE = 83
|
||||
exports.NATIVE_STRING_EQUAL = 84
|
||||
exports.NATIVE_STRING_FROM_CHARARRAY = 85
|
||||
exports.NATIVE_STRING_FROMBOOL = 86
|
||||
exports.NATIVE_STRING_FROMCHAR = 87
|
||||
exports.NATIVE_STRING_FROMINT = 88
|
||||
exports.NATIVE_STRING_JOIN = 89
|
||||
exports.NATIVE_STRING_LENGTH = 90
|
||||
exports.NATIVE_STRING_SUB = 91
|
||||
exports.NATIVE_STRING_TERMINATED = 92
|
||||
exports.NATIVE_STRING_TO_CHARARRAY = 93
|
||||
exports.NATIVE_STRING_TOLOWER = 94
|
||||
|
||||
@@ -2,7 +2,8 @@ parser = require("../src/bytecode-parser.js");
|
||||
c0vm = require("../src/c0vm.js");
|
||||
c0ffi = require("../src/c0ffi.js");
|
||||
|
||||
var callbacks = {}
|
||||
var callbacks = c0ffi.default_callbacks;
|
||||
console.log("Initial callbacks: " + callbacks[c0ffi.NATIVE_STRING_LENGTH](["hi"]));
|
||||
var printout = "";
|
||||
|
||||
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
||||
@@ -20,30 +21,6 @@ callbacks[c0ffi.NATIVE_PRINTLN] = function(args) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
callbacks[c0ffi.NATIVE_STRING_LENGTH] = function(args) {
|
||||
return args[0].length;
|
||||
}
|
||||
|
||||
callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args) {
|
||||
return args[0];
|
||||
}
|
||||
|
||||
callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args) {
|
||||
console.log("string_from_chararray: " + args);
|
||||
return args[0];
|
||||
}
|
||||
|
||||
callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) {
|
||||
return String.fromCharCode(args[0]);
|
||||
}
|
||||
|
||||
callbacks[c0ffi.NATIVE_CHAR_ORD] = function(args) {
|
||||
console.log("native_car_ord: " + args);
|
||||
if (typeof args[0] == "string")
|
||||
return args[0].charCodeAt(0);
|
||||
return args[0];
|
||||
}
|
||||
|
||||
function doTest(filename, expected_result) {
|
||||
return function(test) {
|
||||
var result = c0vm.execute(parser.parse(filename), callbacks, false);
|
||||
|
||||
@@ -47,15 +47,14 @@ block script
|
||||
|
||||
function compile() {
|
||||
var code = encodeURIComponent($("#inputCode").val());
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
async: true,
|
||||
url: "http://www.contrib.andrew.cmu.edu/~amgutier/cc0.cgi",
|
||||
dataType: "jsonp",
|
||||
dataType: "json",
|
||||
data: {
|
||||
data: code
|
||||
},
|
||||
jsonpCallback: "callback"
|
||||
success: callback
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user