-1

Basically, I was curious (a dangerous thing for sure) what language or languages allow you to build up a block of code dynamically to be executed later.

I have looked at http://en.wikipedia.org/wiki/Self-modifying_code I did not find what I was looking for there.

This is not a debate, I am not asking about which one is better than any others. I just want to know which languages contain this programming feature or something similar to it. I would like to see some example code illustrating this feature too if you can.

I have done something similar before in ASP.NET and dynamically built up javascript code to be sent to the page to be executed, or C# generating an SQL query, But never before within the same language. i.e. C# generating C#.

Here is an example of what it could look like (I am writting in a madeup C#/Javaish language)

/ represents a code block escape sequence

CodeBlock codeblock = new CodeBlock();

codeblock+= / print("interesting "); int x = 0;  /

for(int i=0; i++; i<10)
{
  codeblock+= / for(int i=0; i++; i<10) {  x++; Canvas.Draw(new line(x,x+50); } /

}

executionMethod(codeblock);

public void executionMethod(ExecutableCode block)
{
  block.exectute();
}
9
  • 4
    A trivial search of the web should provide a list of more languages than any one person has time to research. And almost any language can construct and load itself. Canonical examples of what you probably mean to ask are things like Lisp, Ruby, Python, blah blah blah. Commented Oct 22, 2013 at 18:10
  • 1
    stackoverflow.com/questions/3057487/self-modifying-code Commented Oct 22, 2013 at 18:41
  • 2
    en.wikipedia.org/wiki/First-class_function Commented Oct 22, 2013 at 18:43
  • 1
    It depends what you mean by that. There's "eval" mechanisms, but even that isn't necessary; Java can compile itself. Lisp macros are expanded automatically. Passing around functions is also one way of describing it, but different. Commented Oct 22, 2013 at 18:45
  • 1
    Your example doesn't even need eval. This could well be done in standard Haskell. Basically, you have a list [0..9] and want to construct a list [drawLine 0 50, drawLine 1 51, ...] which you could "run" with a standard function like sequence. Commented Oct 23, 2013 at 13:03

1 Answer 1

2

"executing a dynamically constructed block of code" is not the same as "metaprogramming".

The ability to execute a dynamic code block is easy to detect: the language has a feature like "eval" (as in your example).

"Metaprogramming" is the ability for one program to manipulate others (sometimes applied to itself). It isn't necessarily a property of the programming language; it can be simply a massive set of facilities that happen to be implemented in some language that are designed to support these activities, e.g., program transformation tools. Metaprogramming implemented directly by language features is often short of complete generality; you can do what the langauge designers decided to let you do, and no more. (Try renaming a variable in most of the "reflective" langauges that exist). That's where the more general tooling is more effective; it doesn't necessarily have limits imposed by langauge designers.

Sign up to request clarification or add additional context in comments.

6 Comments

eval is an easy way to dynamically construct code, but not necessary. For example, C can be compiled with libclang and loaded using either MCJIT or (after being turned into a dynamic library) with ordinary dynamic. I agree that metaprogramming includes much more than run-time code generation.
@delnan: by your standards, all Turing capable languages have the ability to do eval and metaprogramming. The point of the question isn't whether it is possible in the abstract, but whether the langauge/tool has the capability embedded already. I think people would agree that using MCJIT means that "C" does not have built-in eval or metaprogramming facilities.
Not built-in, but outside of philosophical debates and language flame wars that hardly matters. What matters for using it is how easy to use it is - and that is mostly a matter of someone packaging it up in an easier-to-use library (insofar string manipulation can be easy in C). And yes, in principle any turing complete language can do something similar - but there's a huge practical difference between "theoretically possible by turing completeness" (cf. turing tarpits) and "actually done in mature real-world software".
OP: "... I just want to know which languages contain this programming feature..."
I was going through a todo list here and I found something from a long time ago. You ask me to contact you outside of the site, do you still want me to do that?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.