0

I want to write a small DSL that will need to compile to another high level programming language (similar to C, but a lot more limited).

What is the high level overview of what the compiler should do?

To clarify, I'm especially interested into a conversion from an high level programming language to another relatively high level programming language, not in compilation in general.

m fran
  • 131
  • 3
  • Do you really want/need to *compile* the DSL into another language? or do you want to *interpret* the DSL? –  Jun 15 '15 at 13:24
  • 1
    What kind of DSL are you thinking about? On what platform? What is the target language exactly? Please **edit your question to improve it** – Basile Starynkevitch Jun 15 '15 at 13:38
  • 1
    Have you considered embedding your DSL in LISP, instead of hacking up Yet Another Ill-Considered C Lookalike? – John R. Strohm Jun 15 '15 at 14:59

2 Answers2

4

To clarify, I'm especially interested into a conversion from an high level programming language to another relatively high level programming language, not in compilation in general.

Well, I have some bad news for you - translating from one language to another is compilation in general. Thinking that you can skip steps because you're working with high level languages, or because your language is very limited is only going to bring you trouble.

Compilers are a well researched, well known, and well documented sort of software. The standard approach to building them is not as complicated as it may seem, and the general approach will save a pile of headaches and complexity down the road by dealing with the boatloads of corner cases that comprise even the simplest of languages.

Telastyn
  • 108,850
  • 29
  • 239
  • 365
0

If you are translating some DSL to some generated C code (or something similar), look first at this and that answers.

I actually am doing the same within MELT, a Lispy domain specific language to extend the GCC compiler

You did not explain what kind of DSL are you thinking of, and how would the generated C code be used. Do you generate a standalone program, or some "plugin" or "library"?

A significant issue is memory management. You definitely should read about garbage collection (e.g. read the GC handbook).

Of course, translating some high-level domain specific language to C (or some other C-like language, or even any programming language which is lower-level than your DSL) is some form of compilation.

If you translated from a lower-level DSL to a higher-level language, you could read about decompilation and perhaps static binary analysis.

BTW, even for a "small DSL" and a simple C-like (or Pascal-like) target language, it is a lot of work (probably a year at least).

What generating some C-like language (with a good compiler implementation) brings you is that you might work less on optimization techniques inside your own compiler, with the realistic hope that the C (or Pascal, etc...) compiler you are generating code for would be able to optimize your generated code. You could also consider generating Gimple (with the new libgccjit from recent GCC) or LLVM or use some other JIT library (e.g. libjit).

Basile Starynkevitch
  • 32,434
  • 6
  • 84
  • 125