Rewrite the optimizer in CPS style.

This solves limitations on the shapes of trees it is capable
to fold. While not very important for the existing transformations,
it is critical to the upcoming implementation of inlineable
classes.