Last active
December 29, 2015 16:19
-
-
Save zah/7696809 to your computer and use it in GitHub Desktop.
Pattern Matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my alternative grammar is build around blocks as the most prominent syntax element: | |
# foo [anything] means foo do(anything): | |
# ... ... | |
# | |
# foo is foo: | |
# ... ... | |
# | |
# Then we have "match" magic/macro that looks like this: | |
match expr # The compiler automatically looks for a field used in a case | |
# statement and matches against it | |
nkSym [position typ] # Here the arguments are treated as variables that have to | |
# be unpacked from the matched value. | |
# Notice that you don't have to specify all of them | |
# it's also possible to extract "nested" values, but | |
# that's another story | |
... | |
nkIdent [ident] | |
... | |
# example of a library defined deconstructor (regular expressions) | |
match str | |
/(\w+)\s+(\d+)/ [word number] | |
... | |
# We could translate this in nimrod in something like this | |
# (with a bit more hacky transformations inside the macro): | |
match foo: | |
of nkSym(position, typ): | |
... | |
of nkIdent(ident): | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment