views:

516

answers:

8

Greetings,

I search for a programming language for which a compiler exists and that supports self modifying code. I’ve heared that Lisp supports these features, but I was wondering if there is a more C/C++/D-Like language with these features.

To clarify what I mean:

I want to be able to have in some way access to the programms code at runtime and apply any kind of changes to it, that is, removing commands, adding commands, changing them. As if i had the AstTree of my programm. Of course i can’t have that tree in a compiled language, so it must be done different. The compile would need to translate the self-modifying commands into their binay equivalent modifications so they would work in runtime with the compiled code.

I don’t want to be dependent on an VM, thats what i meant with compiled :)

A: 

"Dynamic language" is a broad term that covers a wide variety of concepts. Dynamic typing is supported by C# 4.0 which is a compiled language. Objective-C also supports some features of dynamic languages. However, none of them are even close to Lisp in terms of supporting self modifying code.

To support such a degree of dynamism and self-modifying code, you should have a full-featured compiler to call at run time; this is pretty much what an interpreter really is.

Mehrdad Afshari
Some Lisp implementations don't have interpreters per se, but simulate it by compiling on the fly. I suppose it helps when you've got the code in a parsed form in the first place.
David Thornley
David: Indeed. That's what I meant from "...you should have a full-featured compiler to call at run time." An interpreter seamlessly provide this facility, while in a compiled environment, you should basically compile stuff at run time. Nevertheless, it's not much like statically compiled code.
Mehrdad Afshari
+1  A: 

You might want to consider using C++ with LLVM for (mostly) portable code generation. You can even pull in clang as well to work in C parse trees (note that clang has incomplete support for C++ currently, but is written in C++ itself)

For example, you could write a self-modification core in C++ to interface with clang and LLVM, and the rest of the program in C. Store the parse tree for the main program alongside the self-modification code, then manipulate it with clang at runtime. Clang will let you directly manipulate the AST tree in any way, then compile it all the way down to machine code.

Keep in mind that manipulating your AST in a compiled language will always mean including a compiler (or interpreter) with your program. LLVM is just an easy option for this.

bdonlan
Yes, i read much about LLVM. It is a very nice project, but not what i need in this particular question :)
Marenz
Actually, it sounds like exactly what you need. I've clarified one possible use for it in my answer.
bdonlan
Somewhere in my mind I was hoping for some way to directly apply such modifications, without having to compile them again — direct manipulation of the binary code.But It seems this in nearly impossible without going back to assembler.
Marenz
Direct manipulation of binary code is going to be sheer pain. For one, you're not going to be able to do it from a high-level view; the compiler's going to be mixing up execution order, keeping random variables in registers, etc, so the connection between the asm and the C code is non-obvious. The Linux kernel does do some self-patching - but only to a very limited extent, and only on inline assembler segments.
bdonlan
+2  A: 

C# has always allowed for self-modifying code.

  • C# 1 allowed you to essentially create and compile code on the fly.
  • C# 3 added "expression trees", which offered a limited way to dynamically generate code using an object model and abstract syntax trees.
  • C# 4 builds on that by incorporating support for the "Dynamic Language Runtime". This is probably as close as you are going to get to LISP-like capabilities on the .NET platform in a compiled language.
Jonathan Allen
Reflection.Emit
Mauricio Scheffer
but it is not natively compiled (runs inside the clr). OP wasn't clear about that though.
Johannes Rudolph
That isn't necessarily true. C# does compile to native code on some platforms such as iPhone and XBox 360. However, you can't use Reflection.Emit on those platforms.
Jonathan Allen
A: 

Try groovy. It's a dynamic Java-JVM based language that is compiled at runtime. It should be able to execute its own code.

http://groovy.codehaus.org/

Otherwise, you've always got Perl, PHP, etc... but those are not, as you suggest, C/C++/D- like languages.

Lincoln
Perl or PHP would be enough c/c++/d like, i was more trying to exclude things like cobol, lisp and similar.But i don’t want JIT, i really want it completely compiled to and independet executable
Marenz
In that case, definitely go with Perl... you'll never go back.
Lincoln
A: 

JavaScirpt + V8 (the Chrome JavaScript compiler)

JavaScript is

  • dynamic
  • self-modifying (self-evaluating) (well, sort of, depending on your definition)
  • has a C-like syntax (again, sort of, that's the best you will get for dynamic)

And you now can compile it with V8: http://code.google.com/p/v8/

DrJokepu
V8 is "Just In Time" compiled, but it still runs in a virtual machine and the asker doesn't want that.
Callum Rogers
Clearly, self-evaluating language must either run on a JIT compiler or an interpreter, otherwise how could it run data as code? There's no such thing as a 100% precompiled self-evaluating language.
DrJokepu
+10  A: 

Probably there is a reason Lisp is like it is? Lisp was designed to program other languages and to compute with symbolic representations of code and data. The boundary between code and data is no longer there. This influences the design AND the implementation of a programming language.

Lisp has got its syntactical features to generate new code, translate that code and execute it. Thus pre-parsed code is also using the same data structures (symbols, lists, numbers, characters, ...) that are used for other programs, too.

Lisp knows its data at runtime - you can query everything for its type or class. Classes are objects themselves, as are functions. So these elements of the programming language and the programs also are first-class objects, they can be manipulated as such. Dynamic language has nothing to do with 'dynamic typing'.

'Dynamic language' means that the elements of the programming language (for example via meta classes and the meta-object protocol) and the program (its classes, functions, methods, slots, inheritance, ...) can be looked at runtime and can be modified at runtime.

Probably the more of these features you add to a language, the more it will look like Lisp. Since Lisp is pretty much the local maximum of a simple, dynamic, programmable programming language. If you want some of these features, then you might want to think which features of your other program language you have to give up or are willing to give up. For example for a simple code-as-data language, the whole C syntax model might not be practical.

So C-like and 'dynamic language' might not really be a good fit - the syntax is one part of the whole picture. But even the C syntax model limits us how easy we can work with a dynamic language.

Rainer Joswig
Thanks for that elaborating answere. I knew some parts of it already, but i still wondered if it would be possible with a more c-like and if someone did it. I guess I will have a more thorough look at lisp.
Marenz
A: 

You can find a sample in c# here : http://reboltutorial.com/blog/redirect-shell-to-rebol-console/

Rebol Tutorial
A: 

I don’t want to be dependent on an VM, thats what i meant with compiled :)

If that's all you're looking for, I'd recommend Python or Ruby. They can both run on their own virtual machines and the JVM and the .Net CLR. Thus, you can choose any runtime you want. Of the two, Ruby seems to have more meta-programming facilities, but Python seems to have more mature implementations on other platforms.

Jason Baker