Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
77
Task/Execute-Brain-/Jsish/execute-brain-.jsish
Normal file
77
Task/Execute-Brain-/Jsish/execute-brain-.jsish
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* javascript bf interpreter
|
||||
* by wenxichang@163.com
|
||||
*/
|
||||
|
||||
function execute(code)
|
||||
{
|
||||
var mem = new Array(30000);
|
||||
var sp = 10000;
|
||||
var opcode = new String(code);
|
||||
var oplen = opcode.length;
|
||||
var ip = 0;
|
||||
var loopstack = new Array();
|
||||
var output = "";
|
||||
|
||||
for (var i = 0; i < 30000; ++i) mem[i] = 0;
|
||||
|
||||
while (ip < oplen) {
|
||||
switch(opcode[ip]) {
|
||||
case '+':
|
||||
mem[sp]++;
|
||||
break;
|
||||
case '-':
|
||||
mem[sp]--;
|
||||
break;
|
||||
case '>':
|
||||
sp++;
|
||||
break;
|
||||
case '<':
|
||||
sp--;
|
||||
break;
|
||||
case '.':
|
||||
if (mem[sp] != 10 && mem[sp] != 13) {
|
||||
output = output + Util.fromCharCode(mem[sp]);
|
||||
} else {
|
||||
puts(output);
|
||||
output = "";
|
||||
}
|
||||
break;
|
||||
case ',':
|
||||
var s = console.input();
|
||||
if (!s) exit(0);
|
||||
|
||||
mem[sp] = s.charCodeAt(0);
|
||||
break;
|
||||
case '[':
|
||||
if (mem[sp]) {
|
||||
loopstack.push(ip);
|
||||
} else {
|
||||
for (var k = ip, j = 0; k < oplen; k++) {
|
||||
opcode[k] == '[' && j++;
|
||||
opcode[k] == ']' && j--;
|
||||
if (j == 0) break;
|
||||
}
|
||||
if (j == 0) ip = k;
|
||||
else {
|
||||
puts("Unmatched loop");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ']':
|
||||
ip = loopstack.pop() - 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ip++;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
if (Interp.conf('unitTest') > 0) execute('
|
||||
++++++++++[>+>+++>++++>+++++++ >++++++++>+++++++++>++++++++++>+++++++++
|
||||
++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<+
|
||||
+.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---.
|
||||
');
|
||||
Loading…
Add table
Add a link
Reference in a new issue