r/ProgrammingLanguages • • Apr 12 '21

What are some cool/wierd features of a programming language you know?

I'm asking this question out of curiosity and will to widen my horizons.

146 Upvotes

204 comments sorted by

View all comments

Show parent comments

1

u/b2gills Apr 21 '21

In Raku, regexes are code.

It's just code with a different base syntax and semantics.

As such they can have parameters and variables.

'AABBCC' ~~ /
    ^ # beginning of string

    $<a> = A+

    {}
    :my $count = $<a>.chars; # lexical variable

    $<b> = B ** {$count}
    $<c> = C ** {$count}

    $ # end of string
/;

In the case of a regex like rob|robert it acts a bit like this pseudo code:

given $input {
    when 'robert' {…}
    when 'rob'    {…}
}

Note that it gets sorted by length of the part of the match we know about. (rob.*|robert would get sorted the same as above even though rob.* may end up longer.)

It would even get sorted by specificity. so robert would be before rob... but after robert.

It of course can cheat:

given $input {
    when 'rob' {
        when 'ert' {…} # robert
        when ''    {…} # rob
    }
}

This is important since Raku's sub languages can be embedded inside of the others.

'robert' ~~ /
    | rob    { say 'matched just "rob"' }
    | robert { say 'matched a full "robert"' }
/
# matched a full "robert"

If we rewrite into the pseudo language I have above:

given $input {
    when 'rob' {
        when 'ert' { say 'matched a full "robert"' }
        when ''    { say 'matched just "rob"' }
    }
}

If you were to re-write rob|robert as a Raku grammar, you could have this:

grammar Foo {
    token TOP { <name> }

    # acts like /rob|robert/
    proto token name {*}
    token name:sym<rob>    { <sym> }
    token name:sym<robert> { <sym> }
}

grammar Bar is Foo {
    # acts like /alice|roberta|rob|robert/
    token name:sym<alice> { <sym> }
    token name:sym<roberta> { <sym> }
}

Note that it can still do the optimization upon the call to the <name> method/regex.

given $input {
    when 'alice'     { name:sym<alice> }
    when 'rob' {
        when 'ert'  {
            when 'a' { name:sym<roberta> }
            when ''  { name:sym<robert> }
        }
        when ''      { name:sym<rob> }
    }
}

You have rob|robert transforming into another similar (but potentially faster) regex rob(ert)?, but in doing so you lose a piece of information. Effectively what Raku does is add branching paths instead. So then it can keep track of more information.

I think that you are thinking about regexes as their own thing, separated from everything else. You also seem to think about it as something that is both flat and linear. If so, those thought processes are limiting how you think about them.

I got really good at regexes in Perl because I saw regexes as the programming language that they are. Regexes in Raku are nicer for the very reason that it expands upon this fact.

1

u/PL_Design Apr 21 '21 edited Apr 21 '21

I've written parsers, parser generators, path finding algorithms, and inference engines before, and they're all variations of Search. I'm very familiar with how this stuff works. The hard part about any of this is adapting theory to reality, which is why I'm ruminating so much on how to make my design work how I feel it should. My interest here specifically is about controlling DFAs very closely, which is a different application than what you're talking about. I do appreciate the breakdown of how Raku sees this, though, which has kickstarted some ideas in my head.