About This Project.
This project was for an Assigned task for the course COMP2041 in UNSW NOTE: You may not use this code to commit acts of academic misconduct if you are a current student of COMP2041 assigned with the task pl2py or similar
The Code
#!/usr/bin/perl -w
# written by Dankoon Yoo September 2016
# for COMP2041/9041 assignment (UNSW)
# Dankoon Yoo | d.yoo@student.unsw.edu.au | dankoonyoo@gmail.com
# Also a personal challenge to see if I could get this translator to function without functions (subroutines)
$i = 0;
$sysTrue = 0;
$fiTrue = 0;
$reTrue = 0;
$blank = 0;
while ($iter = <>) {
if ($iter =~ /^\s*$/) {
$blank = 1;
}
if ($iter =~ /\<STDIN\>/ || $iter =~ /\@ARGV/ || $iter =~ /^\s*exit/) {
$sysTrue = 1;
if ($iter =~ /\<STDIN\>/){
#$iter =~ s/\<STDIN\>/sys\.stdin\.readline\(\)/g;
}
if ($iter =~ /\@ARGV/){
#$iter =~ s/\@ARGV/sys\.argv\[1\:\]/g;
}
}
if ($iter =~ /\<\>/) {
#<> fileinput
$fiTrue = 1;
}
if ($iter =~ /\=\~/) {
#=~ re
$reTrue = 1;
}
#%vartype;
#0 is undef var
#1 is num var
#2 is str var
if ($iter =~ /\$.*\s?/) {
#var type test
$var = $&;
$var =~ s/\s.*$//;
$var =~ s/\;//;
$var =~ s/\$//;
chomp $var;
if(exists($vartype{$var})) {
#holder
} else {
if ($iter =~ /\$.*\s*\=\s*\"/) {
#string is vartype 2
$vartype{$var} = 2;
} elsif ($iter =~ /\$.*\s*\=+\s*\"/ || $iter =~ /\$.*\s*(eq|ne)\s*\"/) {
#string is vartype 2
$vartype{$var} = 2;
} elsif ($iter =~ /\$.*\s*\=\s*[1-9]?[0-9]+/) {
#num is vartype 1
$vartype{$var} = 1;
} elsif ($iter =~ /\$.*\s*\=\=\s*[1-9]?[0-9]+/) {
#num is vartype 1 due to comp operator ==
$vartype{$var} = 1;
} elsif ($iter =~ /\$.*\s*\<\=\s*[1-9]?[0-9]+/ || $iter =~ /\$.*\s*\>\=\s*[1-9]?[0-9]+/ || $iter =~ /\$.*\s*\<\s*[1-9]?[0-9]+/ || $iter =~ /\$.*\s*\>\s*[1-9]?[0-9]+/) {
#num is vartype 1 due to comp operators [<>] [<>]=
$vartype{$var} = 1;
}
}
if ($iter =~ /\$.*\s*\=\s*\"/) {
#string is vartype 2
$vartype{$var} = 2;
} elsif ($iter =~ /\$.*\s*\=+\s*\"/ || $iter =~ /\$.*\s*(eq|ne)\s*\"/) {
#string is vartype 2
$vartype{$var} = 2;
} elsif ($iter =~ /\$.*\s*\=\s*[1-9]?[0-9]+/) {
#num is vartype 1
$vartype{$var} = 1;
} elsif ($iter =~ /\$.*\s*\=\=\s*[1-9]?[0-9]+/) {
#num is vartype 1 due to comp operator ==
$vartype{$var} = 1;
} elsif ($iter =~ /\$.*\s*\<\=\s*[1-9]?[0-9]+/ || $iter =~ /\$.*\s*\>\=\s*[1-9]?[0-9]+/ || $iter =~ /\$.*\s*\<\s*[1-9]?[0-9]+/ || $iter =~ /\$.*\s*\>\s*[1-9]?[0-9]+/) {
#num is vartype 1 due to comp operators [<>] [<>]=
$vartype{$var} = 1;
}
}
# @iterControl is iteration control array
$iterControl[$i] = $iter;
$i = $i + 1;
}
$arraySize = $i + 1;
$c = 100;
while ($c < $arraySize) {
$line = "hello";
$line = $iterControl[$c];
$line = $line;
if ($line !~ /^blankk$/){
print $line;
} elsif ($line =~ /^blankk$/){
$line = "\n";
print $line;
}
$c = $c + 1;
}
$c = 0;
while ($c < $i) {
$line = "hello";
$line = $iterControl[$c];
$line = $line;
if ($line){
$blank = 0;
} else {
$blank = 1;
}
if ($line =~ /^#!/ && $c == 0) {
# translate #! line
print "#!/usr/local/bin/python3.5 -u\n";
if ($sysTrue == 1 || $fiTrue == 1 || $reTrue == 1){
print "import ";
if ($sysTrue == 1) {
print "sys";
if ($fiTrue == 1 || $reTrue == 1) {
print ", ";
}
}
if ($fiTrue == 1) {
print "fileinput";
if ($reTrue == 1) {
print ", ";
}
}
if ($reTrue == 1) {
print "re";
}
print "\n";
}
}
full code available on github
Other
As a personal challenge to myself, the code was written without the use of any functions (subroutines)
Authored by
Dan K Yoo (@dankyoo)