/
Control.pm
executable file
·187 lines (164 loc) · 6.45 KB
/
Control.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
package Perlito5::Grammar;
token unless {
unless <.opt_ws> <Perlito5::Expression.term_paren>
<.opt_ws> <Perlito5::Expression.term_curly>
[
<.opt_ws>
else <.opt_ws>
'{' <.opt_ws>
<Perlito5::Grammar.exp_stmts>
<.opt_ws>
'}'
{
my $body = Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_curly"})->[2];
if (!defined($body)) {
die "Missing code block in 'if'";
}
$MATCH->{"capture"} = Perlito5::AST::If->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_paren"})->[2],
body => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts"}) || [] ),
otherwise => Perlito5::AST::Lit::Block->new( stmts => $body),
)
}
|
{
my $body = Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_curly"})->[2];
if (!defined($body)) {
die "Missing code block in 'unless'";
}
$MATCH->{"capture"} = Perlito5::AST::If->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_paren"})->[2],
body => Perlito5::AST::Lit::Block->new(stmts => [ ]),
otherwise => Perlito5::AST::Lit::Block->new(stmts => $body),
)
}
]
};
token if {
if <.opt_ws> <Perlito5::Expression.term_paren>
<.opt_ws> <Perlito5::Expression.term_curly>
[
<.opt_ws>
else <.opt_ws>
'{' <.opt_ws>
<Perlito5::Grammar.exp_stmts>
<.opt_ws>
'}'
{
my $body = Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_curly"})->[2];
if (!defined($body)) {
die "Missing code block in 'if'";
}
$MATCH->{"capture"} = Perlito5::AST::If->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_paren"})->[2],
body => Perlito5::AST::Lit::Block->new( stmts => $body),
otherwise => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts"}) || [] )
)
}
|
<.opt_ws>
els <if>
{
my $body = Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_curly"})->[2];
if (!defined($body)) {
die "Missing code block in 'if'";
}
$MATCH->{"capture"} = Perlito5::AST::If->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_paren"})->[2],
body => Perlito5::AST::Lit::Block->new( stmts => $body),
otherwise => Perlito5::AST::Lit::Block->new( stmts => [ Perlito5::Match::flat($MATCH->{"if"}) ] ),
)
}
|
{
my $body = Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_curly"})->[2];
if (!defined($body)) {
die "Missing code block in 'if'";
}
$MATCH->{"capture"} = Perlito5::AST::If->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.term_paren"})->[2],
body => Perlito5::AST::Lit::Block->new(stmts => $body),
otherwise => Perlito5::AST::Lit::Block->new(stmts => [ ]),
)
}
]
};
token when {
when <.ws> <exp>
{
die "when - not implemented";
my $body = Perlito5::Match::flat($MATCH->{"exp"})->{'end_block'};
if (!defined($body)) {
die "Missing code block in 'when'";
}
$MATCH->{"capture"} = When->new(
parameters => Perlito5::Match::flat($MATCH->{"exp"})->{'exp'},
body => $body )
}
};
token for {
for 'each'?
[
<.ws> my <.opt_ws> <Perlito5::Grammar.var_ident> <.opt_ws>
'(' <Perlito5::Expression.paren_parse> ')' <.opt_ws>
'{' <.opt_ws>
<Perlito5::Grammar.exp_stmts>
<.opt_ws>
'}'
{
$MATCH->{"capture"} = Perlito5::AST::For->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.paren_parse"}),
topic => undef,
body => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts"}), sig => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.var_ident"}) )
)
}
|
<.opt_ws>
'(' <Perlito5::Expression.paren_parse> ')' <.opt_ws>
'{' <.opt_ws>
<Perlito5::Grammar.exp_stmts>
<.opt_ws>
'}'
{
$MATCH->{"capture"} = Perlito5::AST::For->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.paren_parse"}),
topic => undef,
body => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts"}), sig => undef )
)
}
]
};
token while {
while <.opt_ws>
'(' <Perlito5::Expression.paren_parse> ')' <.opt_ws>
'{' <.opt_ws>
<Perlito5::Grammar.exp_stmts>
<.opt_ws>
'}'
{
$MATCH->{"capture"} = Perlito5::AST::While->new(
cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.paren_parse"}),
body => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts"}), sig => undef )
)
}
};
=begin
=head1 NAME
Perlito5::Grammar - Grammar for Perlito
=head1 SYNOPSIS
my $match = $source.parse;
Perlito5::Match::flat($match); # generated Perlito AST
=head1 DESCRIPTION
This module generates a syntax tree for the Perlito compiler.
=head1 AUTHORS
Flavio Soibelmann Glock <fglock@gmail.com>.
The Pugs Team E<lt>perl6-compiler@perl.orgE<gt>.
=head1 SEE ALSO
The Perl 6 homepage at L<http://dev.perl.org/perl6>.
The Pugs homepage at L<http://pugscode.org/>.
=head1 COPYRIGHT
Copyright 2006, 2009, 2011, 2012 by Flavio Soibelmann Glock, Audrey Tang and others.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See L<http://www.perl.com/perl/misc/Artistic.html>
=end