Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AST Pattern matching
// "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!
@chensb
Copy link

chensb commented Oct 23, 2014

It's awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment