Compilation Perl 6中后缀或后循环前的点是什么意思?
在Perl文档中,有一节是关于 在大多数情况下,点可以放在后缀或后循环之前: 从技术上讲,不是真正的运营商;它的语法在编译器中是特殊的 我试过自己:Compilation Perl 6中后缀或后循环前的点是什么意思?,compilation,grammar,raku,postfix-operator,Compilation,Grammar,Raku,Postfix Operator,在Perl文档中,有一节是关于 在大多数情况下,点可以放在后缀或后循环之前: 从技术上讲,不是真正的运营商;它的语法在编译器中是特殊的 我试过自己: > my @a = 1,2,3,4,5 > @a[1] # 2 > @a.[1] # 2 > my %a = Perl => 6, Python => 3, PHP => 7 > %a<Perl> #6 > %a.<Perl> #6 > my @weekda
> my @a = 1,2,3,4,5
> @a[1] # 2
> @a.[1] # 2
> my %a = Perl => 6, Python => 3, PHP => 7
> %a<Perl> #6
> %a.<Perl> #6
> my @weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
> @weekdays.antipairs.hash{'Sunday'} # 6, I expected it to be syntax wrong, but it did work!
> @weekdays.antipairs.hash.{'Sunday'} # 6, seems visual clarity or brevity
>my@a=1,2,3,4,5
>@a[1]#2
>@a[1]#2
>我的%a=Perl=>6,Python=>3,PHP=>7
>%a#6
>%a#6.
>my@weekdays=[“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日];
>@weekdays.antipairs.hash{'Sunday'}#6,我原以为它的语法是错误的,但它确实有效!
>@weekdays.antipairs.hash.{'Sunday'}#6,看起来视觉清晰或简洁
那么,在Perl 6中,后缀或后循环前的点是什么意思?它究竟是怎么做到的?我对此很好奇。谢谢。Perl 6中的表达式被解析为
termish
东西,它们之间有infixish
东西。termish
依次定义为零个或多个prefixish
事物,后跟术语本身,后跟零个或多个postfixish
事物。postfixish
包含以下所有内容:
- 方法调用(如
).foo
- 后缀运算符(如
)++
- 后循环修复运算符(如
中的@a[42]
)[42]
- 在这些上执行hyper(
)以跨数据结构分发它们>
@weekdays.antipairs.hash{'Sunday'}
解析很好(为什么@weekdays.antipairs.hash
也可以工作,甚至@weekdays.antipairs.hash.say
也可以)
至于
,语法只接受并忽略后缀
或后循环
之前的
。这里是一个略为精简的解析器版本,我对其进行了一些注释,以解释这些片段是什么
token postfixish {
<!stdstopper>
# If we're not in a string interpolation, allow unspace (that's
# where you write `\ ++`, for example, allowing spreading
# postfixish things over multiple lines).
[ <!{ $*QSIGIL }> [ <.unsp> | '\\' ] ]?
# Here we match the >> for doing a hyper. Note that it accepts
# but disregards a '.' before it. It's not captured at all and
# doesn't affect the code that is compiled.
[ ['.' <.unsp>?]? <postfix_prefix_meta_operator> <.unsp>?]**0..1
[
| <OPER=postfix>
# When there'd be no confusion with a method call, a '.' is
# also accepted and disregarded before a postfix operator
| '.' <?before \W> <OPER=postfix> ## dotted form of postfix operator (non-wordy only)
| <OPER=postcircumfix>
# Ditto here for recognized postcircumfixes
| '.' <?[ [ { < ]> <OPER=postcircumfix>
| <OPER=dotty>
| <OPER=privop>
]
}
将产生:
[2 3 4 5 6 7 8 9 10 11]
下面是一个后电路修复示例:
my %h = a => 1, b => 2, c => 3;
say .<a> + .<c> given %h;
my%h=a=>1,b=>2,c=>3;
说。给定%h;
它产生4
Perl 6语法的设计通常是为了方便将代码从一种形式转换到另一种形式,在一些地方接受
,即使在不严格需要它的地方,也会稍微简化(因此可以重构为例如%h1.+%h2;
,解析器也可以使用它)。考虑到<>代码> %h 短>代码> %H.<代码>,这将有助于学习目的。给%h一点惊喜
所提供的额外空间也可能有助于清晰,特别是如果要堆叠多个后缀,甚至可能-如果语言扩展出于某种原因决定定义一些“有趣的”术语或后缀运算符-用作消除歧义的手段。当您有一个返回可调用的的sub时,它实际上很重要
sub foo { 'hello' }
sub bar { &foo; }
bar.WHAT # (Sub)
bar().WHAT # (Sub)
bar.().WHAT # (Str)
bar()().WHAT # (Str)
bar().().WHAT # (Str)
bar.().().WHAT # No such method 'CALL-ME' for invocant of type 'Str'
你的解释很清楚。所以它是故意设计的。酷,谢谢
my %h = a => 1, b => 2, c => 3;
say .<a> + .<c> given %h;
sub foo { 'hello' }
sub bar { &foo; }
bar.WHAT # (Sub)
bar().WHAT # (Sub)
bar.().WHAT # (Str)
bar()().WHAT # (Str)
bar().().WHAT # (Str)
bar.().().WHAT # No such method 'CALL-ME' for invocant of type 'Str'