| 1 | use v5.10; | ||
| 2 | use MooseX::Declare; | ||
| 3 | { | ||
| 4 | # STD needs to be important from the main package | ||
| 5 | package main; | ||
| 6 | BEGIN { | ||
| 7 | $main::VERSION = '0.05'; | ||
| 8 | } | ||
| 9 | use STD; | ||
| 10 | } | ||
| 11 | BEGIN {package main;require 'viv'}; | ||
| 12 | class Mildew::Frontend::STD { | ||
| 13 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 14 | use Digest::MD4 qw(md4_hex); | ||
| 15 | has options=>(is=>'ro',default=>sub {{}}); | ||
| 16 | has debug=>(is=>'rw',default=>0); | ||
| 17 | has setting=>(is=>'rw',default=>'MildewCORE'); | ||
| 18 | has tmp=>(is=>'rw',default=>'/tmp/mildew/'); | ||
| 19 | has syml_search_path=>(lazy_build=>1,is=>'rw'); | ||
| 20 | has PERL6LIB=>(lazy_build=>1,is=>'rw'); | ||
| 21 | method BUILD { | ||
| 22 | my ($debug,$setting,$tmp,$syml_search_path,$PERL6LIB); | ||
| 23 | GetOptionsFromArray( | ||
| 24 | ($self->options->{FRONTEND} // []), | ||
| 25 | 'debug' => \$debug, | ||
| 26 | 'setting=s' => \$setting, | ||
| 27 | 'tmp=s' => \$tmp, | ||
| 28 | 'syml-search-path=s' => \$syml_search_path, | ||
| 29 | 'PERL6LIB=s' => \$PERL6LIB, | ||
| 30 | ); | ||
| 31 | $self->debug($debug); | ||
| 32 | $self->setting($setting) if $setting; | ||
| 33 | $self->tmp($tmp.'/') if $tmp; | ||
| 34 | $self->syml_search_path([map {$_.'/'} split(':',$syml_search_path)]) if $syml_search_path; | ||
| 35 | $self->PERL6LIB($PERL6LIB) if $PERL6LIB; | ||
| 36 | } | ||
| 37 | use Scalar::Util qw(reftype); | ||
| 38 | sub prune { | ||
| 39 | my $what = shift; | ||
| 40 | |||
| 41 | state %pruned; | ||
| 42 | return unless defined $what; | ||
| 43 | return if $pruned{$what}++; | ||
| 44 | |||
| 45 | if (reftype $what and reftype $what eq 'HASH') { | ||
| 46 | delete $what->{'.'}; | ||
| 47 | delete $what->{BEG}; | ||
| 48 | delete $what->{END}; | ||
| 49 | delete $what->{WS}; | ||
| 50 | for my $key (keys %{$what}) { | ||
| 51 | prune($what->{$key}); | ||
| 52 | } | ||
| 53 | } elsif (reftype $what and reftype $what eq 'ARRAY') { | ||
| 54 | for (@{$what}) { | ||
| 55 | prune($_); | ||
| 56 | } | ||
| 57 | } else { | ||
| 58 | } | ||
| 59 | } | ||
| 60 | method _build_syml_search_path { | ||
| 61 | require Mildew::Setting::SMOP; | ||
| 62 | [$self->tmp,Mildew::Setting::SMOP::std_tmp_files_path().'/'] | ||
| 63 | } | ||
| 64 | method _build_PERL6LIB { | ||
| 65 | require Mildew::Setting::SMOP; | ||
| 66 | Mildew::Setting::SMOP::standard_libs(); | ||
| 67 | } | ||
| 68 | method parse($source) { | ||
| 69 | $::ORIG = $source; | ||
| 70 | local $ENV{'PERL6LIB'} = $self->PERL6LIB; | ||
| 71 | mkdir($self->tmp); | ||
| 72 | my $m = STD->parse($source, actions=>'STD::Actions',tmp_prefix=>$self->tmp,syml_search_path=>$self->syml_search_path,setting=>$self->setting,filename=>md4_hex($source)); | ||
| 73 | local $ENV{'DEFAULT_SETTING_FOR_MODULES'} = 'MildewCORE'; | ||
| 74 | |||
| 75 | |||
| 76 | if ($self->debug) { | ||
| 77 | require YAML::XS; | ||
| 78 | prune($m); | ||
| 79 | die YAML::XS::Dump($m->{'_ast'}); | ||
| 80 | } | ||
| 81 | $m->{'_ast'}->emit_m0ld; | ||
| 82 | } | ||
| 83 | } |