variables in root scope

Changed code gen to declare all variables in the root scope.

also did some refactoring in `generate.ml`

closes #86