forked from rakudo/rakudo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperl6.pir
238 lines (190 loc) · 5.91 KB
/
perl6.pir
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
231
232
233
234
235
236
237
238
=head1 TITLE
perl6.pir - The Rakudo Perl 6 compiler.
=head2 Description
This is the base file for the Rakudo Perl 6 compiler.
=cut
.loadlib 'perl6_group'
.loadlib 'perl6_ops'
.include 'src/gen_builtins.pir'
=head2 Functions
=over 4
=item onload()
Creates the Perl 6 compiler by subclassing a C<PCT::HLLCompiler> object.
=cut
.namespace [ 'Perl6';'Compiler' ]
.sub 'onload' :load :init :anon
load_bytecode 'PCT.pbc'
.local pmc p6meta, perl6
p6meta = get_hll_global ['Perl6Object'], '$!P6META'
perl6 = p6meta.'new_class'('Perl6::Compiler', 'parent'=>'PCT::HLLCompiler')
load_bytecode 'config.pbc'
perl6.'language'('Perl6')
perl6.'parsegrammar'('Perl6::Grammar')
perl6.'parseactions'('Perl6::Grammar::Actions')
## set the compilation stages in the @stages attribute
$P0 = split ' ', 'parse past check_syntax post pir evalpmc'
setattribute perl6, '@stages', $P0
## set the command line options
$P0 = split ' ', 'c e=s help|h target=s trace|t=s encoding=s output|o=s version|v'
setattribute perl6, '@cmdoptions', $P0
## set the $usage attribute
$P0 = new 'String'
$P0 = <<'USAGE'
Usage: perl6 [switches] [--] [programfile] [arguments]
-c check syntax only (runs BEGIN and CHECK blocks)
-e program one line of program
-h, --help display this help text
--target=[stage] specify compilation stage to emit
-t, --trace=[flags] enable trace flags
--encoding=[mode] specify string encoding mode
-o, --output=[name] specify name of output file
-v, --version display version information
USAGE
setattribute perl6, '$usage', $P0
## set the $version attribute
.local pmc cfg
$P0 = new 'String'
$P0 = 'This is Rakudo Perl 6'
push_eh _handler
# currently works in the build tree, but not in the install tree
cfg = _config()
$P0 .= ', revision '
$S0 = cfg['revision']
$P0 .= $S0
$P0 .= ' built on parrot '
$S0 = cfg['VERSION']
$P0 .= $S0
$S0 = cfg['DEVEL']
$P0 .= $S0
$P0 .= "\n"
$P0 .= 'for '
$S0 = cfg['archname']
$P0 .= $S0
_handler:
pop_eh
$P0 .= ".\n\nCopyright 2006-2008, The Perl Foundation.\n"
setattribute perl6, '$version', $P0
## create a list for holding the stack of nested blocks
$P0 = new ['List']
set_hll_global ['Perl6';'Grammar';'Actions'], '@?BLOCK', $P0
## create a list for holding the stack of nested package
## declarators
$P0 = new 'List'
set_hll_global ['Perl6';'Grammar';'Actions'], '@?PKGDECL', $P0
## create a list for holding the stack of nested package
## namespaces (we store the namespace as a flat, ::
## separated string for now, for handing to .parse_name)
$P0 = new 'List'
set_hll_global ['Perl6';'Grammar';'Actions'], '@?NS', $P0
## create a (shared) metaclass node
$P0 = get_hll_global ['PAST'], 'Var'
$P0 = $P0.'new'( 'name'=>'metaclass', 'scope'=>'register' )
set_hll_global ['Perl6';'Grammar';'Actions'], '$?METACLASS', $P0
## create the $?CLASSMAP hash
$P0 = new ['Hash']
set_hll_global ['Perl6';'Grammar';'Actions'], '%?CLASSMAP', $P0
## create a list of END blocks to be run
$P0 = new 'List'
set_hll_global ['Perl6'], '@?END_BLOCKS', $P0
## tell PAST::Var how to encode Perl6Str and Str values
$P0 = get_hll_global ['PAST';'Compiler'], '%valflags'
$P0['Perl6Str'] = 'e'
$P0['Str'] = 'e'
.end
.namespace ['Perl6';'Compiler']
=item check_syntax(source [, "option" => value, ...])
Check the syntax of C<source> after PAST tree has been built,
to ensure C<BEGIN> and C<CHECK> blocks have been executed.
=cut
.sub 'check_syntax' :method
.param pmc source
.param pmc adverbs :slurpy :named
$I0 = adverbs['c']
if $I0 goto check_syntax
.return ()
check_syntax:
## if we're here, then syntax is OK
say 'syntax OK'
exit 0
.end
=item main(args :slurpy) :main
Start compilation by passing any command line C<args>
to the Perl 6 compiler.
=cut
.sub 'main' :main
.param pmc args_str
$S0 = args_str[1]
if $S0 != '-le' goto not_harness
exit 0
not_harness:
$P0 = compreg 'Perl6'
$P1 = $P0.'command_line'(args_str, 'encoding'=>'utf8', 'transcode'=>'ascii')
.include 'iterator.pasm'
.local pmc iter
$P0 = get_hll_global ['Perl6'], '@?END_BLOCKS'
iter = new 'Iterator', $P0
iter = .ITERATE_FROM_END
iter_loop:
unless iter goto iter_end
$P0 = pop iter
$P0()
goto iter_loop
iter_end:
.end
.sub 'parse_name' :method
.param string name
## remove any type parameterization for now
.local string type_param
type_param = ''
$I0 = index name, '['
if $I0 == -1 goto type_param_done
type_param = substr name, $I0
name = substr name, 0, $I0
type_param_done:
## divide name based on ::
.local pmc list
list = split '::', name
## move any leading sigil to the last item
.local string sigil
$S0 = list[0]
sigil = substr $S0, 0, 1
$I0 = index '$@%&', $S1
if $I0 < 0 goto sigil_done
substr $S0, 0, 1, ''
list[0] = $S0
$S0 = list[-1]
$S0 = concat sigil, $S0
list[-1] = $S0
sigil_done:
## remove any empty items from the list
$P0 = iter list
list = new 'ResizablePMCArray'
iter_loop:
unless $P0 goto iter_done
$S0 = shift $P0
unless $S0 goto iter_loop
push list, $S0
goto iter_loop
iter_done:
if type_param == '' goto no_add_type_param
$S0 = pop list
concat $S0, type_param
push list, $S0
no_add_type_param:
.return (list)
.end
=back
=cut
.include 'src/gen_setting.pir'
.include 'src/gen_grammar.pir'
.include 'src/parser/expression.pir'
.include 'src/parser/methods.pir'
.include 'src/parser/quote_expression.pir'
.include 'src/gen_actions.pir'
.include 'src/gen_metaop.pir'
.include 'src/gen_junction.pir'
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: