Basic working version with states.

A lot of TODOs and error checking remain, but this is a skeleton upon
which an Engine can be built and the remainder of the specification can
be implemented.