-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();
}
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • 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. – Dave Newton Oct 22 '13 at 18:10
  • Okay, what do I search for then specifically? Because I don't understand the underlying terminology very well. I have searched for "languages that feature self programming" , and come up with useless results. – Alexander Ryan Baggett Oct 22 '13 at 18:27
  • How about... "languages with metaprogramming" or something similar? – Dave Newton Oct 22 '13 at 18:29
  • Well, I have done research and found maybe metaprogramming is not the correct term I am looking for. Self-modifying code is closer. But still not exactly what I am looking for in terms of terminology. So what is the correct term for languages that allow code blocks to be built up dynamically then be paramaterized and executed later? – Alexander Ryan Baggett Oct 22 '13 at 18:36
  • 1
    http://stackoverflow.com/questions/3057487/self-modifying-code – Kevin Panko Oct 22 '13 at 18:41
  • 2
    http://en.wikipedia.org/wiki/First-class_function – Kevin Panko Oct 22 '13 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. – Dave Newton Oct 22 '13 at 18:45
  • I think the closest words now that I can find are "Code as data" like what is done in lisp. I think that would be a good answer for my question. – Alexander Ryan Baggett Oct 22 '13 at 19:04
  • 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. – Ingo Oct 23 '13 at 13:03

1 Answers1

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • `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. –  Oct 22 '13 at 18:16
  • @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. – Ira Baxter Oct 22 '13 at 18:20
  • 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". –  Oct 22 '13 at 18:25
  • OP: "... I just want to know which languages contain this programming feature..." – Ira Baxter Oct 22 '13 at 20:01
  • 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? – NullUserException Oct 25 '13 at 16:18
  • @NullUserException: I think there was a tech topic that seemed worth discussion, but I no longer remember what it was. You and I are both in Austin, might be worth a brief chat anyway. – Ira Baxter Oct 25 '13 at 20:20