-
Notifications
You must be signed in to change notification settings - Fork 3
/
bf.jsm
41 lines (35 loc) · 1.19 KB
/
bf.jsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/*jshint expr: true, es5: true, esnext: true */
/*global module: false */
module.version = 1.6;
module.MAX_LOOP_TIMES = 1000;
module.input = "You know it's rude to stare, right?";
// http://code.google.com/p/jslibs/wiki/JavascriptTips#Brainfuck_interpreter
module.cmd_bf = function cmd_bf(e) {
var code = e.args;
if (!code) {
e.reply("Interpret some Brainfuck. bf <code>");
return true;
}
var out = "", loopIn = [], loopOut = [];
for (var i = 0, arr = []; i < code.length; i++) {
if (code[i] == "[")
arr.push(i);
else if (code[i] == "]")
loopOut[loopIn[i] = arr.pop()] = i;
}
for (var cp = 0, dp = 0, l = this.MAX_LOOP_TIMES, ip = 0, m = {}; cp < code.length && l > 0; cp++, l--) {
switch (code[cp]) {
case ">": dp++; break;
case "<": dp--; break;
case "+": m[dp] = ((m[dp]||0)+1)&255; break;
case "-": m[dp] = ((m[dp]||0)-1)&255; break;
case ".": out += String.fromCharCode(m[dp]); break;
case ",": m[dp] = this.input.charCodeAt(ip++)||0; break;
case "[": m[dp]||(cp=loopOut[cp]); break;
case "]": cp = loopIn[cp]-1; break;
}
}
e.reply(out.replace("\n", " ", "g"));
return true;
};