Pattern matching

In the world of computer programming, there is a concept known as pattern matching.
This technique allows us to take a complex value, such as an array or a variant, and compare it to a set of patterns.
If the value fits a certain pattern, the matching process continues and we can extract specific values from that value.
This is a powerful tool for making our code more readable and efficient,
and in this section we’ll be exploring the different ways that pattern matching can be used in daScript.

Previous paragraph was generated by ChatGPT. I’ve also asked it to be written in Richard Feynman style and now I can’t stop hearing it in my head.

But why stop there. Behold the new and exciting pattern matching documentation.

Writing documentation is hard. We have all those auto-documenting tools. Back in the 1812 I discovered doxygen. daScript has something similar implemented via comment macros.
We generate rst. From the rst we generate PDF version, as well as WEB version. But at the end of the day someone has to write all this text around samples, explain what the samples do, make it look professional.

There is also the pesky question of being (and not being) native speaker of English language. After all all your base are belong to us. Some? Any?
Whats good for the blog is not good for THE OFFICIAL DOCUMENTATION OF THE WORLDS MOST SERIOUS PROGRAMMING LANGUAGE (all caps intended).

In the ideal scenario I would like to just copy-paste my unit tests for each type of the pattern matching available, and the documentation magically appears.
Turns out that in 2023 the reality is not that far. I occasionally had to ask ChatGPT to stick to correct daScript syntax and well formed rst format.
I had to modify couple samples for the concepts to be more transparent. At times it feels like talking to a student with reasonable attention span and good sense of language.

Now, this post would not be complete without some useful information about implementation details. After all the documentation is there and praising ChatGPT is just a trend.
So lets get right to it.

daslib/match is how pattern matching is implemented. Its 100% implemented in daScript using macros. The rabbit hole is getting deeper and deeper.

I’ve added support for keywords. Its now possible to specify a keyword and wether it needs an oxford comma for the significant white space.
That way instead of

match(expr) <|
    if ...
        ...
    if match_type(type<int>,blah)
        ...

We can write

match expr
    if ...
        ...
    if match_type<int> blah
        ...

Keyword is a one-way street. Once something is keyword, it can no longer appear anywhere other than keyword context or require statement.
It may eventually leak into few other constructs (like right side of the is and as), but for now its very limited.

I’ve extended support for the canVisitArguments in the CallMacro. Its now down to individual arguments.
After all we don’t want to infer anything inside quote, nor do we want to see any changes for similar macros (qmacro_function comes to mind).

match does not start matching, until match expression is resolved. There are several patterns implemented - and I’ve tried to stick to original daScript syntax with each of them.
It was easy, because its actually original syntax with the standard AST.

[[Expression]] matches required as and is operations. They were already in place. For the rest of the structures I had to indicate it with [match_as_is] annotation.
There is no good way to check if function exists at compilation time. Think macros. Think potentially infinite inference. So for expressions it just happens.
The rest of them need an annotation. One day there will be a way to indicate that handled type has similar machinery as well.

Lastly match does not actually create variables (via &), even though I had it in in earlier implementations. Because lifetime of the expression can change, and local references are unsafe.

Now for the question of the year: “what took you so long?”. It seems like an obvious concept. It could easily be in the core language.
It is also something I wanted to be implemented with macros only with the very specific use cases in mind.
I don’t have a habit of using pattern matching in my day to day programming, because C++ does not have one (and daScript did not have one).
I am very much looking forward to developing such habit.

The entire implementation is ~500 loc, and the code is written to serve as en example a sample of more advanced macros.