Skip to content

Commit 199e8b6

Browse files
committed
Prepare for some extra :decl(...) possibilities.
These will cover things that today require code generation or are not available at NQP level, but probably should be. It will allow Perl 6's custom lexpad type to be eliminated in favor of the shared NQP one, as well as simplifying code generation for certain constructs and giving us a freer hand in how we compile time. Mostly, this just documents the new things.
1 parent a5942d5 commit 199e8b6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

docs/qast.markdown

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,35 @@ For example, the following block takes two positional parameters.
318318
...
319319
)
320320

321+
Parameter declarations can also be given:
322+
323+
* :default(...) - a QAST tree that produces a default value for the parameter
324+
if it is not passed. This makes it optional.
325+
* :slurpy(1) - specifies that the parameter is slurpy. Use :named(1) also for
326+
a named slurpy parameter.
327+
328+
Finally, there are a couple of other values of decl that work with lexicals.
329+
330+
* static - means that the lexical should be given the value specified in the
331+
:value(...) argument. Useful for things that wish to install symbols in the
332+
lexical scope with the intention they'll not be mutated (for example, a type
333+
declaration may be installed using this). No attempt is made to ensure you
334+
do not re-bind such a symbol, but do not do this; runtimes are free to turn
335+
lookups of static lexical symbols into direct references to the symbol.
336+
* contvar - means that the lexical should be initialized to a clone of the
337+
:value(...) argument. Presumably, this represents some kind of container
338+
type. There are no restrictions on re-binding.
339+
* statevar - same as for contvar, except the container created will be used
340+
for all given closure clones. To be clear, cloning a code ref doesn't bring
341+
state variables along. On the initial call, containers are formed in the way
342+
that contvar forms them: by cloning the :value(...) argument.
343+
321344
## QAST::VarWithFallback
322345

346+
In the context of a bind, or with native types, this is exactly the same as a
347+
QAST::Var. For fetches of object types, if a null is produced, the QAST tree
348+
in :fallback(...) will be run and the value that it evaluates to produced
349+
instead.
323350

324351
## QAST::BVal
325352
A QAST::Block can only appear once in the QAST tree. So what if you want to

src/QAST/Var.nqp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class QAST::Var is QAST::Node {
33
has str $!scope;
44
has str $!decl;
55
has int $!slurpy;
6-
has $!default;
6+
has $!default_or_value;
77

88
method name(*@value) {
99
$!name := @value[0] if @value;
@@ -18,7 +18,8 @@ class QAST::Var is QAST::Node {
1818
!nqp::isnull_s($!decl) ?? $!decl !! ""
1919
}
2020
method slurpy(*@value) { $!slurpy := @value[0] if @value; $!slurpy }
21-
method default(*@value) { $!default := @value[0] if @value; $!default }
21+
method default(*@value) { $!default_or_value := @value[0] if @value; $!default_or_value }
22+
method value(*@value) { $!default_or_value := @value[0] if @value; $!default_or_value }
2223

2324
method substitute_inline_placeholders(@fillers) {
2425
self

0 commit comments

Comments
 (0)