Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: c5cd4cde3d
Fetching contributors…

Cannot retrieve contributors at this time

executable file 246 lines (214 sloc) 6.697 kB
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
Jump to Line
Something went wrong with that request. Please try again.