在将一种音乐语言翻译成另一种音乐语言(ABC 到 Alda)作为学习 Raku DSL 能力的借口的过程中,我注意到似乎没有办法终止.parse! 这是我缩短的演示代码:
#!/home/hsmyers/rakudo741/bin/perl6
use v6d;
# use Grammar::Debugger;
use Grammar::Tracer;
my $test-n01 = q:to/EOS/;
a b c d e f g
A B C D E F G
EOS
grammar test {
token TOP { <score>+ }
token score {
<.ws>?
[
| <uc>
| <lc>
]+
<.ws>?
}
token uc { <[A..G]> }
token lc { <[a..g]> }
}
test.parse($test-n01).say;
Run Code Online (Sandbox Code Playgroud)
Grammer::Tracer 显示的最后一部分说明了我的问题。
| score
| | uc
| | * MATCH "G" …Run Code Online (Sandbox Code Playgroud) 使用以下代码:
use v6d;
# sub circumfix:<? ?>( @a ) {
# @a[0] >= @a[1] & @a[0] <= @a[2];
# };
sub circumfix:<? ?>( $a, $b, $c ) {
$a >= $b & $a <= $c;
};
if (? <5 0 10> ?) {
say 'Truthy';
}
else {
say 'Falsey';
}
Run Code Online (Sandbox Code Playgroud)
结果是:
(base) hsmyers@BigIron:~/board$ perl6 ./op.p6
Too few positionals passed; expected 3 arguments but got 1
in sub circumfix:<? ?> at ./op.p6 line 7
in block <unit> at ./op.p6 line …Run Code Online (Sandbox Code Playgroud) 在尝试调试程序代码时,我遇到了以下问题:
(base) hsmyers@BigIron:~$ rlwrap -A raku
To exit type 'exit' or '^D'
> my regex N { <[A..G]> };
regex N { <[A..G]> }
> my %h = A => 1, B => 2;
{A => 1, B => 2}
> 'B' ? %h.keys
True
> my $m = 'B' ~~ / <N> /;
?B?
N => ?B?
> $m ? %h.keys
False
> $m.Str ? %h.keys
True
> my $n = $m.Str
B
> $n ? %h.keys
True
> …Run Code Online (Sandbox Code Playgroud) 在使用 NCurses 模块时,我遇到了一些奇怪的行为,我在 repl 中将其归纳为:
> my $c = ' '.ord
32
> $c.WHAT
(Int)
> my int32 $n = ' '.ord
32
> $n.WHAT
Bytecode validation error at offset 128, instruction 20:
operand type 32 does not match register type 24 for op getlex_ni in frame <unit>
> my int32 $m = 32
32
> $m.WHAT
Bytecode validation error at offset 128, instruction 20:
operand type 32 does not match register type 24 for op getlex_ni in frame <unit> …Run Code Online (Sandbox Code Playgroud) 我选择在Perl 6中重新设计我的先前代码的一部分,在这种情况下,是棋盘。前两节课进展顺利(或者至少工作了,我知道的很少,我无法说出它们的正确性) ,但我坚持使用第三个。这是代码:
#!/home/hsmyers/rakudo741/bin/perl6
# board.p6 - Beginnings of a PGN toolset. And place to start learning
# Perl 6/Raku.
use v6d;
#!___________________________________________________________
constant $size = 4;
class Piece {
my Str @namesOfPieces[$size] = <
white-rook white-knight white-bishop white-queen
>;
my Str @abrevsOfPieces[$size] = <
R N B Q K B N R
>;
my Str @symbolsOfPieces[$size] = <
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
>;
my Str @codeptsOfPieces[$size] = (
"\x2656", "\x2658", "\x2657", "\x2655",
);
has Str …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个令牌,允许使用匹配的分隔符嵌套内容。如果不是“(AB)”,则 (AB) 应至少匹配“AB”。并且 (A(c)B) 将返回两个匹配项“(A(c)B)”,依此类推。
代码从其来源归结为:
#!/home/hsmyers/rakudo741/bin/perl6
use v6d;
my @tie;
class add-in {
method tie($/) { @tie.push: $/; }
}
grammar tied {
rule TOP { <line>* }
token line {
<.ws>?
[
| <tie>
| <simpleNotes>
]+
<.ws>?
}
token tie {
[
|| <.ws>? <simpleNotes>+ <tie>* <simpleNotes>* <.ws>?
|| <openParen> ~ <closeParen> <tie>
]+
}
token openParen { '(' }
token closeParen { ')' }
token simpleNotes {
[
| <[A..Ga..g,'>0..9]>
| <[|\]]>
| <blank>
]
} …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么捕获(命名和未命名)在命名正则表达式中似乎不起作用?我期待这是我做错了什么,但如果是这样,我看不到它。这是从 raku repl 中捕获的文本作为我的简短示例。
> my $s = '16.01.2020 09:18 286';
> my $dReg = /^(\d**2)\.(\d**2)\.(\d**4)/;
> $s ~~ /<$dReg>/;
?16.01.2020?
> if $s ~~ /<$dReg>/ { say $0 }
Nil
> my $dReg1 = /^$<day> = (\d**2)\.$<mon> = (\d**2)\.$<year> = (\d**4)/;
/^$<day> = (\d**2)\.$<mon> = (\d**2)\.$<year> = (\d**4)/
> $s ~~ /<$dReg1>/;
?16.01.2020?
> if $s ~~ /<$dReg1>/ { say $<day> }
Nil
> if $s ~~ /^$<day> = (\d**2)\.$<mon> = (\d**2)\.$<year> = (\d**4)/ { say $<day> }
?16? …Run Code Online (Sandbox Code Playgroud) 虽然我偶尔会在 Perl regex 中做梦,但 Common Lisp (CLisp) 的格式规范仍然让我有点困惑。我正在拍摄以下结果:
给出我想要的列表("No Match" (-2378 11 4) (-2378 11 5)):
| No Match| -2378 11 4| -2378 11 5|
Run Code Online (Sandbox Code Playgroud)
从另一端出来。这是我得到的:
[685]> (fss sd)
("No Match" (-2378 11 4) (-2378 11 5))
[686]> (format t "|~{~9<~a~>~2*~}~:*~{~*~{|~6d~3d~3d~}~}|" (fss sd))
| No Match| -2378 11 4
*** - There are not enough arguments left for this format directive.
Current point in control string:
"|~{~9<~a~>~2*~}~:*~{~*~{|~6d~3d~3d~}~}|"
|
The following restarts are available:
ABORT :R1 Abort main loop …Run Code Online (Sandbox Code Playgroud)