I am searching for programming languages with a certain paradigm, or the name of the paradigm which works like follows:

  1. You start with writing the source code of a program or something more like a script.
  2. This is then executed by the "compiler" (interpreted or just in time compiled) and produces a actual executable.
  3. You run the produced machine code.

So the compiler does not only translate source code to machine code but also provides a runtime environment while doing so. The code is executed by the compiler at one, with no user interaction, just the source code as input (no Read–eval–print loop, console or such things).

For example, C++ (11/14/17) then to go in this direction with things like recursive templates and constexpr, where entire functions can be executed at compile time (not runtime). But I imagine it even more generic and allowing for reflection (like Smalltalk, where everything is an object). It could help to reduce redundancy of the source code and allow more abstraction.

Code example:

const char* names[] = { "a", "b", "c", ... };
template<int size>
struct S {
    assert(size < sizeof(names)/sizeof(void*));
    for(int i = 0; i < size; ++i)
        this.add_field(Type::float, names[i]);

This can not be done in C/C++, D has a bit more features like static ifs ans so on, but no language I know actually supports this.

Is there a name for this paradigm and are there languages which are heavily based on it?

  • 1
    sounds like REPL – gnat Jul 5 '15 at 14:14
  • Just link statically your executable. Your question is OS specific. Look into SBCL – Basile Starynkevitch Jul 5 '15 at 14:14
  • It might be, that I did not describe it well enough, but I am pretty sure the question is nothing implementation/platform specific and has also nothing to do with linking – Alexander Meißner Jul 5 '15 at 14:18
  • 1
    Is "code generation" the term you're looking for? – Ixrec Jul 5 '15 at 14:23
  • It is certainly related, but every compiler has a "code generator", so that is nothing specific about the paradigm. – Alexander Meißner Jul 5 '15 at 14:24

From what you describe, it sounds like you are looking for a potent preprocessor to generate the code that is subsequently compiled. Preprocessors are, of course, almost as old as computer languages themselves, however, most are really limited in extend and syntactic capabilities. Afaik, there are basically two kinds of preprocessors:

  • Non-turing complete preprocessors that provide little more than header inclusion and/or conditional compilation. These are not what you need.

  • Turing complete preprocessors. You can do any computation in these, however, their syntax generally sucks just as much as the syntax of the non-turing complete ones.

    I know of two such preprocessors:

    • m4

    • C++ templates. Though not exactly a preprocessor (they work somewhere in the middle of the parse rather than doing string transformations on the input text), they are quite often abused to do complex calculations at compile time.

    m4 has the advantage of allowing you to inspect the resulting code. That makes m4 generated programs easier to debug than template generated programs. But, as I said, the syntax sucks in both cases, so you can't write complex preprocessor programs and expect your code to remain readable.

For complex preprocessing tasks, I would look at a real interpreted language like python or ruby to generate the source code. Doing code generation in such a language feels a bit clumsy because you have to explicitly write the resulting code to a file, but readability is significantly higher than either m4 or C++ templates.

  • That is exactly what I am talking about, but as a matter of fact the solution seems to be using a script language (which is a bit ugly because of the lack of pointers, bit operations and so on). I was hoping that a language existed, which was made especially for this purpose. – Alexander Meißner Jul 5 '15 at 14:57
  • Afaik, there is no such language. Of course, that may only be due to my ignorance, there is tons of languages that I've never even heard of. So, if you find out that I'm wrong please tell me about the language you found. I'd be just as interested as you are. – cmaster - reinstate monica Jul 5 '15 at 15:08

NIM http://nim-lang.org/docs/tut1.html

Provides arbitrary compile-time calculations with whole feature set of runtime.

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