From 617e9da07f71a83851113ddb161e6e8379578057 Mon Sep 17 00:00:00 2001 From: Hayden William Arie Van Reyswoud Date: Mon, 7 Oct 2013 17:23:18 +1100 Subject: [PATCH] implementing a few features lacking from specs --- diary.txt | 6 ++- perl2python | 123 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 85 insertions(+), 44 deletions(-) diff --git a/diary.txt b/diary.txt index 326d686..49dc099 100644 --- a/diary.txt +++ b/diary.txt @@ -20,5 +20,7 @@ Date Start Stop Activity Comments 04/10 9:40am 11:00am added in handling for multiple conditional operators in if and while statements and support for $hash{$key}++ 5:05pm 6:00pm added capabilities for handling more arrays/lists 8:00pm 8.25pm finished append to lists func - -Total Time: 1100min = 18hr 20min +07/10 12:50pm 2:00pm fixed bug with comments + 2:30pm 3.10pm trying to fix variable dec + 4:40pm +Total Time: 1210min = 20hr 10min diff --git a/perl2python b/perl2python index b586d54..e07033d 100755 --- a/perl2python +++ b/perl2python @@ -3,15 +3,17 @@ # written by andrewt@cse.unsw.edu.au September 2013 # as a starting point for COMP2041/9041 assignment # http://cgi.cse.unsw.edu.au/~cs2041/13s2/assignments/perl2python -# edited by hwav057@cse.unsw.edu.au for more functionality September 2013 +# edited by hwav057@cse.unsw.edu.au for more functionality September and October 2013 -#fixing implementation of array.append and $array[$i++] - -#split function (happens in variable dec) - -#double hashes? ?? - -#arrays and push pop unshift reverse +#Do in order: +#3. need to do simple uses of printf in perl +#4. split function (happens in variable dec) +#5. range things such as 1..3 +#5. arrays and push pop unshift reverse +#6. need to handle @array = or @array = +#7. need to handle print "@array" and also note that print "@array hi there this is a message\n" +#8. need to be able to alter existing array elements, and use them? +#9. double hashes? ?? our $tab = 0; our @python_source = (); @@ -25,6 +27,12 @@ our $lines_python_source = 0; our $lines_imports = 0; while ($line = <>) { chomp $line; + #need to check if there is any comments at end of line of code, if there is + #will be added to source array at the bottom of this while loop, just before + #next line is translated + if ($line =~ s/\;\s*#(.*)$//) { + $comment = $1; + } if ($line =~ /^#!/ && $. == 1) { # translate #! line print "#!/usr/bin/python2.7 -u\n"; @@ -88,8 +96,15 @@ while ($line = <>) { } } else { # Lines we can't translate are turned into comments + print "HI\n"; addToSourceArray("#$line\n"); } + #adding comments if they exiest + if (defined $comment) { + $python_source[$lines_python_source-1] =~ s/\n//; + $python_source[$lines_python_source-1] .= " #$comment\n"; + undef $comment; + } } outputSource(); @@ -190,31 +205,6 @@ sub variableDec { } else { addToSourceArray("$variable_name = $variable_assign\n"); } - } elsif ($variable_assign =~ /\$/) { - #check if each item is a variable and interp - #print "var ass ($variable_assign)\n"; - @assigns = split / /, $variable_assign; - my $line_to_print = ''; - #$line_to_print = "$variable_name = "; - $i = 0; - foreach $assignment (@assigns) { - if ($assignment eq "\") { - handleImports("sys"); - $assignment = "sys.stdin.readline()"; - } elsif ($assignment =~ /\$/) { - $assignment =~ s/\$//; - } - $assignment =~ s/length/len/; #convert all length() funcs to len() - $line_to_print .= "$assignment"; - if ($i < $#assigns) { $line_to_print .= " "; } - $i++ - } - #$line_to_print .= "\n"; - if ($using_array == 1) { - addToSourceArray("$variable_name$line_to_print)\n"); - } else { - addToSourceArray("$variable_name = $line_to_print\n"); - } } elsif ($variable_assign eq '') { #variable dec is of form $variable++ or $variable-- #also need to check if these are hashes, if so the variable name will @@ -256,12 +246,49 @@ sub variableDec { } else { #case that its of a different form, then can be recognised and fixed addToSourceArray("#$variable_name# THIS IS NOT CODED FOR YET\n"); } - } else { - $variable_assign = checkSysMod($variable_assign); + } else { + #check if each item is a variable and interp + #print "var ass ($variable_assign)\n"; + #$variable_assign =~ s/\"|\'//g; + $variable_assign =~ s/length\((.*)\)/len\($1\)/; #convert all length() funcs to len() + $variable_assign =~ s/\s?\.\s?/ \+ /g; + $variable_assign =~ s/\"\$([a-zA-Z]+)\"/$1/g; + $quote = 0; + $variable = 0; + foreach $letter (split //,$variable_assign) { + if ($letter eq '"') { + + if ($quote == 1) { + if ($variable == 1) { + print " hi\!\n"; + $variable = 0; + $letter = ''; + } + $quote = 0; + } + else { $quote = 1; } + } + if (($letter eq '$')&&($quote == 1)) { + #case that it is a variable inside a string + $to_add .= '"+ '; + $variable = 1; + } + if (($letter eq ' ')&&($variable == 1)) { + #then variable is done + $to_add .= ' + "'; + $variable = 0; + } + $to_add .= $letter; + } + $variable_assign = $to_add; + $variable_assign =~ s/\$//g; + $line_to_print .= "$variable_assign"; + + #$line_to_print .= "\n"; if ($using_array == 1) { - addToSourceArray("$variable_name$variable_assign)\n"); + addToSourceArray("$variable_name$line_to_print)\n"); } else { - addToSourceArray("$variable_name = $variable_assign\n"); + addToSourceArray("$variable_name = $line_to_print\n"); } } } @@ -432,7 +459,7 @@ sub conditionHandling { } elsif ($cond_to_check =~ /^\$(.*)$/) { #variable declaration, just remove "$" return $1; - } elsif ($cond_to_check =~ /^\>=*|[=!]?=|\<=*|%|&|\||\^|~|>>|<<|\*\*$/) { + } elsif ($cond_to_check =~ /^\>=*|[=!]?=|\<=*|%|&|\||\^|~|>>|<<|\*\*|!$/) { #conditional operators that are the same in perl and python #i am also including bitwise operators here as they are also the same #this case here is also used to determine that a variable is an integer @@ -459,6 +486,7 @@ sub handleIf { #function for handling if statements, mainly just changes curly brackets #to colon, and translates the condition statement my $conds_to_trans = $_[0]; + $conds_to_trans =~ s/length\((.+)\)/lenLBRACKET$1RBRACKET/g; $conds_to_trans =~ s/^\(//; $conds_to_trans =~ s/\)$//; #print "($conds_to_trans)\n"; @@ -483,8 +511,15 @@ sub handleIf { $line_to_print .= 'm'; } else { foreach $cond (@conds) { - $line_to_print .= conditionHandling($cond, $last); - $last = $cond; + if ($cond =~ /lenLBRACKET.+RBRACKET/) { + $cond =~ s/LBRACKET/\(/; + $cond =~ s/RBRACKET/\)/; + $cond =~ s/\$//; + $line_to_print .= $cond; + } else { + $line_to_print .= conditionHandling($cond, $last); + $last = $cond; + } } } @@ -494,11 +529,10 @@ sub handleIf { sub handleWhile { my $conds_to_trans = $_[0]; + $conds_to_trans =~ s/length\((.+)\)/lenLBRACKET$1RBRACKET/g; $conds_to_trans =~ s/^\(//; $conds_to_trans =~ s/\)$//; - #print "($conds_to_trans)\n"; my @a = split /\(|\)/,$conds_to_trans; - #print "@a\n"; $line_to_print = "while "; while (my $condi = shift @a) { #got to check if its a logical operator @@ -536,6 +570,11 @@ sub handleWhile { $lines_python_source--; $line_to_print = "for line in $temp"; $should_break = 1; + } elsif ($cond =~ /lenLBRACKET.+RBRACKET/) { + $cond =~ s/LBRACKET/\(/; + $cond =~ s/RBRACKET/\)/; + $cond =~ s/\$//; + $line_to_print .= $cond; } else { $line_to_print .= conditionHandling($cond); }