Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5 - perl6 - create html/perlito5to6.html and "make build-5to6b…
…rowser"
  • Loading branch information
fglock committed Oct 3, 2013
1 parent 9fca914 commit b9edaad
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -145,6 +145,9 @@ build-5js ::
build-5browser ::
perl perlito5.pl -I./src5/lib -Cjs src5/util/perlito5-browser.pl > html/perlito5.js

build-5to6browser ::
perl perlito5.pl -I./src5/lib -Cjs src5/util/perlito5-browser-perl6.pl > html/perlito5to6.js

build-5js3 ::
perl perlito5.pl -I./src5/lib -Cjs3 src5/util/perlito5.pl > perlito5.js

Expand Down
6 changes: 3 additions & 3 deletions html/perlito5.html
@@ -1,12 +1,12 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>"Perlito" Perl 5 Compiler</title>
<title>"Perlito" Perl5 Compiler</title>
</head>
<body>
<h1><a href="http://www.perlito.org">"Perlito" Perl 5 Compiler</a></h1>
<h1><a href="http://www.perlito.org">"Perlito" Perl5 Compiler</a></h1>

<p>Perlito is a compiler collection that implements a Perl 5 and a Perl 6 compiler.</p>
<p>Perlito is a compiler collection that implements a Perl5 and a Perl6 compiler.</p>

<p>Main Perlito repository: <a href="http://github.com/fglock/Perlito">http://github.com/fglock/Perlito</a></p>

Expand Down
88 changes: 88 additions & 0 deletions html/perlito5to6.html
@@ -0,0 +1,88 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>"Perlito" Perl5 to Perl6 Compiler</title>
</head>
<body>
<h1><a href="http://www.perlito.org">"Perlito" Perl5 to Perl6 Compiler</a></h1>

<p>Perlito is a compiler collection that implements a Perl5 and a Perl6 compiler.</p>

<p>Main Perlito repository: <a href="http://github.com/fglock/Perlito">http://github.com/fglock/Perlito</a></p>

<script type="text/javascript" src="perlito5to6.js"></script>

<p>Source program:</p>
<textarea id="source" cols="70" rows="10">
use v5;
use strict;
use feature 'say';

package Main;

say <<"HELLO";
"Perlito Perl5" running in $^O
HELLO

my @x = qw/ x y z /;
print $x[1], "\n";

</textarea><br/>
<input type="button" value="Execute" onclick="execute()"/>

<p>Compiler log:</p>
<textarea id="log-result" readonly="true" cols="70" rows="5"></textarea><br/>

<p>Compiled to Perl6:</p>
<textarea id="js-result" readonly="true" cols="70" rows="5"></textarea><br/>

<script type="text/javascript">

function execute() {

p5pkg.CORE.print = function(List__) {
var i;
for (i = 0; i < List__.length; i++) {
document.getElementById('log-result').value += p5str(List__[i]);
}
return true;
};
p5pkg.CORE.warn = function(List__) {
var i;
List__.push("\n");
for (i = 0; i < List__.length; i++) {
document.getElementById('log-result').value += p5str(List__[i]);
}
return true;
};
p5pkg["main"]["v_^O"] = "browser";
p5pkg["main"]["Hash_INC"]["Perlito5/strict.pm"] = "Perlito5/strict.pm";
p5pkg["main"]["Hash_INC"]["Perlito5/warnings.pm"] = "Perlito5/warnings.pm";

var source = document.getElementById('source').value;
var pos = 0;
var ast;
var match;
document.getElementById('log-result').value = "";
document.getElementById('js-result').value = "";
try {
// compile
document.getElementById('log-result').value += "Compiling.\n";
var start = new Date().getTime();
var js_source = p5pkg["Perlito5"].compile_p5_to_p6([source]);
var end = new Date().getTime();
var time = end - start;
document.getElementById('log-result').value += "Compilation time: " + time + "ms\n";
document.getElementById('js-result').value += js_source + ";\n";

p5pkg.CORE.print(["\nDone.\n"]);
}
catch(err) {
document.getElementById('log-result').value += "Error:\n";
document.getElementById('log-result').value += err + "\n";
document.getElementById('log-result').value += "Compilation aborted.\n";
}
}
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions perlito5.pl
Expand Up @@ -12567,6 +12567,9 @@ package Perlito5::AST::Apply;
if (($code eq 'list:<.>')) {
return ('(' . join(' ~ ', map(Perlito5::Perl6::to_str($_), @{$self->{'arguments'}})) . ')')
};
if (($code eq 'list:<,>')) {
return ('(' . join(', ', map($_->emit_perl6(), @{$self->{'arguments'}})) . ')')
};
if (($code eq 'ternary:<? :>')) {
return (Perlito5::Perl6::tab($level) . '( ' . Perlito5::Perl6::to_bool($self->{'arguments'}->[0]) . ' ?? ' . ($self->{'arguments'}->[1])->emit_perl6() . ' !! ' . ($self->{'arguments'}->[2])->emit_perl6() . ')')
};
Expand Down
10 changes: 8 additions & 2 deletions src5/lib/Perlito5/Perl6/Emitter.pm
Expand Up @@ -484,14 +484,20 @@ package Perlito5::AST::Apply;

if ($code eq 'prefix:<+>') { return '+(' . $self->{arguments}->[0]->emit_perl6() . ')' }

if ($code eq 'list:<.>')
{
if ($code eq 'list:<.>') {
return '('
. join( ' ~ ',
map( Perlito5::Perl6::to_str($_), @{$self->{arguments}} )
)
. ')'
}
if ($code eq 'list:<,>') {
return '('
. join( ', ',
map( $_->emit_perl6, @{$self->{arguments}} )
)
. ')'
}

if ($code eq 'ternary:<? :>') {
return Perlito5::Perl6::tab($level)
Expand Down
29 changes: 29 additions & 0 deletions src5/util/perlito5-browser-perl6.pl
@@ -0,0 +1,29 @@
package Perlito5;

use strict;
use warnings;

use Perlito5::Match;
use Perlito5::Grammar;
use Perlito5::Grammar::Control;
use Perlito5::Grammar::Precedence;
use Perlito5::Grammar::Expression;
use Perlito5::Macro;
use Perlito5::Runtime;

use Perlito5::Perl6::Emitter;

sub compile_p5_to_p6 {
my $s = shift;
$Perlito5::PKG_NAME = 'main';
$Perlito5::PROTO = {};
my $ast = Perlito5::Grammar->exp_stmts($s, 0);
Perlito5::AST::CompUnit::emit_perl6_program(
[
Perlito5::AST::CompUnit->new( name => 'main', body => Perlito5::Match::flat($ast) )
]
);
}

1;

0 comments on commit b9edaad

Please sign in to comment.