Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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)

Something like:

char foo;

Where the char represents the type and the foo is the variable name. 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. The idea from here would be to use foo as a string and hold it's reference in a hash somewhere. 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".

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.

So to my question:

Is there a way of accepting bareword input into a subroutine just like the < > operator does?

Cheers!

like image 374
Phyreprooph Avatar asked Nov 17 '15 04:11

Phyreprooph


1 Answers

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.

like image 168
jjmerelo Avatar answered Oct 20 '22 15:10

jjmerelo