我是Perl的新手,想要使用运算符创建一个特定于域的语言,这些运算符优先使用新的Perl 6语法功能.例如,以正确的方式解析"1 + 2*6".
我到目前为止找到的文档(例如这个)没有为具有优先级声明的运算符提供语法规则的示例.
我有这个非常简单的例子
use v6;
#use Grammar::Tracer;
grammar TestGrammar {
token TOP {
<digit> <infix> <digit>
}
token infix:sym<times> is equiv(&infix:<*>) { <sym> }
}
sub MAIN() {
my $text = "1 times 2" ;
say $text ;
my $match = TestGrammar.parse($text);
say $match;
}
Run Code Online (Sandbox Code Playgroud)
这给了我
No such method 'infix' for invocant of type 'TestGrammar'
Run Code Online (Sandbox Code Playgroud)
我只想构建一个抽象语法树.
我有一个大的bash脚本,我想全局将所有stdout/stderr重定向到一个文件,除了一些进度指示器,如特定的回显消息.例如,请考虑以下脚本
#!/bin/bash
# all stdout and stderr go to out.log
exec &> out.log
special_echo "starting"
# many other commands go here
# which output both the stdout and stderr
# also some special_echo commands in here,
# for example
echo "msg1"
special_echo "middle"
echo "msg2" >&2
special_echo "finished"
Run Code Online (Sandbox Code Playgroud)
当它运行时,输出应该是
$ ./script
starting
middle
finished
$
Run Code Online (Sandbox Code Playgroud)
但是,out.log应该包含
msg1
msg2
Run Code Online (Sandbox Code Playgroud)
如何实现special_echo功能?我已经尝试使用文件描述符并回显,但无法让它显示在屏幕上.
有没有办法实现这一点,而无需将重定向添加到每一行或做这样的答案?
是否有可能在Perl 6中重载常量?这是我正在看的Perl 5示例.
特别是我想获得一个使用的字面值的字符串,例如,如果代码是
my $x = .1e-003 ;
Run Code Online (Sandbox Code Playgroud)
我需要".1e-003"而不是0.0001.