First off, I know this is a question asked by MANY other programmers before me. But I couldn't find any usable resource that could help me.

Well, I'm creating an programming language called "Light" the syntax is comparable to python but it has a strict object-orientated concept.

I have made an interpreter ( in C++ ) for this language yet, my problem is how to turn this into an executable. ( Or simply: how do I make a compiler ? )

Thanks for your attention

P.S.: I've found some links to a very old tutorial but it is in Pascal...

EDIT: Well, well. Now I have found a suitable tutorial for C++. About the project: There have been some changes. Now the language is called "Q." ( kju Dot ).

  • first learn the target language, then see how you would translate the basic code blocks from your language into the the other language – ratchet freak Feb 23 '13 at 20:02
  • 1
    Are you "merely" trying to write a compiler (with the background knowledge from previously implementing an interpreter for the same language), or are you literally trying to turn your interpreter into a compiler? Your phrasing is giving me ideas ;-) – user7043 Feb 23 '13 at 20:02
  • 4
    A Futamura projection? – Dan D. Feb 24 '13 at 5:02
  • 1
    That's trivial - just specialise your interpreter against a particular source code to get its compiled version. – SK-logic Feb 24 '13 at 12:31
  • The question is about the technique called like "tracing compiler" – Dmitry Ponyatov Dec 27 '19 at 2:42

The first couple of steps lexing/parsing (and analysis, depending on how that's structured) can be the same. But you will need to convert your representation to a code-generating representation. Typically LLVM is used for hobbyists as it is pretty much the only remotely decent free code-generating library around.

  • This is exactly what I was going to write, but you got to it first. Have a +1. – Mason Wheeler Feb 23 '13 at 20:20
  • 3
    Wrong. Simply emiting C (or C++, or whatever else) is much more popular for the hobbyist compiler projects. LLVM is much more complicated. And JVM or .NET are not any worse than LLVM for this purpose. – SK-logic Feb 24 '13 at 12:33
  • Those compilers do not produce executables, which the OP does want. As for .NET/JVM, it really depends on the semantics of his language. – DeadMG Feb 24 '13 at 14:29
  • 1
    @DeadMG, ghc, for example, is perfectly capable of producing executables via C. As well as hundreds of the other C-based compilers. Does your religion forbid you to use popen(...) in a compiler? Then you have to throw away gcc itself. As for .NET, unlike JVM it's capable of catering for quite a wide variety of semantics, due to its unsafe features. – SK-logic Feb 25 '13 at 8:55
  • I used LLVM (with python bindings) and it worked like a charm. :) – LaVolpe Apr 25 '15 at 17:05

If all you really want is an executable, you can append the script to the end of an interpreter executable and have it run that.

See: https://stackoverflow.com/questions/5795446/appending-data-to-an-exe

Alternately, you can just have the interpreter load the code from a file in the same directory as the interpreter and get largely the same result, just with multiple files.

A true compiler would convert the code into some other language and use the compiler for that language. But if you just want to distribute executables, there's no need to do that.

  • Well, I know this way of packing :) used it for Python ( before I heard of py2exe ). For me this is cheating and the other problem is performance. My language how it is now is low level BUT strict objectorientated. – LaVolpe Feb 23 '13 at 22:17
  • it does not make compiler definitely, only interpreter embedding – Dmitry Ponyatov Dec 27 '19 at 2:45
  • 1
    @DmitryPonyatov, you felt the need to comment nearly seven years later saying what I'd already said in my answer? – Winston Ewert Dec 27 '19 at 6:46

You may also want to consider using System.Reflection.Emit from the .NET runtime to generate a .NET executable. This will give you access to an existing object-oriented environment which has already solved some problems for you, and the resulting exe can be platform-independent. The runtime will compile the intermediate code in the .exe to machine code as needed, so performance should be comparable to what you'd get from compiling directly to native code.

  • Probably a nice idea, but I designed my language to be low-level at some point ;) Now I am trying to dig through Intels Documentation of OpCodes. – LaVolpe Nov 11 '14 at 18:13

You are asking about the technique known as "tracing compiler": the interpreter gathers information about code execution, and incrementally builds low-level code using collected data.



The difference from the classical JIT is the fact that collected tracing data and compiled code is preserved from every run of a program, which gives you not only executable machine code but also optimization on a real (or training) data. The first pass is a set of tests, which must pass all program branches, to make code for every part of your program. The next ones are program runs on real data (production use), which collects and applies optimizations (things like branch ordering to most frequently used cases).

This method much more complex to implement than a classical compiler construction scheme, so it is not used frequently, and it is complex to find a good set of tutorials on how to do such alike compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.