Created
December 31, 2013 19:13
-
-
Save riston/8201003 to your computer and use it in GitHub Desktop.
Befunge code
This file contains hidden or 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
var Befunge = function () { | |
var WIDTH = 80, | |
HEIGHT = 25, | |
x = 0, | |
y = 0, | |
output = [], // This is array for storing output | |
stack = [], | |
matrix; | |
function _preAllocateArray (width, height) { | |
var array = new Array(width); | |
for (var i = 0; i < width; i++) { | |
array[i] = new Array(height); | |
} | |
return array; | |
} | |
function init() { | |
x = 0; | |
y = 0; | |
matrix = _preAllocateArray(WIDTH, HEIGHT); | |
} | |
function addition () { | |
console.log("addition called"); | |
var a = stack.pop(), | |
b = stack.pop(), | |
result = a + b; | |
stack.push(result); | |
return result; | |
} | |
function subtraction () { | |
var a = stack.pop(), | |
b = stack.pop(), | |
result = b - a; | |
stack.push(result); | |
return result; | |
} | |
function multiplication () { | |
var a = stack.pop(), | |
b = stack.pop(), | |
result = a * b; | |
stack.push(result); | |
return result; | |
} | |
function division () { | |
var a = stack.pop(), | |
b = stack.pop(), | |
result; | |
if (a === 0) { | |
result = 0; | |
} else { | |
result = Math.floor(b / a); | |
} | |
stack.push(result); | |
return result; | |
} | |
function modulo () { | |
var a = stack.pop(), | |
b = stack.pop(), | |
result; | |
if (a === 0) { | |
result = 0; | |
} else { | |
result = b % a; | |
} | |
stack.push(result); | |
return result; | |
} | |
function logicalNot () { | |
var a = stack.pop(); | |
result = (a === 0) ? 1 : 0; | |
stack.push(result); | |
return result; | |
} | |
function greaterThan () { | |
var a = stack.pop(), | |
b = stack.pop(), | |
result; | |
result = (b > a) ? 1 : 0; | |
stack.push(result); | |
return result; | |
} | |
function moveRight () { | |
console.log('Move right'); | |
} | |
function moveLeft () { | |
console.log('Move left'); | |
} | |
function moveUp () { | |
console.log('Move up'); | |
} | |
function moveDown () { | |
console.log('Move down'); | |
} | |
function match (instruction) { | |
var instructions = { | |
'+': addition, | |
'-': subtraction, | |
'*': multiplication, | |
'/': division, | |
'%': modulo, | |
'!': logicalNot, | |
'`': greaterThan, | |
'>': moveRight, | |
'<': moveLeft, | |
'v': moveDown, | |
'^': moveUp | |
} | |
if (instruction in instructions) { | |
// Call the instruction | |
instructions[instruction](); | |
} else if (parseInt(instruction, 10) !== NaN) { | |
stack.push(parseInt(instruction, 10)); | |
} else { | |
// Unknown instruction, push it to stack | |
stack.push(instruction); | |
} | |
} | |
function interpret (code) { | |
var lines = code.split('\n'), | |
instructions, | |
instruction; | |
init(); | |
for (var x = 0; x < lines.length; x++) | |
{ | |
instructions = lines[x].split(''); | |
for (var y = 0; y < instructions.length; y++) | |
{ | |
instruction = instructions[y]; | |
matrix[x][y] = instruction; | |
match(instruction); | |
} | |
} | |
_print(); | |
console.log('Stack', stack); | |
} | |
function _print() { | |
var str = ''; | |
for (var x = 0; x < matrix.length; x++) { | |
for (var y = 0; y < matrix[0].length; y++) { | |
if (matrix[x][y] !== undefined) { | |
str += matrix[x][y]; | |
} else { | |
str += '.'; | |
} | |
} | |
str += '\n'; | |
} | |
console.log(str); | |
} | |
return { | |
"interpret": interpret | |
} | |
}(); | |
Befunge.interpret("34+2*4-2/3%0!\n+@\n><"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment