Optimizes an AST to identify special gate cases.
Converts an AST to JSON.