8

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

Giorgio
19.8k16 gold badges89 silver badges137 bronze badges
asked Feb 23, 2013 at 19:58
5
  • first learn the target language, then see how you would translate the basic code blocks from your language into the the other language Commented Feb 23, 2013 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 ;-) Commented Feb 23, 2013 at 20:02
  • 4
    A Futamura projection? Commented Feb 24, 2013 at 5:02
  • 1
    That's trivial - just specialise your interpreter against a particular source code to get its compiled version. Commented Feb 24, 2013 at 12:31
  • The question is about the technique called like "tracing compiler" Commented Dec 27, 2019 at 2:42

4 Answers 4

12

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.

answered Feb 23, 2013 at 20:02
6
  • This is exactly what I was going to write, but you got to it first. Have a +1. Commented Feb 23, 2013 at 20:20
  • 5
    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. Commented Feb 24, 2013 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. Commented Feb 24, 2013 at 14:29
  • 3
    @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. Commented Feb 25, 2013 at 8:55
  • I used LLVM (with python bindings) and it worked like a charm. :) Commented Apr 25, 2015 at 17:05
4

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.

answered Feb 23, 2013 at 21:29
3
  • 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. Commented Feb 23, 2013 at 22:17
  • it does not make compiler definitely, only interpreter embedding Commented Dec 27, 2019 at 2:45
  • 1
    @DmitryPonyatov, you felt the need to comment nearly seven years later saying what I'd already said in my answer? Commented Dec 27, 2019 at 6:46
3

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.

answered Feb 24, 2013 at 4:52
1
  • 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. Commented Nov 11, 2014 at 18:13
1

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.

https://en.wikipedia.org/wiki/Tracing_just-in-time_compilation

https://stefan-marr.de/papers/oopsla-marr-ducasse-meta-tracing-vs-partial-evaluation/

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.

answered Dec 27, 2019 at 2:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.