About Those Slangs…

I like the idea of slangs. They let you modify the grammar of Perl 6 (or maybe you’d prefer Q, or perhaps Regex?). This means you could easily switch into a more pythonesque style of programming just by importing a module, such as use Slang::Python.

However, not only are there issues with how slangs currently work (or are at least spec’d to), but they, at least I think, are unnecessary.

The current problems:

  • They augment by default : the intended use of the ‘slang’ keyword makes it so that you have augment-like behavior without actually using the augment keyword, which means you globally affect the meaning of, say, Perl6::Grammar as soon as the slang statement is interpreted.
  • No explicit way of including actions : the slang keyword is essentially another way of writing a bunch of grammar rules. What the mechanism doesn’t come with is any way to include actions, which those of you writing Perl 6 grammars are very used to by this point. To be fair, inline code blocks can do the exact same thing, but this lack of an explicit mechanism is indicative of a general forgetfulness on the importance of actions to a grammar 🙂 .

Those things are fixable, but there’s a larger issue at play: modifying any grammar after the fact is hard, for something like Perl 6 it’s daunting. There is no standard set of rule names for an implementation of the grammar, and how to implement the actions of those rules are much harder to standardize (because not everyone will use QAST blocks in their implementation). So this would a implementation-dependent endeavor, both in terms of supporting multiple implementations, and in hoping said implementations don’t break their grammar/action definitions on you.

So, because of how hard it is in general to modify a grammar, I feel that the primary purpose of slangs should be to introduce a new sublanguage, since a new grammar is easy to do 🙂 . I’ve said as much before.

However, I’ve recently realized something: the slang keyword offers no benefits over grammars and actions, at least not in its current form.

With everything at my disposal but slangs, including being able to interact with things like Perl6::Grammar directly, how would I implement a new sublang? Here’s an idea:


grammar Skylang::Grammar {
    regex TOP { ... }

class Skylang::Actions {
    method TOP($/) { ... }

augment grammar Perl6::Grammar {
    rule statement_control:sym<SKYLANG> {
        <sym> '☃' ~ '☄' $<srctext>=(<-[☄]>+)

augment class Perl6::Actions {
    method statement_control:sym<SKYLANG> {
        make Skylang::Grammar.parse(~$<srctext>, :actions(Skylang::Actions)).ast;

(Yes, this would be implementation-dependent too (e.g., on rakudo the ast from Skylang would need to have a bunch of QAST blocks); I never said that was a problem unique to slangs :P)

Plain ol’ modifying the language would involve just the augments. Additionally, depending on what macros end up doing, the above augmentations could be replaced with a single macro declaration.

My point here is that I think Perl 6 already has what you need to modifying the very grammar of the language itself. If we can work out what slangs (and those $~ variables) could be that isn’t just a synonym for grammar, then I’d be all for it. However I can’t presently think of what slang would do differently. The only thing that comes to mind for me is a way of better linking a grammar and its actions together (though that would benefit grammar too, and you can already do it by redefining the parse method (and maybe its friends) in a grammar anyway).

The more interesting question is how to modify the parsing of Perl 6 in an implementation-independent fashion. The grammar side can be helped by standardizing the rules of the grammar, essentially Perl 6’s readable version of a BNF grammar definition in other language specifications. Whether or not the grammar should be standardized at all is another matter though 🙂 .

The actions side, an independent AST specifier, might be far more tricky. But if I understand things correctly, quasi does that for us already.

So I don’t know if we can fashion slang into a far more distinct (and hence useful) keyword than its friend grammar, or if we don’t need it after all, but it’s certainly an interesting thought.

Of course, this is all complicated by the fact that “we must be fairly certain what we want, and we aren’t yet :)”. There’s so little specification of slangs that the point of this post (“slang is useless”) is in all likelihood just plain wrong. Allowing the user to rewrite the language they’re using with the language they’re using is a hard thing to do, and it’s no wonder that all efforts have been placed on everything else so far. But it has led to at least me thinking slang is unnecessary, and if I’m to be proven wrong, it needs to be done soon 😛 .

Maybe we need a “metalanguage”, like the “metamodel”… would that just be NQP?…

This entry was posted in Think Tank and tagged , , , , . Bookmark the permalink.

One Response to About Those Slangs…

  1. tleich says:

    This is what I think would make slangs possible:

    Though, that would of course mean that we would either need to expose QAST or make up something QAST-ish that can be turned into QAST easily.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s