forked from passeriforma/Perl2Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperl2python.pl
executable file
·272 lines (229 loc) · 7.63 KB
/
perl2python.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/perl -w
use strict;
my $whitespaceCounter = 0; #global variable? Its outside the loop...
while (my $line = <>) {
#NOTE: Deal with semicolons on a line by line basis
# translate #! line
if ($line =~ /^#!/ && $. == 1) {
print "#!/usr/bin/python2.7 -u\n";
# Blank & comment lines (unchanged)
} elsif ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
print $line;
#print statement with newline
} elsif ($line =~ /^\s*print\s*"(.*)\\n"[\s;]*$/) {
my $printInput = $1;
if($printInput =~ /ARGV\[(.*)\]$/) { #that variable is ARGV[]
&whitespacePrinter($whitespaceCounter);
my $location = $1;
$location =~ s/\$//;
print "print sys.argv[$location + 1]\n"
} elsif ($printInput =~ /^(.*)\s*\$(.*)*$/) { #there is ONE $variable
$printInput =~ s/\$//; #removes variable signs
&whitespacePrinter($whitespaceCounter);
print "print $printInput\n";
} elsif ($printInput =~ /^(.*)\s*\@(.*)*$/) { #there is ONE @variable
$printInput =~ s/\@//;
&whitespacePrinter($whitespaceCounter);
print "print $printInput\n";
} else { #there is no variable (or many, which currently kill everything)
&whitespacePrinter($whitespaceCounter);
print "print \"$printInput\"\n";
}
#print statment with no newline
} elsif ($line =~ /^\s*print\s*"(.*)"[\s;]*$/) {
my $printInput = $1;
if ($printInput =~ /^\s*print\s*\$\_\s*;$/) { #special case for $_ (IN THE CASE OF PRINTING FROM THE COMMAND LINE?)
&whitespacePrinter($whitespaceCounter);
print "print line\n";
} elsif ($printInput =~ /^(.*)\s*\$(.*)*$/) { #there is ONE variable
&whitespacePrinter($whitespaceCounter);
$printInput =~ s/\$//; #removes variable signs
$printInput =~ s/\@//;
print "sys.stdout.write($printInput)\n";
} else { #there is no variable (or many, which currently kill everything)
&whitespacePrinter($whitespaceCounter);
print "sys.stdout.write(\"$printInput\")\n";
}
#break/continue
} elsif ($line =~ /^\s*last;$/) {
&whitespacePrinter($whitespaceCounter);
print "break\n";
} elsif ($line =~ /^\s*next;$/) {
&whitespacePrinter($whitespaceCounter);
print "continue\n";
#looping through every line in a FILE
} elsif ($line =~ /^\s*while\s*(.*)\<\>(.*)\s*(.*)\s*$/) {
&whitespacePrinter($whitespaceCounter);
print "import fileinput\n";
&whitespacePrinter($whitespaceCounter);
print "for line in fileinput.input():\n"
#looping through STDIN (while loop)
} elsif ($line =~ /^\s*while\s*(.*)\<STDIN\>(.*)\s*(.*)\s*$/) {
&whitespacePrinter($whitespaceCounter);
print "import sys\n";
&whitespacePrinter($whitespaceCounter);
print "for line in sys.stdin:\n";
#chomp from STDIN
} elsif ($line =~ /^\s*chomp\s*\$(.*)\s*;$/) {
&whitespacePrinter($whitespaceCounter);
print "$1 = sys.stdin.readlines()\n"
#split
} elsif ($line =~ /^\s*(.*)\s*=\s*split\(\/(.*)\/,\s*\$(.*)\)\s*;/) {
my $string = $3;
my $delineator = $2;
my $assignmentVariable = $1;
&whitespacePrinter($whitespaceCounter);
print "$assignmentVariable = $string.split(\"$delineator\")\n";
#join
} elsif ($line =~ /^\s*(.*)\s*=\s*join\(\'(.*)\'\,\s*(.*)\)\s*;$/) {
my $assignmentVariable = $1;
my $string = $3;
my $delineator = $2;
&whitespacePrinter($whitespaceCounter);
print "$assignmentVariable = '$delineator'.join([$string])";
#arithmetic operations
} elsif ($line =~ /^\s*[^\s]*\s*=(.*);$/) {
if ($line =~ /^\s*\@(.*)\s*=\s*(.*);$/) {#arrays are dealt with seperately
next;
} else {
&whitespacePrinter($whitespaceCounter);
&arithmeticLines($line);
}
# ++ and --
} elsif ($line =~ /^\s*(.*)\s*\+\+(.*);$/) {
# change ++ and -- to python equivalents
&whitespacePrinter($whitespaceCounter);
my $plusPlus = $1;
$plusPlus =~ s/\$//;
print "$plusPlus +=1\n";
} elsif ($line =~ /^\s*(.*)\s*\-\-(.*);$/) {
&whitespacePrinter($whitespaceCounter);
my $minusMinus = $1;
$minusMinus =~ s/\$//;
print "$minusMinus -= 1\n";
#for loops (If in C style then no direct comparison)?
#foreach (with ARGV) (super specific, could do with broadening in scope)
} elsif ($line =~ /^\s*foreach\s*\$(.*)\s*\((.*)\)\s*{\s*$/) {
#foreach $i (0..$#ARGV) becomes for i in xrange (len(sys.argv) - 1):
my $variableName = $1;
&whitespacePrinter($whitespaceCounter);
print "for $variableName in xrange (len(sys.argv) - 1):\n";
#while loops
} elsif ($line =~ /^\s*(.*)\s*while\s*\((.*)\)(.*)\s*$/) {
if ($line =~ /^\s*(.*)\s*while\s*\((.*)\s*\<STDIN\>\s*\)(.*)\s*$/) { #stdin
&whitespacePrinter($whitespaceCounter);
print "for line in sys.stdin:";
} else {
my $whileCondition = $2;
&whitespacePrinter($whitespaceCounter);
print "while ";
&arithmeticLines($whileCondition);
print ":\n";
$whitespaceCounter ++;
}
# elsif
} elsif ($line =~ /^\s*(.*)\s*elsif\s*\((.*)\)(.*)\s*$/) {
#remember to remove } if present
#becomes elif
my $elsifCondition = $2;
&whitespacePrinter($whitespaceCounter-1);
print "elif "; #so, so frustratingly messy :/
&arithmeticLines($elsifCondition);
print ":\n";
#if statements
} elsif ($line =~ /^\s*(.*)\s*if\s*\((.*)\)(.*)\s*$/) {
my $ifCondition = $2;
&whitespacePrinter($whitespaceCounter);
print "if "; #ugh this is messy :/
&arithmeticLines($ifCondition);
print ":\n";
$whitespaceCounter ++;
#else
} elsif ($line =~ /^\s*(.*)\s*else\s*(.*)\s*$/) {
#remember to remove } if present
&whitespacePrinter($whitespaceCounter-1);
print "else:\n";;
#end curly brace needs removal
} elsif ($line =~ /^\s*}\s*$/) {
$line =~ s/\}/ /;
$whitespaceCounter --;
#ARRAY HANDLING
#array conversion
}elsif ($line =~ /^\s*(.*)\s*\@(.*)\s*(.*)\s*;$/) { #array in the line
my $arrayName = $2;
if (/^\s*(.*)\s*\@(.*)\s*=\s*((.*))\s*;$/) { #declaring the array
$line =~ s/\@//;
$line =~ s/\(/\[/;
$line =~ s/\)/\]/;
$line =~ s/\"/\'/g;
$line =~ s/\;//;
print "$line";
} else { #accessing the array
# $arrayName[0 or whatever];
$line =~ s/\@//;
$line =~ s/\;//;
print "$line";
}
#push
} elsif ($line =~ /^\s*push\s*\@(.*)\,\s*(.*)\s*;$/) {
&whitespacePrinter($whitespaceCounter);
print "$1.push($2)\n";
#pop
} elsif ($line =~ /^\s*pop\s*\@(.*);$/) {
&whitespacePrinter($whitespaceCounter);
print "$1.pop\n";
#unshift
} elsif ($line =~ /^\s*unshift\s*\@(.*)\,\s*(.*)\s*;$/) {
&whitespacePrinter($whitespaceCounter);
print "$1.unshift($2)\n";
#pop
} elsif ($line =~ /^\s*shift\s*\@(.*);$/) {
&whitespacePrinter($whitespaceCounter);
print "$1.shift\n";
#concatenation with . (both dont add spaces by default yay) (with + is the same)
# } elsif ($line =~ /^\s*(.*)\s*\.\s*(.*)\s*;$/) { #untested match
# $line =~ s/\./\+/;
# print $line;
#substitution s/// (UNTESTED)
# $line =~ s/[aeiou]//g; becomes line = re.sub(r'[aeiou]', '', line)
# } elsif ($line =~ /^\s*\$(.*)\s*(.*)\s*s\/(.*)\/(.*)\/(.*);$/) {
# my $variableName = $1;
# my $replaced = $3;
# my $replacedWith = $4;
# print "$variableName = re.sub(r'$3', '$4', $variableName)"
# Lines we can't translate are turned into comments
} else {
print "#$line\n";
}
}
sub arithmeticLines {
# $#
$_[0] =~ s/\$\#ARGV/len\(sys\.argv\)/;
#removes $ before variables
$_[0] =~ s/\$//g;
$_[0] =~ s/\@//g;
#stdin
$_[0] =~ s/\<STDIN\>/float\(sys\.stdin\.readline\(\)\)/;
#and/or/not
$_[0] =~ s/\&\&/and /g;
$_[0] =~ s/\|\|/or /g;
$_[0] =~ s/!\s/not /g;
#comparison operators that dont exist in Python replaced with those that do
$_[0] =~ s/ eq / == /g;
$_[0] =~ s/ ne / != /g;
$_[0] =~ s/ gt / > /g;
$_[0] =~ s/ lt / < /g;
$_[0] =~ s/ ge / >= /g;
$_[0] =~ s/ le / <= /g;
#division
$_[0] =~ s/\//\/\//g;
#remove that semicolon
$_[0] =~ s/\;//;
print $_[0];
}
sub whitespacePrinter {
my $whitespace = $_[0];
for (my $x = 0; $x < $whitespace; $x ++) {
print ' ';
}
}