Metaprogramming: Difference between revisions

m
→‎{{header|Raku}}: Fix comments: Perl 6 --> Raku
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|Raku}}: Fix comments: Perl 6 --> Raku)
Line 1,111:
(formerly Perl 6)
 
Perl 6Raku makes it very easy to do metaprogramming. It is a basic goal of the language.
 
It is trivial to add a new operator. Most Perl 6Raku operators are written as normal multiple-dispatch functions in the setting (known as a "prelude" in other languages, but in Perl 6Raku the setting is a lexical scope notionally surrounding your compilation unit).
 
There is no a built in factorial operator Perl 6Raku. It was purposely left out to use as a demonstration of how easy it is to add it. <tt>:-)</tt>
 
<lang perl6>sub postfix:<!> { [*] 1..$^n }
Line 1,137:
a2 a3rd a17th a32nd a95 a144th a201st
a144th a17th a2 a201st a32nd a3rd a95</pre>
Grammar mixins work in Perl 6Raku because grammar rules are just methods in grammar classes, and Perl 6Raku automatically writes a JIT lexer for you whenever you derive a new language. This functionality already works internally in the standard parser—what is not yet implemented is the <tt>augment slang</tt> hook to allow user code to do this mixin. Perl 6Raku itself is already parsed using such grammar mixins to provide extensible quoting and regex syntax. For example, every time you pick your own quote characters, you're actually deriving a new Perl 6Raku dialect that supports those start and stop characters. Likewise any switches to impose single or double-quote semantics, or heredoc semantics, is merely a grammar mixin on the basic <tt>Q</tt> language.
<lang perl6>say "Foo = $foo\n"; # normal double quotes
say Q:qq 【Foo = $foo\n】; # a more explicit derivation, new quotes</lang>
2,392

edits