Question

What do you call a programming language that can execute its own code (passed as a string literal)? The setting in my mind is something similar to this (forgetting for a moment typing of code, results, etc):

string eight = "5+3"
int result = run(eight)

Where run is keyword. Is that a reflective programming language?

Was it helpful?

Solution

I think you're talking about Meta-circular evaluators. Your run command is the eval in the eval-apply cycle, described in Structure and Implementation of Computer Programs.

OTHER TIPS

The best you can come up with that is exactly what you ask for is "has an eval function" (as mentioned by Javier in his comment to your question).

Self-hosting is not necessary nor does it necessarily do what you want. Two examples are: C is a self hosted language that cannot execute its own code in a string but Tcl is not a self hosted language that can execute its own code given in a string.

  • As a side note, half the time C runs in environments where there is no API to execute external processes. So while in some environments you can configure your system to make C able to execute its own complier and then execute its own code in general it is not really what you are asking for.

  • Side-side note. If we can cheat and use the "execute external compiler" route to do this then a language does not need to be self hosting to do what you want. It just needs an exec function to call its own compiler (which is often written in C).

Same with Reflection/Introspection, having the ability to do introspect yourself does not mean you can execute code in a string. Two examples of languages that has Reflection but does not have a built-in eval are C# and Java.

  • As a side note, once you have an eval function you can usually do Reflection/Introspection albeit in a clumsy way.

Self-interpretive has the same problem with its definition as self-hosting. Tcl is a language where its eval function is not written in itself - it merely exposes the Tcl code evaluator implemented in C.

At first glance, Meta-circular evaluators looks like what you want. It is defined as self-interpretive where the eval function is built in rather than implemented in the language itself. But looking further you will notice that it is defined by the mechanism used to achieve the feature rather that the feature itself. Tcl is again an exception where it does not meet all the definitions of what meta-circular evaluators are but has an eval function nonetheless (no apply, not necessary). So it looks to me like a meta-circular evaluator is a specific implementation of the "has an eval function" feature.

So, I would simply say that any language that "has an eval function" can do what you are asking for.

The class of languages you ask about is equivalent to "self-hosting" languages. A self-hosting language is the one, for which there exists an implementation of compiler or interpreter in the same language.

Here's a proof.

Each self-hosting language is capable to run its own programs. If you pass, for example, C code to a C program, that program can include a compiler (written in C) that compiles and runs the code. This way a C program executes arbitrary C code.

Assume now you have a language X with such a property that it is capable to run an arbitrary code in that language, the code being passed as a string. Then you can create a program that just takes a string as an input and executes the code within it. This will be an interpreter for X language implemented in X. And this is the definition of a self-hosting language.

Here is what this interpreter would look like:

execute ARGV[0]

So, we've proved that each language with the desired property is self-hosting, and each self-hosting language has this property. This means that the class you requested is equal to the class of self-hosting languages.

Self-hosting possibly. I know Lisp is such a language, and Wikipedia referred to it as a self-hosting compiler.

Homoiconicity is a related concept, but I don't think it's exactly what you're looking for either.

In computer programming, homoiconicity is a property of some programming languages, in which the primary representation of programs is also a data structure in a primitive type of the language itself, from homo meaning the same and icon meaning representation. This makes metaprogramming easier than in a language without this property.

Yes, I always thought this was encompassed by Reflection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top