Compiled dynamic language
Asked Answered
D

7

7

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 binary 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 :)

Dereism answered 3/11, 2009 at 21:5 Comment(6)
en.wikipedia.org/wiki/Self-modifying_code#High_level_languagesCenterboard
What do you mean "self-modifying code", and for that matter "dynamic"? The two aren't usually considered synonyms. If you could clarify what you want, we could probably help you better.Smokestack
Yes, I had a look at the wiki page before, but I was hoping for other languages, more c/c++ like.Dereism
What is the use case? Why do you want to be able to compile code at runtime? And have you tried simply calling out to gcc or another compiler and loading the code as a dynamic library?Grey
There is no single use case where i need this for. I more just would love to use such a language, play with it. A possible usa case might be AI programming and optimization, but for me its just something i really would like to get my hands onDereism
You can find a sample in c# here : reboltutorial.com/blog/redirect-shell-to-rebol-consoleOperative
L
10

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.

Lanza answered 3/11, 2009 at 21:49 Comment(1)
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.Dereism
P
3

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.
Pronto answered 3/11, 2009 at 21:10 Comment(2)
but it is not natively compiled (runs inside the clr). OP wasn't clear about that though.Headwind
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.Pronto
I
2

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.

Imprimis answered 3/11, 2009 at 21:10 Comment(4)
Yes, i read much about LLVM. It is a very nice project, but not what i need in this particular question :)Dereism
Actually, it sounds like exactly what you need. I've clarified one possible use for it in my answer.Imprimis
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.Dereism
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.Imprimis
H
1

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/

Hercegovina answered 3/11, 2009 at 21:22 Comment(2)
V8 is "Just In Time" compiled, but it still runs in a virtual machine and the asker doesn't want that.Influx
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.Hercegovina
C
0

"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.

Cragsman answered 3/11, 2009 at 21:9 Comment(2)
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.Smokestack
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.Cragsman
S
0

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.

Spiracle answered 3/11, 2009 at 21:19 Comment(2)
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 executableDereism
In that case, definitely go with Perl... you'll never go back.Spiracle
T
0

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.

Tatum answered 3/11, 2009 at 21:58 Comment(1)
I meant, that I don't want to have any VM of that kind. Ruby, java, all that, it's not compiled. I don't want thatDereism

© 2022 - 2024 — McMap. All rights reserved.