Skip to content

Commit

Permalink
RakuAST: some more actions/grammar streamlining
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Aug 17, 2023
1 parent 86557c9 commit 205fb66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
25 changes: 13 additions & 12 deletions src/Raku/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
my $lock := NQPLock.new;
sub next-id() { $lock.protect({ ++$count }) }

# Given a package, returns a low-level hash for its stash
sub stash-hash($package) {
my $hash := $package.WHO;
nqp::ishash($hash)
?? $hash
!! $hash.FLATTENABLE_HASH
}

# Perform all actions that are needed before any actual parsing can
# be done by a grammar.
method comp-unit-prologue($/) {
Expand Down Expand Up @@ -177,7 +185,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {

my sub resolver-from-revision() {
$setting-name := 'CORE.' ~ $comp.lvs.p6rev($language-revision);
$*R.set-setting(:setting-name($setting-name));
$*R.set-setting(:$setting-name);
}

if $<version> {
Expand Down Expand Up @@ -272,8 +280,9 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
}

# Locate an EXPORTHOW and set those mappings on our current language.
my $EXPORTHOW := $*R.resolve-lexical-constant('EXPORTHOW').compile-time-value;
for stash_hash($EXPORTHOW) {
my $EXPORTHOW :=
$*R.resolve-lexical-constant('EXPORTHOW').compile-time-value;
for stash-hash($EXPORTHOW) {
$*LANG.set_how($_.key, $_.value);
}

Expand Down Expand Up @@ -305,14 +314,6 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
$*LITERALS.set-resolver($*R);
}

sub stash_hash($pkg) {
my $hash := $pkg.WHO;
unless nqp::ishash($hash) {
$hash := $hash.FLATTENABLE_HASH();
}
$hash
}

method comp-unit($/) {
# Do dynamic lookups once
my $COMPUNIT := $*CU;
Expand Down Expand Up @@ -386,7 +387,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
method statement($/) {
my $trace := $/.pragma('trace') ?? 1 !! 0;
# statement ID needs to be captured before creation of statement object
my $statement-id := $*STATEMENT_ID;
my $statement-id := $*STATEMENT-ID;
my $statement;

if $<EXPR> {
Expand Down
43 changes: 23 additions & 20 deletions src/Raku/Grammar.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,6 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
my $*LASTQUOTE := [0,0]; # for runaway quote detection
my $*SORRY_REMAINING := 10; # decremented on each sorry; panic when 0
my $*BORG := {}; # who gets blamed for a missing block
my $*ORIGIN-SOURCE; # where we get source code information from
my @*ORIGIN-NESTINGS := []; # this will be used for the CompUnit object
my $*COMPILING_CORE_SETTING := 0;
# -1 indicates we're outside of any "supply" or "react" block
my $*WHENEVER-COUNT := -1;
Expand Down Expand Up @@ -641,25 +638,31 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
token comp-unit-prologue { <?> }
token comp-unit($outer-cu) {
<.bom>?
<.bom>? # ignore any ByteOrderMark
# Set up compilation unit and symbol resolver according to the language
# version that is declared, if any.
:my $*CU;
:my $*R;
:my $*LITERALS;
:my $*EXPORT;
:my $*IN-TYPENAME;
:my $*CU; # current RakuAST::CompUnit object
:my $*ORIGIN-SOURCE; # current RakuAST::Origin::Source object
:my @*ORIGIN-NESTINGS := []; # handling nested origins
:my $*R; # current RakuAST::Resolver::xxx object
:my $*LITERALS; # current RakuAST::LiteralBuilder object
<.comp-unit-prologue> # set the above variables
:my $*IN-TYPENAME; # fallback for inside typename flag
:my $*FAKE-INFIX-FOUND; # fallback for fake infix handling
:my @*LEADING-DOC := []; # temp storage leading declarator doc
:my $*DECLARAND; # target for trailing declarator doc
:my $*LAST-TRAILING-LINE := -1; # number of last line with trailing doc
:my $*IGNORE-NEXT-DECLARAND; # True if next declarand to be ignored
:my $*DECLARAND-WORRIES := {}; # $/ of worries when clearing DECLARAND
:my $*NEXT_STATEMENT_ID := 1; # to give each statement an ID
:my $*FAKE-INFIX-FOUND := 0;
:my $*begin_compunit := 1; # at start of a compilation unit?
<.comp-unit-prologue>
<.lang_setup($outer-cu)>
:my $*EXPORT;
:my $*COMPILING_CORE_SETTING := 0;
:my $*NEXT-STATEMENT-ID := 0; # to give each statement an ID
:my $*START-OF-COMPUNIT := 1; # flag: start of a compilation unit?
<.lang_setup($outer-cu)> # set the above variables
{ $*R.enter-scope($*CU); $*R.create-scope-implicits(); }
<load_command_line_modules>
Expand Down Expand Up @@ -730,7 +733,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
token statement {
:my $*QSIGIL := '';
:my $*SCOPE := '';
:my $*STATEMENT_ID := $*NEXT_STATEMENT_ID++;
:my $*STATEMENT-ID := ++$*NEXT-STATEMENT-ID;
:my $actions := self.slang_actions('MAIN');
<!!{ $/.set_actions($actions); 1 }>
Expand Down Expand Up @@ -2379,11 +2382,11 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
<trait($*PACKAGE)>*
<.enter-package-scope($<signature>)>
[
|| <?[{]> { $*begin_compunit := 0; } <block>
|| <?[{]> { $*START-OF-COMPUNIT := 0; } <block>
|| ';'
[
|| <?{ $*begin_compunit }>
{ $*begin_compunit := 0; }
|| <?{ $*START-OF-COMPUNIT }>
{ $*START-OF-COMPUNIT := 0; }
<unit-block($*PKGDECL)>
|| { $/.typed_panic("X::UnitScope::TooLate", what => $*PKGDECL); }
]
Expand Down Expand Up @@ -2615,7 +2618,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
~ "placed a semicolon after routine's definition?"
);
}
unless $*begin_compunit {
unless $*START-OF-COMPUNIT {
$/.typed_panic("X::UnitScope::TooLate", what => "sub");
}
unless $*MULTINESS eq '' || $*MULTINESS eq 'only' {
Expand All @@ -2624,7 +2627,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
unless $*R.outer-scope =:= $*UNIT {
$/.typed_panic("X::UnitScope::Invalid", what => "sub", where => "in a subscope");
}
$*begin_compunit := 0;
$*START-OF-COMPUNIT := 0;
}
|| <onlystar>
|| <blockoid>
Expand Down

0 comments on commit 205fb66

Please sign in to comment.