-
Notifications
You must be signed in to change notification settings - Fork 255
/
bf.pl
162 lines (136 loc) · 3.58 KB
/
bf.pl
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
157
158
159
160
161
162
use v5.38;
use warnings;
no feature qw(indirect);
use feature qw(signatures);
no warnings qw(experimental);
use Socket;
package Op;
sub new ($class, $op, $val = undef) {
my $self = {
op => $op,
val => $val,
};
bless $self, $class;
return $self;
}
package Tape;
sub new ($class) {
my $self = {
tape => [0],
pos => 0,
};
bless $self, $class;
return $self;
}
sub get ($self) {
return $self->{tape}[$self->{pos}];
}
sub inc ($self, $x) {
$self->{tape}[$self->{pos}] += $x;
}
sub move ($self, $x) {
$self->{pos} += $x;
my $missing = $self->{pos} - @{$self->{tape}} + 1;
for (my $i = 0; $i < $missing; $i++) {
push @{$self->{tape}}, 0;
}
}
package Printer;
sub new ($class, $quiet) {
my $self = {
sum1 => 0,
sum2 => 0,
quiet => $quiet
};
bless $self, $class;
return $self;
}
sub print ($self, $n) {
if ($self->{quiet}) {
$self->{sum1} = ($self->{sum1} + $n) % 255;
$self->{sum2} = ($self->{sum2} + $self->{sum1}) % 255;
} else {
printf "%c", $n;
}
}
sub checksum ($self) {
return ($self->{sum2} << 8) | $self->{sum1};
}
package Main;
my $INC = 1;
my $MOVE = 2;
my $PRINT = 3;
my $LOOP = 4;
sub parse ($source, $i = 0) {
my $repr = [];
for (; $i < @{$source}; $i++) {
my $c = $source->[$i];
if ($c eq '+') { push @$repr, Op->new($INC, 1); }
elsif ($c eq '-') { push @$repr, Op->new($INC, -1); }
elsif ($c eq '>') { push @$repr, Op->new($MOVE, 1); }
elsif ($c eq '<') { push @$repr, Op->new($MOVE, -1); }
elsif ($c eq '.') { push @$repr, Op->new($PRINT); }
elsif ($c eq '[') {
my ($parsed_loop, $new_i) = parse($source, $i + 1);
$i = $new_i;
push @$repr, Op->new($LOOP, $parsed_loop);
}
elsif ($c eq ']') { last; }
}
return ($repr, $i);
}
sub run ($parsed, $tape, $p) {
foreach my $op (@$parsed) {
my $op_type = $op->{op};
if ($op_type == $INC) { $tape->inc($op->{val}); }
elsif ($op_type == $MOVE) { $tape->move($op->{val}); }
elsif ($op_type == $PRINT) { $p->print($tape->get()); }
elsif ($op_type == $LOOP) {
while ($tape->get() > 0) {
run($op->{val}, $tape, $p);
}
}
}
}
sub notify ($msg) {
socket(my $socket, Socket::PF_INET, Socket::SOCK_STREAM, (getprotobyname('tcp'))[2]);
if (connect($socket, Socket::pack_sockaddr_in(9001, Socket::inet_aton('localhost')))) {
print $socket $msg;
}
close($socket);
}
sub verify {
my $text = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
my $p_left = Printer->new(1);
my @source = split(//, $text);
my ($parsed, $n) = parse(\@source);
run($parsed, Tape->new(), $p_left);
my $left = $p_left->checksum;
my $p_right = Printer->new(1);
foreach my $c (split //, "Hello World!\n") {
$p_right->print(ord($c));
}
my $right = $p_right->checksum;
if ($left != $right) {
print STDERR "${left} != ${right}\n";
exit(1);
}
}
if ($0 eq __FILE__) {
verify;
open (my $fh, "<", shift) or die $!;
undef $/;
$| = 1;
read $fh, my $text, -s $fh;
close($fh);
my $p = Printer->new($ENV{'QUIET'});
notify("Perl\t". $$);
my @source = split(//, $text);
my ($parsed, $n) = parse(\@source);
run($parsed, Tape->new(), $p);
notify("stop");
if ($p->{quiet}) {
printf("Output checksum: %d\n", $p->checksum);
}
}