Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
scroll top