22
33use strict;
44
5+ my $whitespaceCounter = 0; # global variable? Its outside the loop...
6+
57while (my $line = <>) {
68
79 # NOTE: Deal with semicolons on a line by line basis
810
9- # &whitespaceStack(&stateStack); #I think that this works?
11+ # print whitespace, if relevant
12+ # for ($x=0; $x<$whitespaceCounter; $x++) {
13+ # print ' ';
14+ # }
1015
1116# translate #! line
1217 if ($line =~ / ^#!/ && $. == 1) {
2126 my $printInput = $1 ;
2227 if ($printInput =~ / ^(.*)\s *\$ (.*)*$ / ) { # there is ONE variable
2328 $printInput =~ s /\$ // ; # removes variable signal
29+ &whitespacePrinter($whitespaceCounter );
2430 print " print $printInput \n " ;
2531 } else { # there is no variable (or many, which currently kill everything)
32+ &whitespacePrinter($whitespaceCounter );
2633 print " print \" $printInput \"\n " ;
2734 }
2835
2936# print statment with no newline
3037 } elsif ($line =~ / ^\s *print\s *"(.*)"[\s ;]*$ / ) {
3138 my $printInput = $1 ;
3239 if ($printInput =~ / ^(.*)\s *\$ (.*)*$ / ) { # there is ONE variable
40+ &whitespacePrinter($whitespaceCounter );
3341 $printInput =~ s /\$ // ; # removes variable signal
3442 print " sys.stdout.write($printInput )\n " ;
3543 } else { # there is no variable (or many, which currently kill everything)
44+ &whitespacePrinter($whitespaceCounter );
3645 print " sys.stdout.write(\" $printInput \" )\n " ;
3746 }
3847
48+ # break/continue
49+ } elsif ($line =~ / ^\s *last;$ / ) {
50+ &whitespacePrinter($whitespaceCounter );
51+ print " break\n " ;
52+
53+ } elsif ($line =~ / ^\s *next;$ / ) {
54+ &whitespacePrinter($whitespaceCounter );
55+ print " continue\n " ;
56+
3957# arithmetic operations
4058 } elsif ($line =~ / ^\s *[^\s ]*\s *=(.*);$ / ) {
4159# print $line;
60+ &whitespacePrinter($whitespaceCounter );
4261 &arithmeticLines($line );
43- # print "$lineToPrint\n";
4462
45- # break/continue
46- } elsif ($line =~ / ^\s *[^\s ]*\s *(break)(.*);$ / || $line =~ / ^\s *[^\s ]*\s *(continue)(.*);$ / ) {
47- print " $1 "
48-
49- # for loops
63+ # ++ and --
64+ } elsif ($line =~ / ^\s *(.*)\s *\+\+ (.*);$ / ) {
65+ # change ++ and -- to python equivalents
66+ &whitespacePrinter($whitespaceCounter );
67+ my $plusPlus = $1 ;
68+ $plusPlus =~ s /\$ // ;
69+ print " $plusPlus +=1\n " ;
70+ } elsif ($line =~ / ^\s *(.*)\s *\-\- (.*);$ / ) {
71+ &whitespacePrinter($whitespaceCounter );
72+ my $minusMinus = $1 ;
73+ $minusMinus =~ s /\$ // ;
74+ print " $minusMinus -= 1\n " ;
75+
76+ # for loops (If in C style then no direct comparison)?
5077
5178# while loops
79+ } elsif ($line =~ / ^\s *(.*)\s *while\s *\( (.*)\) (.*)\s *$ / ) {
80+ my $whileCondition = $2 ;
81+ &whitespacePrinter($whitespaceCounter );
82+ print " while " ;
83+ &arithmeticLines($whileCondition );
84+ print " :\n " ;
85+ $whitespaceCounter ++;
86+
87+ # elsif
88+ } elsif ($line =~ / ^\s *(.*)\s *elsif\s *\( (.*)\) (.*)\s *$ / ) {
89+ # remember to remove } if present
90+ # becomes elif
91+ my $elsifCondition = $2 ;
92+ &whitespacePrinter($whitespaceCounter -1);
93+ print " elif " ; # so, so frustratingly messy :/
94+ &arithmeticLines($elsifCondition );
95+ print " :\n " ;
5296
5397# if statements
5498 } elsif ($line =~ / ^\s *(.*)\s *if\s *\( (.*)\) (.*)\s *$ / ) {
55- my $condition = $2 ;
56- print " if " ;
57- &arithmeticLines($condition );
99+ my $ifCondition = $2 ;
100+ &whitespacePrinter($whitespaceCounter );
101+ print " if " ; # ugh this is messy :/
102+ &arithmeticLines($ifCondition );
58103 print " :\n " ;
59-
60- # elsif
104+ $whitespaceCounter ++;
61105
62106# else
63- # } elsif ($line =~ /^$/
107+ } elsif ($line =~ / ^\s *(.*)\s *else\s *(.*)\s *$ / ) {
108+ # remember to remove } if present
109+ &whitespacePrinter($whitespaceCounter -1);
110+ print " else:\n " ;;
64111
65112# end curly brace needs removal
66- } elsif ($line =~ / ^\s *(.*)\s *\}\s *\( (.*)\) (.*);$ / ) {
67- $line =~ s /\} // ;
113+ } elsif ($line =~ / ^\s *}\s *$ / ) {
114+ $line =~ s /\} / / ;
115+ $whitespaceCounter --;
116+
117+ # substitution using s/// (UNTESTED AS YET)
118+ } elsif ($line =~ / ^\s *(.*)\s *s\/ (.*)\/ (.*)\/ g(.*)\s *;$ / ) {
119+ my $replaced = $2 ;
120+ my $replaceWith = $3 ;
121+ print " re.compile('$replaced ').sub('$replaceWith ', s)"
122+
123+ # looping through STDIN
124+ } elsif ($line =~ / ^\s *while\s *\(\< STDIN\>\)\s *(.*)\s *$ / ) {
125+ &whitespacePrinter($whitespaceCounter );
126+ print " import sys" ;
127+ &whitespacePrinter($whitespaceCounter );
128+ print " for line in sys.stdin:" ;
68129
69130# Lines we can't translate are turned into comments
70131 } else {
@@ -76,30 +137,32 @@ sub arithmeticLines {
76137# things for non interesting lines; to be called as a sub just before the untranslatables
77138 # removes $ before variables
78139 $_ [0] =~ s /\$ // g ;
79- # change ++ and -- to python equivalents
80- $_ [0] =~ s /\+\+ / \+\= 1/ g ;
81- $_ [0] =~ s /\-\- / \-\= 1/ g ;
82- # and/or/not
83- $_ [0] =~ s /\&\& / and/ g ;
84- $_ [0] =~ s /\|\| / or/ g ;
85- $_ [0] =~ s / !\s / not/ g ;
86140
141+ # and/or/not
142+ $_ [0] =~ s /\&\& / and / g ;
143+ $_ [0] =~ s /\|\| / or / g ;
144+ $_ [0] =~ s / !\s / not / g ;
145+
146+ # comparison operators that dont exist in Python
147+ $_ [0] =~ s / eq / \=\= / g ;
148+ $_ [0] =~ s / ne / !=/ g ;
149+ $_ [0] =~ s / gt / >/ g ;
150+ $_ [0] =~ s / lt / </ g ;
151+ $_ [0] =~ s / ge / >=/ g ;
152+ $_ [0] =~ s / le / <=/ g ;
153+
154+ # division
155+ $_ [0] =~ s /\/ / \/\/ / g ;
156+
157+ # remove that semicolon
87158 $_ [0] =~ s /\; // ;
88159 print $_ [0];
89160}
90161
91- # WHERE IN THE PROCESS DO I DO THESE? Before every line needs an update, yeah?
92-
93- sub stateStack {
94- if ($_ [0] =~ / ^\s *[^\s ]*\s *if(.*)/ || $_ [0] =~ / ^\s *[^\s ]*\s *for(.*)/ || $_ [0] =~ / ^\s *[^\s ]*\s *while(.*)/ ) {
95- # push onto the stack
96- } elsif ($_ [0] =~ / ^\s *\} (.*)$ / ) {
97- # pop from the stack
162+ sub whitespacePrinter {
163+ my $whitespace = $_ [0];
164+ for (my $x = 0; $x < $whitespace ; $x ++) {
165+ print ' ' ;
98166 }
99167}
100168
101- sub whitespaceStack {
102- # prints size of stack in whitespace
103-
104- }
105-
0 commit comments