Permalink
Browse files

Perlito5 - perlito5.js: add -Bjs option

  • Loading branch information...
1 parent 965f687 commit 155b839c9f981fa7eb3e03f72899f6c75ce6d510 @fglock committed Mar 11, 2012
Showing with 80 additions and 57 deletions.
  1. +1 −0 ChangeLog
  2. +5 −0 README-perlito5
  3. +27 −22 perlito5.pl
  4. +47 −35 src5/util/perlito5.pl
View
@@ -6,6 +6,7 @@ Dev 2012-02-29
-- moved the non-standard "token" grammar to a module
- here-docs
- new Perl5-in-Perl6 backend (experimental)
+- new option -Bjs
- perl5 backend:
-- use native data structures and ops
View
@@ -11,6 +11,11 @@ Running the tests using perl6:
. util-perl6/setup-perlito5-perl6.sh
find t5/*.t | perl -ne ' print "*** $_"; chomp; print ` perl -Ilib5 perlito5.pl -Cperl6 $_ > tmp.p6 && perl6 tmp.p6 ` '
+Running the tests using node.js:
+
+ # see below how to create perlito5.js
+ node perlito5.js -Bjs t5/01-sanity.t
+
Compile the compiler to Javascript into perlito5.js:
-- using perlito5.pl:
View
@@ -104,7 +104,7 @@ sub Perlito::add_comp_unit {
($backend = substr($ARGV[0], 2, 10));
($execute = 1);
shift(@ARGV);
- if ((((($backend eq 'perl5')) || (($backend eq 'python'))) || (($backend eq 'ruby')))) {
+ if ((((($backend eq 'perl5') || ($backend eq 'python')) || ($backend eq 'ruby')) || ($backend eq 'js'))) {
($expand_use = 0)
}
};
@@ -116,7 +116,7 @@ sub Perlito::add_comp_unit {
else {
if ((((($ARGV[0] eq '-h')) || (($ARGV[0] eq '--help'))) || (($backend eq '')))) {
($backend = '');
- Perlito5::Runtime::say($_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION, chr(10) . 'perlito5 [switches] [programfile]' . chr(10) . ' switches:' . chr(10) . ' -h --help' . chr(10) . ' -v --verbose' . chr(10) . ' -V --version' . chr(10) . ' -Ctarget target backend: js, perl5, perl6' . chr(10) . ' --expand_use --noexpand_use' . chr(10) . ' expand ' . chr(39) . 'use' . chr(39) . ' statements at compile time' . chr(10) . ' -e program one line of program (omit programfile)' . chr(10));
+ Perlito5::Runtime::say($_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION, chr(10) . 'perlito5 [switches] [programfile]' . chr(10) . ' switches:' . chr(10) . ' -h --help' . chr(10) . ' -v --verbose' . chr(10) . ' -V --version' . chr(10) . ' -Ctarget target backend: js, perl5, perl6' . chr(10) . ' -Btarget execute using the backend: js' . chr(10) . ' --expand_use --noexpand_use' . chr(10) . ' expand ' . chr(39) . 'use' . chr(39) . ' statements at compile time' . chr(10) . ' -e program one line of program (omit programfile)' . chr(10));
shift(@ARGV)
}
};
@@ -148,27 +148,32 @@ sub Perlito::add_comp_unit {
};
($Perlito5::PKG_NAME = 'main');
($Perlito5::PROTO = {});
- ((my $m) = Perlito5::Grammar->exp_stmts($source, 0));
- if (($m->{'to'} != length($source))) {
- die('Syntax Error near ', $m->{'to'})
- };
- add_comp_unit($m->flat());
- ($comp_units = [Perlito5::AST::CompUnit->new(('name' => 'main'), ('body' => $comp_units))]);
- if (($backend eq 'perl5')) {
- Perlito5::Runtime::say('# Do not edit this file - Generated by ', $_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION);
- print(Perlito5::AST::CompUnit::emit_perl5_program($comp_units))
- };
- if (($backend eq 'perl6')) {
- Perlito5::Runtime::say('# Do not edit this file - Generated by ', $_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION);
- print(Perlito5::AST::CompUnit::emit_perl6_program($comp_units))
- };
- if (($backend eq 'js')) {
- Perlito5::Runtime::say('// Do not edit this file - Generated by ', $_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION);
- if ($expand_use) {
- print(Perlito5::Javascript::Runtime->emit_javascript());
- print(Perlito5::Javascript::CORE->emit_javascript())
+ if ($execute) {
+ eval($source)
+ }
+ else {
+ ((my $m) = Perlito5::Grammar->exp_stmts($source, 0));
+ if (($m->{'to'} != length($source))) {
+ die('Syntax Error near ', $m->{'to'})
};
- print(Perlito5::AST::CompUnit::emit_javascript_program($comp_units))
+ add_comp_unit($m->flat());
+ ($comp_units = [Perlito5::AST::CompUnit->new(('name' => 'main'), ('body' => $comp_units))]);
+ if (($backend eq 'perl5')) {
+ Perlito5::Runtime::say('# Do not edit this file - Generated by ', $_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION);
+ print(Perlito5::AST::CompUnit::emit_perl5_program($comp_units))
+ };
+ if (($backend eq 'perl6')) {
+ Perlito5::Runtime::say('# Do not edit this file - Generated by ', $_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION);
+ print(Perlito5::AST::CompUnit::emit_perl6_program($comp_units))
+ };
+ if (($backend eq 'js')) {
+ Perlito5::Runtime::say('// Do not edit this file - Generated by ', $_V6_COMPILER_NAME, ' ', $_V6_COMPILER_VERSION);
+ if ($expand_use) {
+ print(Perlito5::Javascript::Runtime->emit_javascript());
+ print(Perlito5::Javascript::CORE->emit_javascript())
+ };
+ print(Perlito5::AST::CompUnit::emit_javascript_program($comp_units))
+ }
}
};
View
@@ -123,7 +123,12 @@ sub add_comp_unit {
$backend = substr($ARGV[0], 2, 10);
$execute = 1;
shift @ARGV;
- if (($backend eq 'perl5') || ($backend eq 'python') || ($backend eq 'ruby')) {
+ if ( $backend eq 'perl5'
+ || $backend eq 'python'
+ || $backend eq 'ruby'
+ || $backend eq 'js'
+ )
+ {
$expand_use = 0;
}
}
@@ -141,6 +146,7 @@ sub add_comp_unit {
-v --verbose
-V --version
-Ctarget target backend: js, perl5, perl6
+ -Btarget execute using the backend: js
--expand_use --noexpand_use
expand 'use' statements at compile time
-e program one line of program (omit programfile)
@@ -177,44 +183,50 @@ sub add_comp_unit {
$Perlito5::PKG_NAME = 'main';
$Perlito5::PROTO = {};
- my $m = Perlito5::Grammar->exp_stmts($source, 0);
- die "Syntax Error near ", $m->{"to"}
- if $m->{"to"} != length($source);
- add_comp_unit($m->flat());
-
- $comp_units = [
- Perlito5::AST::CompUnit->new(
- name => 'main',
- body => $comp_units,
- ),
- ];
-
- # if ($backend eq 'ast-perl5') {
- # say "# AST dump - do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
- # # TODO - use Data::Dumper
- # say "$comp_units";
- # }
- if ($backend eq 'perl5') {
- say "# Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
- print Perlito5::AST::CompUnit::emit_perl5_program( $comp_units );
- }
- if ($backend eq 'perl6') {
- say "# Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
- print Perlito5::AST::CompUnit::emit_perl6_program( $comp_units );
+
+ if ( $execute ) {
+ eval $source;
}
- if ($backend eq 'js') {
- say "// Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
+ else {
- if ( $expand_use ) {
- print Perlito5::Javascript::Runtime->emit_javascript();
- print Perlito5::Javascript::CORE->emit_javascript();
+ my $m = Perlito5::Grammar->exp_stmts($source, 0);
+ die "Syntax Error near ", $m->{"to"}
+ if $m->{"to"} != length($source);
+ add_comp_unit($m->flat());
+
+ $comp_units = [
+ Perlito5::AST::CompUnit->new(
+ name => 'main',
+ body => $comp_units,
+ ),
+ ];
+
+ # if ($backend eq 'ast-perl5') {
+ # say "# AST dump - do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
+ # # TODO - use Data::Dumper
+ # say "$comp_units";
+ # }
+ if ($backend eq 'perl5') {
+ say "# Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
+ print Perlito5::AST::CompUnit::emit_perl5_program( $comp_units );
+ }
+ if ($backend eq 'perl6') {
+ say "# Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
+ print Perlito5::AST::CompUnit::emit_perl6_program( $comp_units );
+ }
+ if ($backend eq 'js') {
+ say "// Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
+ if ( $expand_use ) {
+ print Perlito5::Javascript::Runtime->emit_javascript();
+ print Perlito5::Javascript::CORE->emit_javascript();
+ }
+ print Perlito5::AST::CompUnit::emit_javascript_program( $comp_units );
}
- print Perlito5::AST::CompUnit::emit_javascript_program( $comp_units );
+ # if ($backend eq 'java') {
+ # say "// Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
+ # print Perlito5::AST::CompUnit::emit_java_program( $comp_units );
+ # }
}
- # if ($backend eq 'java') {
- # say "// Do not edit this file - Generated by ", $_V6_COMPILER_NAME, " ", $_V6_COMPILER_VERSION;
- # print Perlito5::AST::CompUnit::emit_java_program( $comp_units );
- # }
}
=pod

0 comments on commit 155b839

Please sign in to comment.