Skip to content

Instantly share code, notes, and snippets.

@KingOfBrian
Created November 17, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingOfBrian/c57b4a5b6faa57cf3876ab826c555f68 to your computer and use it in GitHub Desktop.
Save KingOfBrian/c57b4a5b6faa57cf3876ab826c555f68 to your computer and use it in GitHub Desktop.
AST Protocol
protocol AST {
associatedtype NodeType = Self
var children: [NodeType] { get }
}
extension AST where NodeType: AST, NodeType.NodeType == NodeType {
func traverse(check: (Self.NodeType) -> Bool) {
for child in children {
if check(child) {
child.traverse(check: check)
}
}
}
}
class Node: AST {
var children: [Node] = []
}
let n = Node()
n.children = [Node(), Node()]
n.traverse() { node in
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment