-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.pl
54 lines (42 loc) · 1.1 KB
/
replace.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
use strict;
use warnings;
sub interpreter {
my $code = shift;
my $debug = shift;
my $separator = shift;
my $safe = shift;
my $flags = shift;
my @lines = split /\n/, $code;
my $i = 0;
if ($debug) {
print "$i:\n";
print "$code\n";
}
while($i < scalar(@lines)){
if ($lines[$i++] =~ /(.*)$separator(.*)/) {
my $find = $1;
my $replace = $2;
if ($safe) {
$code =~ s/$find/$replace/gm;
} else {
if ($debug) {
print "eval: \$code =~ s/$find/$replace/$flags\n";
}
eval("\$code =~ s/$find/$replace/$flags");
}
@lines = split /\n/, $code;
if($debug) {
print "$i: $find $separator $replace\n";
print "$code \n"
}
}
}
if ($debug) {
print "end\n\nresult:\n"
}
return $code;
}
#print interpreter("(1*)\\+(1*)/\$1\$2\n(.|\\s)+/111+1", 1, "/", 0, "gm"); #not working addition
#print interpreter ("(1*)\\+(1*)/\$1\$2\n.*\\n/\n1111+1", 1, "/", 0, "gm"); #working addition
print interpreter ("(\\n*)?(.*)(\\n*)?/\$2\\n\$2", 1, "/", 0, "gm") . "\n"; #infinite loop
#print interpreter ("\n\\n(.*)\\n(\\d*)/\\n\$2\$2\\n\$1\\n\$2\$2\n1", 1, "/", 0, "gm") . "\n";