Skip to content

Commit 38449e2

Browse files
committed
Added more tests and demoes, split, join, fixed some bugs, rewoeked chomp and array handling
1 parent 36770e6 commit 38449e2

File tree

3 files changed

+132
-45
lines changed

3 files changed

+132
-45
lines changed

demo05.pl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/perl -w
2+
3+
#tests array initialisation, push, pop, shift and unshift
4+
5+
@a = (6, 7, 8, 9);
6+
7+
push @a, 9;
8+
pop @a;
9+
unshift @a, 5;
10+
shift @a;
11+
12+
split(/\s+/, "do re mi fa");
13+
14+
#should probably rejig this in a bit to make it more 'relevant'

demo06.pl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/perl -w
2+
3+
#adapted from cookie, on the 2041 website
4+
5+
while (1) {
6+
print "Give me cookie\n";
7+
$line = <STDIN>;
8+
chomp $line;
9+
if ($line eq "cookie") {
10+
last;
11+
}
12+
}
13+
print "Thank you\n";

perlpythonLinesInProgress.pl

Lines changed: 105 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
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";
@@ -24,8 +19,17 @@
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)
@@ -41,7 +45,8 @@
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);
@@ -60,7 +65,7 @@
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

@@ -76,11 +81,30 @@
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*\+\+(.*);$/) {
@@ -96,15 +120,26 @@
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*$/) {
@@ -136,7 +171,24 @@
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);
@@ -158,11 +210,18 @@
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 {
@@ -172,35 +231,35 @@
172231
}
173232
sub 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

206265
sub whitespacePrinter {
@@ -210,3 +269,4 @@ sub whitespacePrinter {
210269
}
211270
}
212271

272+

0 commit comments

Comments
 (0)