forked from mdmoss/shell2python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Builtins.pm
executable file
·230 lines (192 loc) · 5.39 KB
/
Builtins.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl -w
# Matthew Moss
# cs2041, 12s2
use strict;
use Translate;
use Command;
package Builtins;
my %keywords = (
'echo' => \&echo_to_print,
'exit' => \&exit_to_exit,
'read' => \&read_to_stdin,
'cd' => \&cd_to_chdir,
'test' => \&convert_test,
'expr' => \&convert_expr);
sub can_handle {
# Identifies if this module can handle the line
my $input = $_[0];
if ($input =~ /^\s*\`/) {
# It's a backticked expression. Better handle it.
return 1;
}
$input =~ /^\s*(\w+)/;
if (defined ($keywords{$1})) {
return 1;
}
return 0;
}
sub handle {
# This is the generic entry point for converting a line.
# Should only be called after can_handle returns true
my $input = $_[0];
$input =~ /^\s*(\w+)/;
if ($input =~ /^\s*\`/) {
# It's a backticked expression. Better handle it.
return convert_backticks($input);
}
if (defined ($keywords{$1})) {
return &{$keywords{$1}}($input);
}
return $input;
}
sub convert_backticks {
my $input = $_[0];
$input =~ /^\s*\`(.*?)\`\s*$/;
my $expression = $1;
if (can_handle ($expression)) {
return handle ($expression);
}
my $command = Command::handle($expression);
$command =~ s/subprocess\.call/subprocess\.check_output/;
return $command;
}
sub echo_to_print {
# Anything with an unescaped $ is a variable. Otherwise, string.
my $input = $_[0];
chomp $input;
my $result = "";
if ($input =~ />\S+\s*$/) {
# There's stream redirection. Write to file instead
$result = echo_to_file ($input);
} elsif ($input =~ /echo\s+-n\s*/) {
$result = echo_to_stdout ($input);
} else {
$input =~ s/echo//;
$result = "print ".Translate::arguments($input);
}
$result =~ s/\s*$//;
return $result;
}
sub echo_to_stdout {
my $input = $_[0];
$input =~ s/\s*$//;
if ($input =~ /echo\s+-n$/) {
return "sys.stdout.write('')";
} else {
# It actually has some arguments
$input =~ s/echo\s+-n\s+//;
my $string = "sys.stdout.write(".Translate::arguments($input, "str", " + ' ' + ").")";
return $string;
}
return $_[0];
}
my %file_types = (
'>>' => "'a'",
'>' => "'w'");
sub echo_to_file {
# We can only handle a single destination, so we'll assume it's the last one that appears
my $input = $_[0];
$input =~ s/(>>?)(\S+)\s*$//;
my $attrs = $1;
my $file = $2;
# Remove the initial echo
$input =~ s/echo//;
my $result = "with open(".Translate::arguments($file).", ".$file_types{$attrs}.") as f: print >>f, ".Translate::arguments($input);
return $result;
}
sub cd_to_chdir {
my $input = $_[0];
chomp $input;
$input =~ s/cd//;
my $result = "os.chdir(".Translate::arguments($input, "str").")";
return $result;
}
sub exit_to_exit {
my $input = $_[0];
if ($input =~ /exit (\d+)/) {
return "sys.exit(".$1.")";
}
return $input;
}
sub read_to_stdin {
my $input = $_[0];
if ($input =~ /read (\w+)/) {
return $1." = sys.stdin.readline().rstrip()";
}
return $input;
}
sub make_non_numeric_int {
my $input = $_[0];
unless ($input =~ /^\s*\d*\s*$/) {
$input =~ s/(.*)/int\($1\)/;
}
return $input;
}
my %numeric_tests = (
'-eq' => '==',
'-ne' => '!=',
'-gt' => '>',
'-ge' => '>=',
'-lt' => '<',
'-le' => '<=');
sub convert_test_expression {
my $input = $_[0];
my $result = "";
if ($input =~ /-r (\S+)/) {
$result = "os.access(".Translate::arguments($1, "str").", os.R_OK)";
} elsif ($input =~ /-d (\S+)/) {
$result = "os.path.isdir(".Translate::arguments($1, "str").")";
} elsif ($input =~ /(\S+) (\S+) (\S+)/) {
my $lhs = $1;
my $rhs = $3;
if ($2 eq "=") {
$result = Translate::arguments ($lhs)." == ".Translate::arguments ($rhs);
} elsif (defined ($numeric_tests{$2})) {
$lhs = Translate::arguments($lhs, "int");
$rhs = Translate::arguments($rhs, "int");
$result = $lhs." ".$numeric_tests{$2}." ".$rhs;
}
}
return $result;
}
my %test_seperators = (
'-o' => 'or',
'-a' => 'and');
sub convert_test {
my $input = $_[0];
$input =~ s/test\s+//;
# We now have one or more expressions, seperated by [-o|-a];
my $result = "";
while ($input =~ /\s*(.*?)\s*(-a|-o)/) {
my $seperator = $2;
$result = $result.convert_test_expression($1)." ".$test_seperators{$seperator}." ";
$input =~ s/\s*(.*?)\s*(-a|-o)//;
}
$result = $result.convert_test_expression($input);
return $result;
}
my %expr_ops = (
'=' => '==');
sub convert_expr {
my $input = $_[0];
# Crop the expr
$input =~ s/expr\s+//;
# Process the first element - this should always be a value
$input =~ s/(\S+)\s+//;
my $result = Translate::arguments($1, "int");
while ($input =~ /(\S+)\s+(\S+)/) {
my $operation = $1;
my $value = $2;
$operation =~ s/\\//g;
if (defined ($expr_ops{Translate::remove_quotes($operation)})) {
$result = $result." ".$expr_ops{Translate::remove_quotes($operation)};
} else {
$result = $result." ".Translate::remove_quotes($operation);
}
$result = $result." ".Translate::arguments($value, "int");
$input =~ s/\S+\s+\S+//;
}
return $result;
}
1;