BF interpreter on perl

 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
#!/usr/bin/perl
open F,shift;
@code=grep{/[+-\.,\[\]><]/}split'',<F>;
for(my$_=0;$_<@code;++$_){
$cpu[$i]++if$code[$_]eq'+';
$cpu[$i]--if$code[$_]eq'-';
$i--if$code[$_]eq'<';
$i++if$code[$_]eq'>';
print chr$cpu[$i]if$code[$_]eq'.';
$cpu[$i]=ord<STDIN>if$code[$_]eq',';
if($code[$_]eq'['){
if(!$cpu[$i]){
++$brc;
while($brc){
++$_;
$brc++if$code[$_]eq'[';
$brc--if$code[$_]eq']';
}
}else{
next;
}
}elsif($code[$_]eq']'){
if(!$cpu[$i]){
next;
}else{
$brc++if$code[$_]eq']';
while($brc){
--$_;
$brc--if$code[$_]eq'[';
$brc++if$code[$_]eq']';
}
--$_;
}
}
}