-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.js
109 lines (103 loc) · 3.01 KB
/
brainfuck.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function Brainfuck(input, size, max) {
if (max > Number.MAX_SAFE_INTEGER)
return new BigIntBrainfuck(input, size, max);
this.size = size;
this.max = max;
var sanitized = ShortenBrainfuck(input, 7),
parsed = [],
stacktrace = [],
result,
i, text, ltcount, gtcount;
while (sanitized) {
if (result = collapseValueChanges(sanitized)) {
sanitized = result[0];
parsed.push(result[1]);
} else if (result = collapseCellChanges(sanitized)) {
sanitized = result[0];
parsed.push(result[1]);
} else if (match = /^\[([^\[]]*)\]/.exec(sanitized)) {
text = match[1];
ltcount = gtcount = 0;
for (i = 0; i < text.length; i++)
switch (text[i]) {
case '<':
ltcount++;
break;
case '>':
gtcount++;
break;
}
if (ltcount === gtcount) {
loop = [];
while (text)
if (result = collapseValueChanges(text)) {
text = result[0];
loop.push(result[1]);
} else if (result = collapseCellChanges(text)) {
text = result[0];
loop.push(result[1]);
}
for (i = 0; i < loop.length; i++) {
//TODO: +=
}
//do stuff to loop
}
}
}
}
Brainfuck.prototype.calculateIndex = function() { this.index = this.index['%'](this.size);}
Brainfuck.prototype['+'] = function(n) { this.calculateIndex(); this[this.index] += n; }
Brainfuck.prototype['-'] = function(n) { this.calculateIndex(); this[this.index] -= n; }
Brainfuck.prototype['<'] = function(n) { this.index += n; }
Brainfuck.prototype['>'] = function(n) { this.index -= n; }
Brainfuck.prototype.getter = function(i) { return function() { return this[i]; } }
Brainfuck.collapseValueChanges = function(sanitized) {
var match;
if (match = /^[+-]/.exec(sanitized)) {
var n = 0;
for (var i = 0; i < match.length; i++)
n += match[i] === '+' ? 1 : -1;
sanitized = sanitized.slice(match.length);
if (n)
return [sanitized, n > 0 ? ['+', n] : ['-', -n]];
}
}
Brainfuck.collapseCellChanges = function(sanitized) {
var match;
if (match = /^[<>]/.exec(sanitized)) {
var n = 0;
for (var i = 0; i < match.length; i++)
n += match[i] === '>' ? 1 : -1;
sanitized = sanitized.slice(match.length);
if (n)
return [sanitized, n > 0 ? ['>', n] : ['<', -n]];
}
}
function BrainfuckCompiler(code) {
//
}
BrainfuckCompiler.prototype.variables = {
current: function() { return ''; },
this: function() { return ''; },
self: function() { return ''; } //0 offset
//TODO: optimize
}
function ShortenBrainfuck(code, config) {
var level = 0;
if (typeof config === 'number') {
level = config;
config = {};
} else {
config = config || {};
}
if (level || config.sanitize !== false || config.s !== false)
code = code.replace(/[^\+\-<>\.,\[\]]/g, '');
if (level > 1 || config.removeEmptyLoops !== false || config.r !== false)
while (/\[\]/.test(code))
code = code.replace(/\[\]/g, '');
if (level === 2 || level > 3 || config.removeEmptyLoops !== false || config.r !== false) {
while (/\+-|-\+|<>|></.test(code))
code = code.replace(/\+-|-\+|<>|></g, '');
}
//TODO: remove double brackets
}