Generating code for another language in Haskell
Asked Answered
W

1

28

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?

Withrow answered 22/4, 2012 at 12:46 Comment(4)
You 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.Horeb
@BasileStarynkevitch: could you please point to projects which do this? I'm looking for examples and clever tricks...Withrow
You might want to have a look at the nsis package.Doorjamb
@Withrow 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.Diva
E
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).

Enuresis answered 23/4, 2012 at 11:50 Comment(1)
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.Hinds

© 2022 - 2024 — McMap. All rights reserved.