katsaii / catspeak-lang

A cross-platform modding language for GameMaker games.
https://www.katsaii.com/catspeak-lang/
MIT License
94 stars 6 forks source link

Dev 3.0.0 - Implement `match` expression #97

Closed nonk123 closed 1 year ago

nonk123 commented 1 year ago

The syntax is as follows:

match some_value {
    value1 => result1,
    value2 => result2,
    // ...
    else => otherwise,
}

This will evaluate expression some_value and compare the result against value1, value2, ... sequentially. If anything "matches" (is equal to some_value), the corresponding result is returned. Otherwise, whatever is in the optional else block is returned.

The results and values can be any expression including blocks. values are evaluated lazily; if a match is found, no values evaluate further.

match arms must currently end in a comma including the final arm. Rationale: it's the rustfmt default behavior, and this specific match syntax is inspired by Rust. Enforcing this rule also helps with parsing a little since no extra peeks are required. This can be changed if needed.

match returns undefined if the arms block is empty or if no conditions were met without an else block present.

The => arrow token is now a thing too. It's only used for match as of now.

The current match-{1..5} unit tests don't cover everything, but that is totally fixable. Need a thorough review of what is done so far before adding more code.