Refactor constant handling in the generator

Use classes instead of named tuples, add name and value checking and
refactor common code into functions.