88
99 # NOTE: Deal with semicolons on a line by line basis
1010
11- # print whitespace, if relevant
12- # for ($x=0; $x<$whitespaceCounter; $x++) {
13- # print ' ';
14- # }
15-
1611# translate #! line
1712 if ($line =~ / ^#!/ && $. == 1) {
1813 print " #!/usr/bin/python2.7 -u\n " ;
2419 # print statement with newline
2520 } elsif ($line =~ / ^\s *print\s *"(.*)\\ n"[\s ;]*$ / ) {
2621 my $printInput = $1 ;
27- if ($printInput =~ / ^(.*)\s *\$ (.*)*$ / ) { # there is ONE variable
28- $printInput =~ s /\$ // ; # removes variable signal
22+ if ($printInput =~ / ARGV\[ (.*)\] $ / ) { # that variable is ARGV[]
23+ &whitespacePrinter($whitespaceCounter );
24+ my $location = $1 ;
25+ $location =~ s /\$ // ;
26+ print " print sys.argv[$location + 1]\n "
27+ } elsif ($printInput =~ / ^(.*)\s *\$ (.*)*$ / ) { # there is ONE $variable
28+ $printInput =~ s /\$ // ; # removes variable signs
29+ &whitespacePrinter($whitespaceCounter );
30+ print " print $printInput \n " ;
31+ } elsif ($printInput =~ / ^(.*)\s *\@ (.*)*$ / ) { # there is ONE @variable
32+ $printInput =~ s /\@ // ;
2933 &whitespacePrinter($whitespaceCounter );
3034 print " print $printInput \n " ;
3135 } else { # there is no variable (or many, which currently kill everything)
4145 print " print line\n " ;
4246 } elsif ($printInput =~ / ^(.*)\s *\$ (.*)*$ / ) { # there is ONE variable
4347 &whitespacePrinter($whitespaceCounter );
44- $printInput =~ s /\$ // ; # removes variable signal
48+ $printInput =~ s /\$ // ; # removes variable signs
49+ $printInput =~ s /\@ // ;
4550 print " sys.stdout.write($printInput )\n " ;
4651 } else { # there is no variable (or many, which currently kill everything)
4752 &whitespacePrinter($whitespaceCounter );
6065# looping through every line in a FILE
6166 } elsif ($line =~ / ^\s *while\s *(.*)\<\> (.*)\s *(.*)\s *$ / ) {
6267 &whitespacePrinter($whitespaceCounter );
63- print " import fileinput\n "
68+ print " import fileinput\n " ;
6469 &whitespacePrinter($whitespaceCounter );
6570 print " for line in fileinput.input():\n "
6671
7681 &whitespacePrinter($whitespaceCounter );
7782 print " $1 = sys.stdin.readlines()\n "
7883
84+ # split
85+ } elsif ($line =~ / ^\s *(.*)\s *=\s *split\(\/ (.*)\/ ,\s *\$ (.*)\)\s *;/ ) {
86+ my $string = $3 ;
87+ my $delineator = $2 ;
88+ my $assignmentVariable = $1 ;
89+ &whitespacePrinter($whitespaceCounter );
90+ print " $assignmentVariable = $string .split(\" $delineator \" )\n " ;
91+
92+ # join
93+ } elsif ($line =~ / ^\s *(.*)\s *=\s *join\(\' (.*)\'\,\s *(.*)\)\s *;$ / ) {
94+ my $assignmentVariable = $1 ;
95+ my $string = $3 ;
96+ my $delineator = $2 ;
97+ &whitespacePrinter($whitespaceCounter );
98+ print " $assignmentVariable = '$delineator '.join([$string ])" ;
99+
79100# arithmetic operations
80101 } elsif ($line =~ / ^\s *[^\s ]*\s *=(.*);$ / ) {
81- # print $line;
82- &whitespacePrinter($whitespaceCounter );
83- &arithmeticLines($line );
102+ if ($line =~ / ^\s *\@ (.*)\s *=\s *(.*);$ / ) {# arrays are dealt with seperately
103+ next ;
104+ } else {
105+ &whitespacePrinter($whitespaceCounter );
106+ &arithmeticLines($line );
107+ }
84108
85109# ++ and --
86110 } elsif ($line =~ / ^\s *(.*)\s *\+\+ (.*);$ / ) {
96120 print " $minusMinus -= 1\n " ;
97121
98122# for loops (If in C style then no direct comparison)?
123+ # foreach (with ARGV) (super specific, could do with broadening in scope)
124+ } elsif ($line =~ / ^\s *foreach\s *\$ (.*)\s *\( (.*)\)\s *{\s *$ / ) {
125+ # foreach $i (0..$#ARGV) becomes for i in xrange (len(sys.argv) - 1):
126+ my $variableName = $1 ;
127+ &whitespacePrinter($whitespaceCounter );
128+ print " for $variableName in xrange (len(sys.argv) - 1):\n " ;
99129
100130# while loops
101131 } elsif ($line =~ / ^\s *(.*)\s *while\s *\( (.*)\) (.*)\s *$ / ) {
102- my $whileCondition = $2 ;
103- &whitespacePrinter($whitespaceCounter );
104- print " while " ;
105- &arithmeticLines($whileCondition );
106- print " :\n " ;
107- $whitespaceCounter ++;
132+ if ($line =~ / ^\s *(.*)\s *while\s *\( (.*)\s *\< STDIN\>\s *\) (.*)\s *$ / ) { # stdin
133+ &whitespacePrinter($whitespaceCounter );
134+ print " for line in sys.stdin:" ;
135+ } else {
136+ my $whileCondition = $2 ;
137+ &whitespacePrinter($whitespaceCounter );
138+ print " while " ;
139+ &arithmeticLines($whileCondition );
140+ print " :\n " ;
141+ $whitespaceCounter ++;
142+ }
108143
109144# elsif
110145 } elsif ($line =~ / ^\s *(.*)\s *elsif\s *\( (.*)\) (.*)\s *$ / ) {
136171 $line =~ s /\} / / ;
137172 $whitespaceCounter --;
138173
174+
139175# ARRAY HANDLING
176+ # array conversion
177+ }elsif ($line =~ / ^\s *(.*)\s *\@ (.*)\s *(.*)\s *;$ / ) { # array in the line
178+ my $arrayName = $2 ;
179+ if (/ ^\s *(.*)\s *\@ (.*)\s *=\s *((.*))\s *;$ / ) { # declaring the array
180+ $line =~ s /\@ // ;
181+ $line =~ s /\( / \[ / ;
182+ $line =~ s /\) / \] / ;
183+ $line =~ s /\" / \' / g ;
184+ $line =~ s /\; // ;
185+ print " $line " ;
186+ } else { # accessing the array
187+ # $arrayName[0 or whatever];
188+ $line =~ s /\@ // ;
189+ $line =~ s /\; // ;
190+ print " $line " ;
191+ }
140192# push
141193 } elsif ($line =~ / ^\s *push\s *\@ (.*)\,\s *(.*)\s *;$ / ) {
142194 &whitespacePrinter($whitespaceCounter );
158210 print " $1 .shift\n " ;
159211
160212
161- # substitution using s/// (UNTESTED AS YET)
162- } elsif ($line =~ / ^\s *(.*)\s *s\/ (.*)\/ (.*)\/ g(.*)\s *;$ / ) {
163- my $replaced = $2 ;
164- my $replaceWith = $3 ;
165- print " re.compile('$replaced ').sub('$replaceWith ', s)"
213+ # concatenation with . (both dont add spaces by default yay) (with + is the same)
214+ # } elsif ($line =~ /^\s*(.*)\s*\.\s*(.*)\s*;$/) { #untested match
215+ # $line =~ s/\./\+/;
216+ # print $line;
217+
218+ # substitution s/// (UNTESTED)
219+ # $line =~ s/[aeiou]//g; becomes line = re.sub(r'[aeiou]', '', line)
220+ # } elsif ($line =~ /^\s*\$(.*)\s*(.*)\s*s\/(.*)\/(.*)\/(.*);$/) {
221+ # my $variableName = $1;
222+ # my $replaced = $3;
223+ # my $replacedWith = $4;
224+ # print "$variableName = re.sub(r'$3', '$4', $variableName)"
166225
167226# Lines we can't translate are turned into comments
168227 } else {
172231}
173232sub arithmeticLines {
174233
175- # deals with arrays being initiated specifically
176- if ($_ [0] =~ / ^\s *\@ (.*)\s *\=\s *\( (.*)\)\s *;$ / ) {
177- &whitespacePrinter($whitespaceCounter );
178- print " $1 = [$2 ]\n " ;
234+ # $#
235+ $_ [0] =~ s /\$\# ARGV/ len\( sys\. argv\) / ;
179236
180- } else {
181- # removes $ before variables
182- $_ [0] =~ s /\$ // g ;
237+ # removes $ before variables
238+ $_ [0] =~ s / \$ // g ;
239+ $_ [0] =~ s /\@ // g ;
183240
184- # and/or/not
185- $_ [0] =~ s /\&\& / and / g ;
186- $_ [0] =~ s /\|\| / or / g ;
187- $_ [0] =~ s / !\s / not / g ;
241+ # stdin
242+ $_ [0] =~ s /\< STDIN\> / float\( sys\. stdin\. readline\(\)\) / ;
188243
189- # comparison operators that dont exist in Python
190- $_ [0] =~ s / eq / == / g ;
191- $_ [0] =~ s / ne / != / g ;
192- $_ [0] =~ s / gt / > / g ;
193- $_ [0] =~ s / lt / < / g ;
194- $_ [0] =~ s / ge / >= / g ;
195- $_ [0] =~ s / le / <= / g ;
244+ # and/or/not
245+ $_ [0] =~ s /\&\& / and / g ;
246+ $_ [0] =~ s /\|\| / or / g ;
247+ $_ [0] =~ s / !\s / not / g ;
248+
249+ # comparison operators that dont exist in Python replaced with those that do
250+ $_ [0] =~ s / eq / == / g ;
251+ $_ [0] =~ s / ne / != / g ;
252+ $_ [0] =~ s / gt / > / g ;
253+ $_ [0] =~ s / lt / < / g ;
254+ $_ [0] =~ s / ge / >= / g ;
255+ $_ [0] =~ s / le / <= / g ;
196256
197257 # division
198- $_ [0] =~ s /\/ / \/\/ / g ;
258+ $_ [0] =~ s /\/ / \/\/ / g ;
199259
200260 # remove that semicolon
201- $_ [0] =~ s /\; // ;
202- print $_ [0];
203- }
261+ $_ [0] =~ s /\; // ;
262+ print $_ [0];
204263}
205264
206265sub whitespacePrinter {
@@ -210,3 +269,4 @@ sub whitespacePrinter {
210269 }
211270}
212271
272+
0 commit comments