Skip to content

Instantly share code, notes, and snippets.

@zah
Last active December 29, 2015 16:19
Show Gist options
  • Save zah/7696809 to your computer and use it in GitHub Desktop.
Save zah/7696809 to your computer and use it in GitHub Desktop.
Pattern Matching
# 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