From 92aafc83c1d57ab2f7cbe7cc09681327945598db Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Fri, 23 Jul 2010 10:02:57 -0700 Subject: [PATCH] Unify handling of setting and programs in CompilerDriver --- CompilerDriver.pm | 50 +++++++++++++++++++++++------------------------ Makefile | 4 ++-- Unit.pm | 10 +++++----- niecza_eval | 3 +-- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/CompilerDriver.pm b/CompilerDriver.pm index 65c51f38..56d833c3 100644 --- a/CompilerDriver.pm +++ b/CompilerDriver.pm @@ -4,7 +4,7 @@ use warnings; use 5.010; use Sub::Exporter -setup => { - exports => [ qw(header setting mainline ast) ] + exports => [ qw(compile) ] }; open ::NIECZA_OUT, ">&", \*STDOUT; @@ -23,43 +23,43 @@ use Storable; use Niecza::Grammar (); use Niecza::Actions (); -sub header { print ::NIECZA_OUT <parsefile("CORE.setting", - setting => 'NULL', actions => 'Niecza::Actions')->{_ast}; - $setting_ast->write; - store $::SETTING_RESUME, 'CORE_ast.store'; -} + $::SETTING_RESUME = retrieve($args{lang} . '_ast.store') + unless $args{lang} eq 'NULL'; -sub mainline { - my $code = shift; - local $::UNITNAME = ''; - local $::SETTING_RESUME = retrieve 'CORE_ast.store'; - $STD::ALL = {}; - Niecza::Grammar->parse($code, actions => 'Niecza::Actions')->{_ast}->write; -} + my ($m, $a) = $args{file} ? ('parsefile', $args{file}) : + ('parse', $args{code}); + my $ast = Niecza::Grammar->$m($a, setting => $args{lang}, + actions => 'Niecza::Actions')->{_ast}; -sub ast { - my $code = shift; - local $::UNITNAME = 'Mainline'; - $STD::ALL = {}; - my $a = Niecza::Grammar->parse($code, actions => 'Niecza::Actions')->{_ast}; - delete $a->mainline->{outer}; - delete $a->{setting}; - print YAML::XS::Dump($a); + if ($args{ast}) { + delete $a->mainline->{outer}; + delete $a->{setting}; + print STDOUT YAML::XS::Dump($a); + return; + } + + $::SETTING_RESUME = undef; + $ast->write; + store $::SETTING_RESUME, ($::UNITNAME . '_ast.store') + if $::SETTING_RESUME; } 1; diff --git a/Makefile b/Makefile index c1e60a20..80ab1366 100644 --- a/Makefile +++ b/Makefile @@ -8,14 +8,14 @@ all: CORE.dll git rev-parse HEAD | cut -c1-7 > VERSION test: $(COMPILER) test.pl CORE.dll - perl -MFile::Slurp -MCompilerDriver=:all -e 'header; mainline(scalar read_file("test.pl"))' > Program.cs + perl -MFile::Slurp -MCompilerDriver=:all -e 'compile(main => 1, file => "test.pl")' > Program.cs gmcs /r:Kernel.dll /r:CORE.dll Program.cs prove -e 'mono --debug=casts' Program.exe .DELETE_ON_ERROR: CORE.cs: $(COMPILER) CORE.setting - perl -MCompilerDriver=:all -e 'header; setting' > CORE.cs + perl -MCompilerDriver=:all -e 'compile(lang => "NULL", file => "CORE.setting")' > CORE.cs Kernel.dll: Kernel.cs gmcs /target:library /out:Kernel.dll Kernel.cs diff --git a/Unit.pm b/Unit.pm index 59169404..06d08f57 100644 --- a/Unit.pm +++ b/Unit.pm @@ -22,24 +22,24 @@ use 5.010; sub gen_code { my ($self) = @_; $self->mainline->outer($self->setting) if $self->setting; - CodeGen->know_module($self->setting_name); + CodeGen->know_module($self->csname($self->setting_name)); CodeGen->know_module($self->csname); CodeGen->new(name => 'BOOT', entry => 1, ops => CgOp::letn('pkg', CgOp::rawsget('Kernel.Global'), - CgOp::rawscall($self->setting_name . '.Initialize'), + CgOp::rawscall($self->csname($self->setting_name) . '.Initialize'), CgOp::letn('protopad', - CgOp::cast('Frame', CgOp::rawsget($self->setting_name . + CgOp::cast('Frame', CgOp::rawsget($self->csname($self->setting_name) . '.Environment')), ($self->is_setting ? CgOp::rawsset($self->csname . '.Installer', CgOp::protosub($self->mainline)) : - CgOp::subcall(CgOp::rawsget($self->setting_name . '.Installer'), + CgOp::subcall(CgOp::rawsget($self->csname($self->setting_name) . '.Installer'), CgOp::newscalar(CgOp::protosub($self->mainline)))), CgOp::return()))); } sub csname { - my $x = $_[0]->name; + my $x = $_[1] // $_[0]->name; $x =~ s/::/./g; $x ||= 'MAIN'; $x; diff --git a/niecza_eval b/niecza_eval index 4d0f432f..6ac9ca3c 100755 --- a/niecza_eval +++ b/niecza_eval @@ -36,8 +36,7 @@ sub run { return; } open ::NIECZA_OUT, ">", "Program.cs"; - header; - mainline(shift); + compile(main => 1, code => shift(), ast => $ast); close ::NIECZA_OUT; system 'gmcs /r:Kernel.dll /r:CORE.dll Program.cs';