From 205fb661b5ea3c4ea1860ac59927d0f4aadb1fc7 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Thu, 17 Aug 2023 10:48:20 +0200 Subject: [PATCH] RakuAST: some more actions/grammar streamlining --- src/Raku/Actions.nqp | 25 +++++++++++++------------ src/Raku/Grammar.nqp | 43 +++++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/Raku/Actions.nqp b/src/Raku/Actions.nqp index b2421c6d298..2e08848f6c9 100644 --- a/src/Raku/Actions.nqp +++ b/src/Raku/Actions.nqp @@ -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($/) { @@ -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 $ { @@ -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); } @@ -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; @@ -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 $ { diff --git a/src/Raku/Grammar.nqp b/src/Raku/Grammar.nqp index 069e04b98af..6e6a51ef3cc 100644 --- a/src/Raku/Grammar.nqp +++ b/src/Raku/Grammar.nqp @@ -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; @@ -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(); } @@ -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'); @@ -2379,11 +2382,11 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common { * <.enter-package-scope($)> [ - || { $*begin_compunit := 0; } + || { $*START-OF-COMPUNIT := 0; } || ';' [ - || - { $*begin_compunit := 0; } + || + { $*START-OF-COMPUNIT := 0; } || { $/.typed_panic("X::UnitScope::TooLate", what => $*PKGDECL); } ] @@ -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' { @@ -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; } || ||