/
perlito5.pl
executable file
·245 lines (214 loc) · 6.54 KB
/
perlito5.pl
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
239
240
241
242
243
244
use v5;
package Perlito;
use Perlito5::Match;
use Perlito5::Emitter::Token;
use Perlito5::Expression;
use Perlito5::Grammar::Control;
use Perlito5::Grammar::Regex;
use Perlito5::Grammar;
use Perlito5::Javascript::Emitter;
use Perlito5::Javascript::Runtime;
use Perlito5::Javascript::CORE;
use Perlito5::Javascript::IO;
use Perlito5::Macro;
use Perlito5::Perl5::Emitter;
use Perlito5::Perl6::Emitter;
use Perlito5::Precedence;
## use Perlito5::Python::Emitter;
## use Perlito5::Ruby::Emitter;
use Perlito5::Runtime;
use Perlito5::Dumper;
my $_V5_COMPILER_NAME = 'Perlito5';
my $_V5_COMPILER_VERSION = '9.0';
my $source = '';
my $backend = '';
my $execute = 0;
my $verbose = 0;
my $expand_use = 1;
if ($verbose) {
warn "// Perlito5 compiler";
warn "// ARGV: @ARGV";
}
if (($ARGV[0] eq '-v') || ($ARGV[0] eq '--verbose')) {
$verbose = 1;
shift @ARGV;
}
while (substr($ARGV[0], 0, 2) eq '-I') {
$lib = substr($ARGV[0], 2, 10);
unshift @INC, $lib;
shift @ARGV;
}
if (substr($ARGV[0], 0, 2) eq '-C') {
$backend = substr($ARGV[0], 2, 10);
$execute = 0;
shift @ARGV;
if ( $backend eq 'perl5'
|| $backend eq 'python'
|| $backend eq 'ruby'
|| $backend eq 'perl6'
)
{
$expand_use = 0;
}
}
if (substr($ARGV[0], 0, 2) eq '-B') {
$backend = substr($ARGV[0], 2, 10);
$execute = 1;
shift @ARGV;
if ( $backend eq 'perl5'
|| $backend eq 'python'
|| $backend eq 'ruby'
|| $backend eq 'js'
)
{
$expand_use = 0;
}
}
if (($ARGV[0] eq '-V') || ($ARGV[0] eq '--version')) {
$backend = '';
say $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION;
shift @ARGV;
}
elsif (($ARGV[0] eq '-h') || ($ARGV[0] eq '--help') || ($backend eq '')) {
$backend = '';
say $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION, "
perlito5 [switches] [programfile]
switches:
-h --help
-v --verbose
-V --version
-Idirectory specify \@INC/include directory (several -I's allowed)
-Ctarget target backend: js, perl5, perl6
-Cast-perl5 emits a dump of the abstract syntax tree
-Btarget execute using the backend: js
--expand_use --noexpand_use
expand 'use' statements at compile time
-e program one line of program (omit programfile)
";
shift @ARGV;
}
if ($ARGV[0] eq '--expand_use') {
$expand_use = 1;
shift @ARGV;
}
if ($ARGV[0] eq '--noexpand_use') {
$expand_use = 0;
shift @ARGV;
}
if ($backend && @ARGV) {
if ($ARGV[0] eq '-e') {
shift @ARGV;
if ($verbose) {
warn "// source from command line: ", $ARGV[0];
}
$source = shift @ARGV;
}
else {
if ($verbose) {
warn "// source from file: ", $ARGV[0];
}
$source = Perlito5::IO::slurp( shift @ARGV );
}
if ($verbose) {
warn "// backend: ", $backend;
warn "now parsing";
}
$Perlito5::PKG_NAME = 'main';
$Perlito5::PROTO = {};
if ( $execute ) {
$Perlito5::EXPAND_USE = 1;
my $ok;
eval "package main; no strict; $source ; \$ok = 1";
if ( !$ok ) {
my $error = $@
|| "Unknown error";
warn $error;
}
}
else {
# since we are generating code that will run from scratch,
# we need to start with an empty %INC so that all modules are "used"
%INC = ();
# partially disable "use"
$Perlito5::EXPAND_USE = 0;
my $m;
my $ok;
eval {
$m = Perlito5::Grammar->exp_stmts($source, 0);
$ok = 1;
};
if ( !$ok
|| $m->{"to"} != length($source)
)
{
my $error = $@
|| ( $m->{"to"} != length($source)
&& "Syntax Error near " . $m->{"to"}
)
|| "Unknown error";
warn $error;
}
else {
my $comp_units;
if ($expand_use) {
$comp_units = Perlito5::Grammar::Use::add_comp_unit(Perlito5::Match::flat($m))
}
else {
$comp_units = Perlito5::Match::flat($m);
}
$comp_units = [
Perlito5::AST::CompUnit->new(
name => 'main',
body => $comp_units,
),
];
if ($backend eq 'perl5') {
say "# Do not edit this file - Generated by ", $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION;
print Perlito5::AST::CompUnit::emit_perl5_program( $comp_units );
}
if ($backend eq 'perl6') {
say "# Do not edit this file - Generated by ", $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION;
print Perlito5::AST::CompUnit::emit_perl6_program( $comp_units );
}
if ($backend eq 'js') {
say "// Do not edit this file - Generated by ", $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION;
if ( $expand_use ) {
print Perlito5::Javascript::Runtime->emit_javascript();
print Perlito5::Javascript::CORE->emit_javascript();
print Perlito5::Javascript::IO->emit_javascript();
}
print Perlito5::AST::CompUnit::emit_javascript_program( $comp_units );
}
# if ($backend eq 'java') {
# say "// Do not edit this file - Generated by ", $_V5_COMPILER_NAME, " ", $_V5_COMPILER_VERSION;
# print Perlito5::AST::CompUnit::emit_java_program( $comp_units );
# }
if ($backend eq 'ast-perl5') {
say Perlito5::Dumper::Dumper( $comp_units );
}
elsif ($backend eq 'ast-pretty') {
eval 'use Data::Printer {colored=>1,class=>{expand=>"all",show_methods=>"none"}};p($comp_units);1';
print $@;
}
}
}
}
=pod
=head1 NAME
perlito5 - Perl5 compiler
=head1 SYNOPSIS
perlito5 -Cjs program.pl
=head1 DESCRIPTION
This program reads Perl5 source code and generates native code.
The compiler options are available with the command:
perlito5 -h
=head1 AUTHORS
Flavio Soibelmann Glock <fglock@gmail.com>.
=head1 SEE ALSO
L<http://www.perlito.org>
=head1 COPYRIGHT
Copyright 2011, 2012 by Flavio Soibelmann Glock 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>
=cut