-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpl2py.pl
executable file
·72 lines (53 loc) · 1.54 KB
/
pl2py.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
#!/usr/bin/env perl
use strict;
use warnings;
my $file;
if (not defined $ARGV[0]){
exit();
}else{
$file = $ARGV[0];
}
local $/=undef;
open FILE, $file;
my $string = <FILE>;
close FILE;
#$string =~ s/("\w*($\w*")/
#Classes
#$string =~ s/package\s+(\w+);/
#Functions
$string =~ s/sub\s+(\w+)\s*{\s+my\s+(.+)\s+=\s+\@_;/def $1$2:/g;
$string = block($string);
print $string;
sub block{
my ($string) = @_;
#Conditionals
$string =~ s/if\s*\((.+)\)\s*{/if $1:/g;
$string =~ s/}\s*else\s*{\s*\n/else:\n/g;
$string =~ s/elsif/elif/g;
#Arrays
#TO DO: NOT MAKE SWALLOW ALL
$string =~ s/\$(\w+){\$?((?:.|})+)}/$1\[$2\]/g;
#Loops
$string =~ s/foreach\s*\(keys\s+%{(.+)}\)\s*{/for item in $1:/g;
#Function calls
$string =~ s/\$(\w+)->{\$?(.+)}/$1.$2/g;
$string =~ s/\$(\w+)->(\w+\(.*\))/$1.$2/g;
#Operations
$string =~ s/\s+eq\s+/ == /g;
$string =~ s/\$(\w+)\s*\.=\s*(.*);/$1 = $1 + $2;/g;
$string =~ s/\s*\.\s*/ + /g;
$string =~ s/\$(\w+)\s*\+=\s*(.*);/$1 = $1 + $2;/g;
$string =~ s/\$(\w+)\s*\++\s*;/$1 = $1 + 1;/g;
$string =~ s/\$(\w+)\s*=~\s*\/(.*)\//re.match("$2", $1)/g;
#$string =~ s/->/./g;
#Variables
$string =~ s/my\s+\%(\w+)\s*=\s*(.*);/$1 = $2;/g;
$string =~ s/my\s+\@(\w+)\s*;/$1 = dict();/g;
$string =~ s/my\s+\$(\w+)\s*;/$1 = None;/g;
$string =~ s/my\s+\$(\w+)\s*=\s*(.*);/$1 = $2;/g;
$string =~ s/\$(\w+)/$1/g;
#Misc
$string =~ s/;//g;
$string =~ s/\n\s*}//g;
return $string;
}