-
Notifications
You must be signed in to change notification settings - Fork 255
/
bf.js
156 lines (133 loc) · 3.5 KB
/
bf.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const assert = require("assert");
const fs = require("fs");
const net = require('net');
class StringIterator {
constructor(str) {
this.str = str;
this.current = 0;
this.last = str.length - 1;
}
done() {
return this.current > this.last;
}
next() {
if (this.current > this.last)
throw StopIteration;
else
return this.str[this.current++];
}
}
class Tape {
constructor() {
this.pos = 0;
this.tape = [0];
}
inc(x) {
this.tape[this.pos] += x;
}
move(x) {
this.pos += x;
while (this.pos >= this.tape.length) this.tape.push(0);
}
get() {
return this.tape[this.pos];
}
}
class Printer {
constructor(quiet) {
this.sum1 = 0;
this.sum2 = 0;
this.quiet = quiet;
}
print(n) {
if (this.quiet) {
this.sum1 = (this.sum1 + n) % 255;
this.sum2 = (this.sum2 + this.sum1) % 255;
} else {
process.stdout.write(String.fromCharCode(n));
}
}
get checksum() {
return (this.sum2 << 8) | this.sum1;
}
}
const INC = 1;
const MOVE = 2;
const LOOP = 3;
const PRINT = 4;
class Op {
constructor(op, v) {
this.op = op;
this.v = v;
}
}
class Brainfuck {
constructor(text, p) {
this.ops = this.parse(new StringIterator(text));
this.p = p;
}
parse(iterator) {
var res = [];
while (!iterator.done()) {
switch(iterator.next()) {
case '+': res.push(new Op(INC, 1)); break;
case '-': res.push(new Op(INC, -1)); break;
case '>': res.push(new Op(MOVE, 1)); break;
case '<': res.push(new Op(MOVE, -1)); break;
case '.': res.push(new Op(PRINT, 0)); break;
case '[': res.push(new Op(LOOP, this.parse(iterator))); break;
case ']': return res;
}
}
return res;
}
_run(ops, tape) {
for (var i = 0; i < ops.length; i++) {
const op = ops[i];
switch(op.op) {
case INC: tape.inc(op.v); break;
case MOVE: tape.move(op.v); break;
case LOOP: while (tape.get() > 0) this._run(op.v, tape); break;
case PRINT: this.p.print(tape.get()); break;
}
}
}
run() {
this._run(this.ops, new Tape());
}
}
function notify(msg) {
return new Promise(resolve => {
const client = net.connect(9001, 'localhost', () => {
client.end(msg, 'utf8', () => {
client.destroy();
resolve();
});
}).on('error', resolve);
});
}
function verify() {
const text = `++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.`;
const p_left= new Printer(true);
new Brainfuck(text, p_left).run();
const left = p_left.checksum;
const p_right = new Printer(true);
for (const c of "Hello World!\n") {
p_right.print(c.charCodeAt(0));
}
const right = p_right.checksum;
assert.deepStrictEqual(left, right);
}
(async function() {
verify();
const text = fs.readFileSync(process.argv[2].toString()).toString();
const p = new Printer(process.env.QUIET);
await notify(`Node.js\t${process.pid}`);
new Brainfuck(text, p).run();
await notify('stop');
if (p.quiet) {
console.log(`Output checksum: ${p.checksum}`)
}
})();