-
Notifications
You must be signed in to change notification settings - Fork 4
/
Flow.pm
executable file
·143 lines (121 loc) · 3.07 KB
/
Flow.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
#!/usr/bin/perl -w
# Matthew Moss
# cs2041, 12s2
use strict;
use Translate;
use Assignment;
use Builtins;
use Command;
package Flow;
my %keywords;
$keywords{'if'} = \&convert_if;
$keywords{'elif'} = \&convert_elif;
$keywords{'for'} = \&convert_for;
$keywords{'while'} = \&convert_while;
$keywords{'else'} = \&convert_else;
$keywords{'do'} = \&ignore_keywords;
$keywords{'then'} = \&ignore_keywords;
$keywords{'done'} = \␣
$keywords{'fi'} = \␣
sub can_handle {
# Identifies if this module can handle the line
my @input = split (/\s/, $_[0]);
if (defined ($keywords{$input[0]})) {
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 = split (/\s/, $_[0]);
if (defined ($keywords{$input[0]})) {
return &{$keywords{$input[0]}}($_[0]);
}
return $_[0];
}
sub get_indent_delta {
my $result = 0;
$_[0] =~ /(\w+)/;
if ($1 =~ /^(do|then)$/) {
$result = 4;
} elsif ($1 =~ /^(done|fi|elif)$/) {
$result = -4;
}
return $result;
}
sub convert_if {
my $input = $_[0];
my $result = "";
if ($input =~ /if (.*)$/) {
$result = "if ".convert_condition($1).":";
}
return $result;
}
sub convert_elif {
my $input = $_[0];
my $result = "";
if ($input =~ /elif (.*)$/) {
$result = "elif ".convert_condition($1).":";
}
return $result;
}
sub convert_while {
my $input = $_[0];
my $result = "";
if ($input =~ /while\s+(.*)$/) {
$result = "while ".convert_condition($1).":";
}
return $result;
}
sub convert_condition {
my $condition = $_[0];
my $result = "";
if ($condition =~ /^\$/) {
# It's a variable
$result = Translate::arguments($condition);
} elsif (Builtins::can_handle($condition)){
$result = Builtins::handle($condition);
} else {
$result = "not ".Command::handle($condition);
}
return $result;
}
sub convert_for {
my $input = $_[0];
my $result = "";
if ($input =~ /for (\w+) in (.*)$/) {
my $variable = $1;
my $iterables = $2;
if ($iterables =~ /\*.*/) {
# This is a special case for the use of " for some reason.
$result = 'for '.$variable.' in sorted(glob.glob("'.$iterables.'")):';
} else {
$result = "for ".$variable." in ".Translate::arguments($iterables).":";
}
}
return $result;
}
sub convert_else {
return "else:";
}
sub ignore_keywords {
my $result = $_[0];
$result =~ s/^\s*(do|then)\s*(.*)$//;
my $expression = $2;
if ($expression) {
if (Assignment::can_handle($expression)) {
$result = Assignment::handle($expression);
} elsif (Builtins::can_handle($expression)) {
$result = Builtins::handle($expression);
} elsif (Command::can_handle($expression)) {
$result = Command::handle($expression);
}
}
return $result;
}
sub blank {
return "";
}
1;