加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl6 – Perl 6的其他问题是否是一个特殊的声明分离案例?

发布时间:2020-12-15 21:50:01 所属栏目:大数据 来源:网络整理
导读:从 syntax doc: A closing curly brace followed by a newline character implies a statement separator,which is why you don’t need to write a semicolon after an if statement block. if True { say "Hello";}say "world"; 这很好,Why is this Perl
从 syntax doc:

A closing curly brace followed by a newline character implies a statement separator,which is why you don’t need to write a semicolon after an if statement block.

if True {
    say "Hello";
}
say "world";

这很好,Why is this Perl 6 feed operator a “bogus statement”?发生了什么.

但是,这条规则对于一个不受约束的其他人如何运作?这是一个特例吗?

if True {
    say "Hello";
}
else {
    say "Something else";
}
say "world";

或者,with-orwith example怎么样:

my $s = "abc";
with   $s.index("a") { say "Found a at $_" }
orwith $s.index("b") { say "Found b at $_" }
orwith $s.index("c") { say "Found c at $_" }
else                 { say "Didn't find a,b or c" }

解决方法

您找到的文档并不完全正确. documentation has been updated and is now correct.它现在写着:

Complete statements ending in bare blocks can omit the trailing semicolon,if no additional statements on the same line follow the block’s closing curly brace }.

For a series of blocks that are part of the same if/elsif/else (or similar) construct,the implied separator rule only applies at the end of the last block of that series.

原始答案:

查看if在nqp和Rakudo中的if语法,似乎if / elsif / else组块一起被解析为一个控制语句.

在nqp中if的规则

rule statement_control:sym<if> {
    <sym>s
    <xblock>
    [ 'elsif's <xblock> ]*
    [ 'else's <else=.pblock> ]?
}

(https://github.com/perl6/nqp/blob/master/src/NQP/Grammar.nqp#L243,截至2017年8月5日)

在Rakudo的if规则

rule statement_control:sym<if> {
    $<sym>=[if|with]<.kok> {}
    <xblock(so ~$<sym>[0] ~~ /with/)>
    [
        [
        | 'else'h*'if' <.typed_panic: 'X::Syntax::Malformed::Elsif'>
        | 'elif' { $/.typed_panic('X::Syntax::Malformed::Elsif',what => "elif") }
        | $<sym>='elsif' <xblock>
        | $<sym>='orwith' <xblock(1)>
        ]
    ]*
    {}
    [ 'else' <else=.pblock(so ~$<sym>[-1] ~~ /with/)> ]?
}

(截至2017年8月5日https://github.com/rakudo/rakudo/blob/nom/src/Perl6/Grammar.nqp#L1450)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读