On Wed, Aug 23, 2000 at 12:26:38PM +1000, Timothy Docker wrote:
>
> I'm writing a haskell program that generates C++ code based upon
> some haskell data structures. At the moment, the code is somewhat
> ugly with lots of stuff like
>
> mutatorDef structName (name,vtype) =
> "inline void\n" ++
> structName ++ "::" ++ (mutatorName name) ++
> "( " ++ (cppParamType vtype) ++ " v ) {\n" ++
> " " ++ (storageName name) ++ " = v;\n" ++
> "}\n\n"
>
> All those ++ operators working on raw strings bug me, and manually
> getting the indentation correct is a pain. Is there a more
> functional approach to generating source code? I thought this could
> be a common enough task that there could be a library, but a perusal
> of haskell.org didn't seem to show anything relevant.
To make matters worse, you're likely getting lousy efficiency with all
of the ++ operators. Look through the code for showS in the Prelude
for better ways to hook strings together. Paul Hudak talks about the
efficient use of the show functions at:
http://www.haskell.org/tutorial/stdclasses.html
Now, with regard to the code being ugly, my suggestion would be to
check out some of the pretty printer libraries out there, and to look
through them. They basically solve a similar problem, except they
first parse a normal program into a tree, then flatten the tree in a
standard way. In your case you'll likely build the tree directly,
then call the final stages of the pretty printer. There is no
shortage of pretty printer libraries for Haskell, or for FP in
general.
Hope this helps, at least a little :)
--
-- Jeffrey Straszheim | A sufficiently advanced
-- Systems Engineer, Programmer | regular expression is
-- http://www.shadow.net/~stimuli | indistinguishable from
-- stimuli AT shadow DOT net | magic