forked from kostya/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bf.php
146 lines (123 loc) · 3.43 KB
/
bf.php
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
<?php
class StringIterator {
function __construct($str) {
$this->str = $str;
$this->current = 0;
$this->last = strlen($str) - 1;
}
function done() {
return $this->current > $this->last;
}
function next() {
if ($this->current > $this->last)
throw new Exception("StopIteration");
else
return $this->str[$this->current++];
}
}
class Tape {
function __construct() {
$this->pos = 0;
$this->tape = [0];
}
function inc($x) {
$this->tape[$this->pos] += $x;
}
function move($x) {
$this->pos += $x;
while ($this->pos >= count($this->tape)) $this->tape []= 0;
}
function get() {
return $this->tape[$this->pos];
}
}
class Printer {
function __construct($quiet) {
$this->sum1 = 0;
$this->sum2 = 0;
$this->quiet = $quiet;
}
function print($n) {
if ($this->quiet) {
$this->sum1 = ($this->sum1 + $n) % 255;
$this->sum2 = ($this->sum2 + $this->sum1) % 255;
} else {
echo chr($n);
}
}
function checksum() {
return ($this->sum2 << 8) | $this->sum1;
}
}
class Op {
function __construct($op, $v) {
$this->op = $op;
$this->v = $v;
}
}
class Brainfuck {
const INC = 1;
const MOVE = 2;
const LOOP = 3;
const PRINT = 4;
function __construct($text, $p) {
$this->ops = $this->parse(new StringIterator($text));
$this->p = $p;
}
function parse($iterator) {
$res = [];
while (!$iterator->done()) {
switch($iterator->next()) {
case '+': $res []= new Op(self::INC, 1); break;
case '-': $res []= new Op(self::INC, -1); break;
case '>': $res []= new Op(self::MOVE, 1); break;
case '<': $res []= new Op(self::MOVE, -1); break;
case '.': $res []= new Op(self::PRINT, 0); break;
case '[': $res []= new Op(self::LOOP, $this->parse($iterator)); break;
case ']': return $res;
}
}
return $res;
}
function _run($ops, $tape) {
foreach($ops as $op) {
switch($op->op) {
case self::INC: $tape->inc($op->v); break;
case self::MOVE: $tape->move($op->v); break;
case self::LOOP: while ($tape->get() > 0) $this->_run($op->v, $tape); break;
case self::PRINT: $this->p->print($tape->get()); break;
}
}
}
function run() {
$this->_run($this->ops, new Tape());
}
}
function notify($msg) {
$fp = stream_socket_client("tcp://localhost:9001", $errno, $errstr);
if (!$fp) die("$errstr ($errno)");
fwrite($fp, $msg);
fclose($fp);
}
function verify() {
$text = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
$p_left = new Printer(true);
(new Brainfuck($text, $p_left))->run();
$left = $p_left->checksum();
$p_right = new Printer(true);
foreach(str_split("Hello World!\n") as $c) {
$p_right->print(ord($c));
}
$right = $p_right->checksum();
assert($left == $right);
}
verify();
$text = file_get_contents($argv[1]);
$p = new Printer(@$_SERVER["QUIET"]);
notify("PHP\t" . getmypid());
(new Brainfuck($text, $p))->run();
notify('stop');
if ($p->quiet) {
echo "Output checksum: ".$p->checksum()."\n";
}