Created
September 26, 2014 02:58
-
-
Save iahu/25ae484d9b091ee73610 to your computer and use it in GitHub Desktop.
jsBefunge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Befunge(code) { | |
var output = ''; | |
var stack = []; | |
var codeArray = code.split('\n'); | |
var x = 0; | |
var y = 0; | |
var d = code.charAt(0); | |
var next = d; | |
var cmd = 'NORMAL'; | |
var loop = true; | |
var v; | |
while (loop) { | |
switch(d) { | |
case '>': | |
x += 1; | |
break; | |
case '<': | |
x -= 1; | |
break; | |
case '^': | |
y -= 1; | |
break; | |
case 'v': | |
y += 1; | |
break; | |
default: | |
d = '>'; | |
stack.push(next); | |
continue; | |
} | |
try { | |
next = codeArray[y][x]; | |
} catch(e) { | |
next = null; | |
break; | |
} | |
if (next === '"') { | |
cmd = cmd === 'STRING' ? 'NORMAL' : 'STRING'; | |
continue; | |
} | |
if (cmd === 'STRING') { | |
stack.push( next.charCodeAt(0) ); | |
continue; | |
} else { | |
switch ( next ) { | |
case '?': | |
next = '><^v'[Math.floor(Math.random() * 4)]; | |
break; | |
case '_': | |
d = +stack.pop() === 0 ? '>': '<'; | |
continue; | |
// break; | |
case '|': | |
d = +stack.pop() === 0 ? 'v': '^'; | |
continue; | |
// break; | |
} | |
if ( '<>^v'.indexOf(next) >= 0 ) { | |
d = next; | |
continue; | |
} else { | |
switch(next) { | |
case ':': | |
next = stack.pop(); | |
if ( typeof next === 'undefined' ) { | |
stack.push( 0 ); | |
} else { | |
stack.push(next); | |
stack.push(next); | |
} | |
continue; | |
case '.': | |
output += parseInt( stack.pop() ); | |
continue; | |
case ',': | |
output += String.fromCharCode( + stack.pop() ); | |
continue; | |
case '`': | |
next = stack.pop(); | |
stack.push( +(+next < +stack.pop()) ); | |
continue; | |
case '!': | |
next = !stack.pop(); | |
stack.push( +next ); | |
continue; | |
case '\\': | |
// Swap two values on top of the stack. | |
next = stack.pop(); | |
stack.unshift( stack.pop() ); | |
stack.push(next); | |
stack.push( stack.shift() ); | |
continue; | |
case '$': | |
stack.pop(); | |
continue; | |
case 'p': | |
stack.unshift( stack.pop() ); // y...vx | |
stack.unshift( stack.pop() ); // xy...v | |
v = String.fromCharCode(+stack.pop()); // xy... | |
stack.push( stack.shift() ); // y...x | |
next = codeArray[ +stack[0] ].split(''); // current line to array | |
next[+stack.pop()] = v; | |
codeArray[ +stack.shift() ] = next.join(''); | |
continue; | |
case 'g': | |
next = codeArray[ +stack.pop() ][ +stack.pop() ]; | |
stack.push( next.charCodeAt(0) ); | |
continue; | |
case '#': | |
x += {'<': -1, '>': 1}[d] || 0; | |
y += {'^': -1, 'v': 1}[d] || 0; | |
continue; | |
case ' ': | |
continue; | |
case '@': | |
loop = false; | |
break; | |
} | |
} | |
if ( next && '+-*/%'.indexOf(next) >= 0 ) { | |
stack.unshift( stack.pop() ); | |
next = parseInt( +eval(stack.pop() +next+ +stack.shift() ) || 0 ); | |
} | |
} | |
stack.push(next); | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment