Skip to content

Instantly share code, notes, and snippets.

Created August 7, 2016 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/29b4ef79151b661b06e467c4b52564e2 to your computer and use it in GitHub Desktop.
Save anonymous/29b4ef79151b661b06e467c4b52564e2 to your computer and use it in GitHub Desktop.
Very messy implementation of Pipefuck
const fs = require('fs');
var code = fs.readFileSync(process.argv[2]).toString();
var tokens = code.split("\n").map(x=>x.split(''));
var tape = [0];
var ptr = 0;
var quickdraw = true;
var outt = "";
var running = true;
var x = 0, y = 0;
var direction = 0;
var debug = false;
var move = () => {
switch(direction){
case 0:
x++;
break
case 1:
y++;
break
case 2:
x--;
break
case 3:
y--;
}
}
var repr = (a, b) => {
var out = "";
for (var y = 0; y < tokens.length; y++){
for (var x = 0; x < tokens[y].length; x++){
if (a===x && b===y){
out+="[" + tokens[y][x] + "]";
}else{
out+=" " + tokens[y][x] + " ";
}
}
out+="\n";
}
process.stdout.write("\n".repeat(100));
process.stdout.write("Debug information: ");
process.stdout.write("\n");
process.stdout.write(out)
process.stdout.write("\n");
process.stdout.write("Tape: " + tape.map((t,i)=>i==ptr?("[" +t+"]"):(" " + t + " ")).join(''))
process.stdout.write("\n");
process.stdout.write("X:" + a + " Y:" + b);
process.stdout.write('\n');
process.stdout.write("Programme output: \n");
process.stdout.write(outt.substring(outt.length-100));;
}
var findF = (tok) => {
var matches = [];
for (var i = tokens.length - 1; i >= 0; i--) {
for (var j = tokens[i].length - 1; j >= 0; j--) {
if (tokens[i][j] == tok){
if(i!=y||j!=x){
matches.push([j, i]);
}
}
}
}
return matches;
}
var step = 10;
var iv = ()=>{
var draw = true;
var token = tokens[y][x];
if(!running){
clearInterval(intervall);
return;
}
if(token===undefined){
running = false;
return;
}
draw = !("=|/\\".indexOf(token)>-1);
if(debug && (!quickdraw || draw)){
repr(x, y)
};
tape[ptr] = tape[ptr] || 0;
switch(token){
case "~":
move(); move();
return;
break;
case "+":
tape[ptr]++;
break;
case "*":
if(tape[ptr]==0){
move(); move();
return;
}
break;
case "-":
tape[ptr]--;
break;
case ">":
ptr++;
break;
case "<":
ptr--;
break
case "$":
if(tape[ptr]==0){
outt+="\n";
}else{
outt+=String.fromCharCode(tape[ptr]);
}
break;
case "#":
outt+=tape[ptr];
break;
case "/":
if (direction==1)direction=2;else
if (direction==0)direction=3;else
if (direction==2)direction=1;else
if (direction==3)direction=0;
break;
case "!":
direction+=2;
direction = direction % 4;
break;
case "\\":
if (direction==0)direction=1;else
if (direction==1)direction=0;else
if (direction==2)direction=3;else
if (direction==3)direction=2;
break;
case "|":
break;
case "=":
break;
default:
if(token != " " && token != "\n" && token != '' && token != '\t'){
var found = findF(token);
console.log(token)
var loc = found[Math.floor(Math.random()*found.length)];
x = loc[0];
y = loc[1];
break;
}
}
move();
};
style = 1; // 0 QUICK EXEC, 1 DEBUG INFO + SLOW PROCESS
quickdraw = false;
if(style==0){
debug = false;
mode = 0;
step = 0;
}else{
debug = true;
mode = 1;
step = 25;
}
step = 100;
if (mode == 0){
while(running){
iv();
}
}else{
var intervall = setInterval(iv, step);
}
if(!debug){
console.log("Final output: " + outt)
console.log("Tape-state: " + tape.map((t,i)=>i==ptr?("[" +t+"]"):(" " + t + " ")).join(''));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment