views:

163

answers:

7

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?

A: 

Self-interpretive and self-hosting? See http://en.wikipedia.org/wiki/Self-interpreter, http://en.wikipedia.org/wiki/Self-hosting.

Chris Dennett
+1  A: 

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

Bob
AFAIK, self-hosted language means that there's an implementation written in the same language.
Javier
+1  A: 

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.

ire_and_curses
This is basically the term we used in my graduate language design class. LISP is a good language for playing with this kind of stuff. Try writing a simple LISP evaluator using LISP in ~150 lines. It's good fun! :D
Stuart Branham
A: 

Yes, I always thought this was encompassed by Reflection.

Zachary
As I understood it, reflection is about a program being able to examine its own constructs, not about being able to execute arbitrary code. For example, both C# and Java have reflection facilities, but neither has a keyword for executing code provided as a string.
itowlson
From the Wikipedia page: "Evaluate a string as if it were a source code statement at runtime"This isn't the only thing reflection is, but it is a subset of it.
Zachary
@Zachary, yes something like the Wikipedia quote.
M.S.
The quote from the page describing reflection.
Zachary
+1  A: 

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.

Greg Hewgill
The one I was looking for evals the code in a string construct. From what I see, homoiconicity includes say a `code` type. But yes, close enough.
M.S.
+2  A: 

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.

Pavel Shved
But what happens when you run out of turtles?
SamB
+2  A: 

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.

slebetman