简体   繁体   English

Perl 6 - 接受“Bareword”输入的子程序

[英]Perl 6 - Subroutine taking "Bareword" input

So I've been diving into Perl 6 and have been looking at interpreting another language using Perl 6's operator definitions.所以我一直在深入研究 Perl 6,并一直在研究使用 Perl 6 的运算符定义来解释另一种语言。 I understand that this could be done by parsing the code but I'm looking to push Perl 6's capabilities to see what it can do.我知道这可以通过解析代码来完成,但我希望推动 Perl 6 的功能,看看它可以做什么。 Having this functionality would also make the parsing a lot easier拥有此功能也将使解析更容易

I'm trying to make a variable definition in a C-style format.(The language isn't important)我正在尝试以 C 样式格式进行变量定义。(语言并不重要)

Something like:就像是:

char foo;

Where the char represents the type and the foo is the variable name.其中char代表类型,而 foo 是变量名。 From my understanding the char can be interpreted using an operator definition like so:根据我的理解,可以使用像这样的运算符定义来解释字符

sub prefix:<char>($input) {
    say $input;
}

Which calls a subroutine with the foo as $input .它使用foo作为$input调用子例程。 The idea from here would be to use foo as a string and hold it's reference in a hash somewhere.这里的想法是使用foo作为字符串并将它的引用保存在某个地方的哈希中。 The problem with this is that Perl 6 seems to see any bareword as a function call and will complain when it can't find the "Undeclared routine".问题在于 Perl 6 似乎将任何裸字视为函数调用,并且在找不到“未声明的例程”时会抱怨。

I've looked possibly everywhere for an answer to this and the only thing that makes me still think that this may be possible is the qw function from Perl 5 which is now < > in Perl 6. The < > is obviously an operator which leads me to believe that there is a subroutine defined somewhere that tells this operator how to work and how to deal with the bareword input.我看可能是到处找一个答案,这让我仍然认为这是可能的唯一的事情是在Perl 5 QW功能现在是<> Perl 6中的<>显然是一个运营商,这导致我相信在某处定义了一个子程序,它告诉这个操作符如何工作以及如何处理裸字输入。

So to my question:所以我的问题:

Is there a way of accepting bareword input into a subroutine just like the < > operator does?有没有一种方法可以像< >运算符那样将裸字输入接受到子程序中?

Cheers!干杯!

The best way to do that would be to create a Grammar that parses your language.最好的方法是创建一个语法来解析你的语言。 If you additionally want it to run the DSL you have just created, combine it with Actions .如果您还希望它运行您刚刚创建的 DSL,请将其与Actions结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM