Created
October 17, 2014 22:19
-
-
Save melix/c02227081dd60f5cedaa to your computer and use it in GitHub Desktop.
AST Pattern matching
This file contains hidden or 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
// "macro" builds the AST of the corresponding block | |
def ast1 = macro { foo(a) + foo(a) } | |
def ast2 = macro { foo(a) + foo(foo(a)) } | |
// we build another AST which will be used as a pattern | |
def pattern = macro { | |
x + foo(x) | |
}.withConstraints { | |
// declare that x is not a variable, but a placeholder | |
placeholder x | |
} | |
assert !ast1.matches(pattern) // then the first AST doesn't match the pattern | |
assert ast2.matches(pattern) // but the second one does! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's awesome!