From 8605d1d17ca1b95a448b52fa44d21a2c47cca1ae Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Sun, 11 Jul 2010 16:41:34 -0700 Subject: [PATCH] Expose the preinit/run split to the entry point --- CodeGen.pm | 21 ++++++++++++--------- CompilerDriver.pm | 36 +++++++++++++++++++++++++++++++++++- Niecza/Actions.pm | 2 +- Unit.pm | 32 +++++++++++++++----------------- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/CodeGen.pm b/CodeGen.pm index 586e8bcd..bb826a9f 100644 --- a/CodeGen.pm +++ b/CodeGen.pm @@ -45,6 +45,7 @@ use 5.010; has name => (isa => 'Str', is => 'ro'); has uid => (isa => 'Int', is => 'ro', default => sub { ++(state $i) }); + has entry => (isa => 'Bool', is => 'ro', default => 0); has depth => (isa => 'Int', is => 'rw', default => 0); has maxdepth => (isa => 'Int', is => 'rw', default => 0); has savedepth => (isa => 'Int', is => 'rw', default => 0); @@ -503,6 +504,7 @@ use 5.010; sub csname { my ($self) = @_; + return $self->name if $self->entry; my @name = split /\W+/, $self->name; shift @name if @name && $name[0] eq ''; join("", (map { ucfirst $_ } @name), "_", $self->uid, "C"); @@ -511,19 +513,20 @@ use 5.010; sub write { my ($self) = @_; my $name = $self->csname; - print " " x 8, "private static Frame $name(Frame th) {\n"; - print " " x 12, "Console.WriteLine(\"Entering $name @ \" + th.ip);\n"; + my $vis = ($self->entry ? 'public' : 'private'); + print " " x 4, "$vis static Frame $name(Frame th) {\n"; + print " " x 8, "Console.WriteLine(\"Entering $name @ \" + th.ip);\n"; if ($self->maxdepth) { - print " " x 12, "object " . join(", ", map { "s$_" } + print " " x 8, "object " . join(", ", map { "s$_" } 0 .. ($self->maxdepth - 1)) . ";\n"; } - print " " x 12, "switch (th.ip) {\n"; - print " " x 16, "case 0:\n"; - print " " x 16, $_ for @{ $self->buffer }; - print " " x 16, "default:\n"; - print " " x 20, "throw new Exception(\"Invalid IP\");\n"; - print " " x 12, "}\n"; + print " " x 8, "switch (th.ip) {\n"; + print " " x 12, "case 0:\n"; + print " " x 12, $_ for @{ $self->buffer }; + print " " x 12, "default:\n"; + print " " x 16, "throw new Exception(\"Invalid IP\");\n"; print " " x 8, "}\n"; + print " " x 4, "}\n"; } __PACKAGE__->meta->make_immutable; diff --git a/CompilerDriver.pm b/CompilerDriver.pm index 9df9d51e..800777d8 100644 --- a/CompilerDriver.pm +++ b/CompilerDriver.pm @@ -10,4 +10,38 @@ use Op (); use Niecza::Grammar (); use Niecza::Actions (); -Niecza::Grammar->parsefile("setting", setting => 'NULL', actions => 'Niecza::Actions')->{_ast}->write; +{ + local $::UNITNAME = 'Mainline'; + Niecza::Grammar->parsefile("setting", setting => 'NULL', actions => 'Niecza::Actions')->{_ast}->write; +} + +print <sl_to_block($M->{statementlist}{_ast}, subname => 'mainline'); - $M->{_ast} = Unit->new(mainline => $body); + $M->{_ast} = Unit->new(mainline => $body, name => $::UNITNAME); } 1; diff --git a/Unit.pm b/Unit.pm index a96b5c93..f0f56777 100644 --- a/Unit.pm +++ b/Unit.pm @@ -2,12 +2,17 @@ use strict; use warnings; use 5.010; +# A Unit generates a CLR class with a BOOT member +# All used Units except for the setting go into Niecza.Kernel.Units +# The main program generates a main class, which sets up Units and runs the +# setting +# BOOT subs take one argument, the outer protopad { package Unit; use Moose; has mainline => (isa => 'Body', is => 'ro', required => 1); - - has codegen => (isa => 'CodeGen', is => 'rw'); + has name => (isa => 'Str', is => 'ro', required => 1); + has codegen => (isa => 'CodeGen', is => 'rw'); sub code { my ($self) = @_; @@ -15,16 +20,17 @@ use 5.010; if ($cg) { return $cg; } else { - $self->codegen($cg = CodeGen->new(name => 'boot')); + $self->codegen($cg = CodeGen->new(name => 'BOOT', entry => 1)); $cg->new_aux('protopad', 'Frame'); $cg->new_aux('how', 'Variable'); - $cg->push_null('Frame'); + $cg->pos(0); + $cg->fetch; + $cg->cast('Frame'); $cg->push_aux('protopad'); $cg->open_protopad; $self->mainline->do_preinit($cg); $cg->close_sub($self->mainline->code); - $cg->call_sub(0,0); - $cg->return; + $cg->return(1); return $cg; } } @@ -35,20 +41,12 @@ use 5.010; print <code->csname ]})); - Frame current = root_f; - while (current != null) { - current = current.Continue(); - } - } +using Niecza; +public class @{[ $self->name ]} { EOH $self->code->write; $self->mainline->write; - print " }\n}\n" + print "}\n" } __PACKAGE__->meta->make_immutable;