changed because Mitchell broke things
This commit is contained in:
@@ -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)
|
all: $(FILES)
|
||||||
browserify $(FILES) -o vm.js
|
browserify $(FILES) -o vm.js
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f vm.js
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ function print(arg) {
|
|||||||
$("#output").append(arg);
|
$("#output").append(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
callbacks = {};
|
callbacks = c0ffi.default_callbacks;
|
||||||
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
||||||
print(args[0]);
|
print(args[0]);
|
||||||
print("<br />");
|
print("<br />");
|
||||||
@@ -33,6 +33,9 @@ console.log(callbacks);
|
|||||||
|
|
||||||
$("#run").click(function() {
|
$("#run").click(function() {
|
||||||
var input = $("#bytecode").html().replace(/(\r\n|\n|\r)/gm,"");
|
var input = $("#bytecode").html().replace(/(\r\n|\n|\r)/gm,"");
|
||||||
|
|
||||||
|
$("#output").text("");
|
||||||
|
|
||||||
var file = parser.parse($("#bytecode").text());
|
var file = parser.parse($("#bytecode").text());
|
||||||
print("<br />");
|
print("<br />");
|
||||||
print(c0vm.execute(file, callbacks));
|
print(c0vm.execute(file, callbacks));
|
||||||
|
|||||||
@@ -299,7 +299,6 @@ function i32_to_array(i32) {
|
|||||||
((i32 >> 8) & 0xFF),
|
((i32 >> 8) & 0xFF),
|
||||||
((i32 >> 16) & 0xFF),
|
((i32 >> 16) & 0xFF),
|
||||||
((i32 >> 24) & 0xFF)];
|
((i32 >> 24) & 0xFF)];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function array_to_i32(array) {
|
function array_to_i32(array) {
|
||||||
@@ -334,13 +333,13 @@ var ProgramState = function(parsed_file, callback_dict) {
|
|||||||
try {
|
try {
|
||||||
this.natives[i] = callback_dict[i];
|
this.natives[i] = callback_dict[i];
|
||||||
} catch (key_not_found) {
|
} catch (key_not_found) {
|
||||||
this.natives[i] = function (arg) {
|
this.natives[i] = function (arg) {
|
||||||
console.log("Native function " + name + " called, ran method stub.");
|
console.log("Native function " + name + " called, ran method stub.");
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memory is just a big array of bytes, right?
|
// Memory is just a big array of bytes, right?
|
||||||
// "Allocation" is appending onto this array
|
// "Allocation" is appending onto this array
|
||||||
// A pointer to memory is an index into this array.
|
// A pointer to memory is an index into this array.
|
||||||
@@ -409,7 +408,7 @@ ProgramState.prototype.step = function() {
|
|||||||
case op.BIPUSH:
|
case op.BIPUSH:
|
||||||
this.frame.pc += 2;
|
this.frame.pc += 2;
|
||||||
var val = this.frame.program[this.frame.pc-1];
|
var val = this.frame.program[this.frame.pc-1];
|
||||||
|
|
||||||
// Do sign extension if necessary
|
// Do sign extension if necessary
|
||||||
if ((val & 0x80) != 0)
|
if ((val & 0x80) != 0)
|
||||||
val = -0x80 + (val & 0x7F);
|
val = -0x80 + (val & 0x7F);
|
||||||
@@ -611,7 +610,7 @@ ProgramState.prototype.step = function() {
|
|||||||
};
|
};
|
||||||
console.log("Unknown native function index " + f.function_table_index);
|
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);
|
arg_array);
|
||||||
this.push(native_function(arg_array));
|
this.push(native_function(arg_array));
|
||||||
break;
|
break;
|
||||||
@@ -623,7 +622,7 @@ ProgramState.prototype.step = function() {
|
|||||||
var address = this.heap.length;
|
var address = this.heap.length;
|
||||||
|
|
||||||
for (var i = 0; i < size; i++) this.heap.push(0);
|
for (var i = 0; i < size; i++) this.heap.push(0);
|
||||||
|
|
||||||
this.push(address);
|
this.push(address);
|
||||||
this.frame.pc += 2;
|
this.frame.pc += 2;
|
||||||
break;
|
break;
|
||||||
@@ -636,7 +635,7 @@ ProgramState.prototype.step = function() {
|
|||||||
|
|
||||||
this.heap.push(num_elements);
|
this.heap.push(num_elements);
|
||||||
this.heap.push(size);
|
this.heap.push(size);
|
||||||
|
|
||||||
for (var i = 0; i < num_elements; i++) {
|
for (var i = 0; i < num_elements; i++) {
|
||||||
for (var j = 0; j < size; j++)
|
for (var j = 0; j < size; j++)
|
||||||
this.heap.push(0);
|
this.heap.push(0);
|
||||||
@@ -752,7 +751,7 @@ function execute(file, callbacks, v) {
|
|||||||
if (verbose) log(file);
|
if (verbose) log(file);
|
||||||
|
|
||||||
log("Beginning execution");
|
log("Beginning execution");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
var val = state.step();
|
var val = state.step();
|
||||||
if (val !== undefined) return val;
|
if (val !== undefined) return val;
|
||||||
@@ -771,7 +770,7 @@ function execute(file, callbacks, v) {
|
|||||||
// save state (maybe in a global in this file?)
|
// save state (maybe in a global in this file?)
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.execute = execute;
|
exports.execute = execute;
|
||||||
@@ -795,7 +794,7 @@ function print(arg) {
|
|||||||
$("#output").append(arg);
|
$("#output").append(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
callbacks = {};
|
callbacks = c0ffi.default_callbacks;
|
||||||
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
||||||
print(args[0]);
|
print(args[0]);
|
||||||
print("<br />");
|
print("<br />");
|
||||||
@@ -812,6 +811,9 @@ console.log(callbacks);
|
|||||||
|
|
||||||
$("#run").click(function() {
|
$("#run").click(function() {
|
||||||
var input = $("#bytecode").html().replace(/(\r\n|\n|\r)/gm,"");
|
var input = $("#bytecode").html().replace(/(\r\n|\n|\r)/gm,"");
|
||||||
|
|
||||||
|
$("#output").text("");
|
||||||
|
|
||||||
var file = parser.parse($("#bytecode").text());
|
var file = parser.parse($("#bytecode").text());
|
||||||
print("<br />");
|
print("<br />");
|
||||||
print(c0vm.execute(file, callbacks));
|
print(c0vm.execute(file, callbacks));
|
||||||
@@ -926,4 +928,4 @@ exports.lookup_table = {
|
|||||||
|
|
||||||
},{}],7:[function(require,module,exports){
|
},{}],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
|
|
||||||
|
|
||||||
@@ -47,9 +47,8 @@ block script
|
|||||||
|
|
||||||
function compile() {
|
function compile() {
|
||||||
var code = encodeURIComponent($("#inputCode").val());
|
var code = encodeURIComponent($("#inputCode").val());
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST",
|
type: "GET",
|
||||||
async: true,
|
async: true,
|
||||||
url: "http://www.contrib.andrew.cmu.edu/~amgutier/cc0.cgi",
|
url: "http://www.contrib.andrew.cmu.edu/~amgutier/cc0.cgi",
|
||||||
dataType: "jsonp",
|
dataType: "jsonp",
|
||||||
|
|||||||
Reference in New Issue
Block a user