I want to generate code for another language in Haskell, but I'm puzzled on how to approach this problem. I'm looking for an abstract and elegant representation of the language in Haskell, which is also useful for code generation. The language in question is InstallScript (link to the language reference guide). Could someone please provide hints (maybe with examples?) on how to capture the language constructs employed in InstallScript in Haskell types, or point to similar projects for inspiration?
Asked
Active
Viewed 4,953 times
28
-
6You just need to define & implement abstract types to represent the abstract syntax tree of your object language (InstallScript) and to have the appropriate pretty printers, etc etc. – Basile Starynkevitch Apr 22 '12 at 12:54
-
@BasileStarynkevitch: could you please point to projects which do this? I'm looking for examples and clever tricks... – akosch Apr 22 '12 at 12:56
-
5You might want to have a look at the [nsis package](http://hackage.haskell.org/package/nsis-0.1.2). – bzn Apr 22 '12 at 12:59
-
@akosch You might also want to look at the language-javascript project on hackage.haskell.org which provides a parser, an AST and a pretty printer for javascript. – Erik de Castro Lopo May 03 '12 at 23:58
1 Answers
22
This is a classic problem to solve in Haskell.
- Define a abstract syntax tree for your language
- Define a pretty printer (to generate code)
For bonus points:
- Define a parser
- Write a nice Haskell combinator library to let you embed the target in Haskell
- Write an optimizer for your abstract syntax
There are many, many examples on Hackage (e.g. this one).

Daniel Wagner
- 145,880
- 9
- 220
- 380

Don Stewart
- 137,316
- 36
- 365
- 468
-
There is a incorrect word in the hash of the url of "many, many examples" (should be 'cat:Language'). Since it is just one byte I can not edit it. – Ginhing Jan 24 '19 at 11:32