Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating code for another language in Haskell

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?

like image 664
akosch Avatar asked Apr 22 '12 12:04

akosch


1 Answers

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).

like image 70
Don Stewart Avatar answered Oct 03 '22 16:10

Don Stewart