Programming language for self-modifying code?

后端 未结 14 1781
别那么骄傲
别那么骄傲 2020-12-23 13:05
  • I am recently thinking about writing self-modifying programs, I think it may be powerful and fun. So I am currently looking for a language that allows
相关标签:
14条回答
  • 2020-12-23 13:41

    You can do this in Maple (the computer algebra language). Unlike those many answers above which use compiled languages which only allow you to create and link in new code at run-time, here you can honest-to-goodness modify the code of a currently-running program. (Ruby and Lisp, as indicated by other answerers, also allow you to do this; probably Smalltalk too).

    Actually, it used to be standard in Maple that most library functions were small stubs which would load their 'real' self from disk on first call, and then self-modify themselves to the loaded version. This is no longer the case as the library loading has been virtualized.

    As others have indicated: you need an interpreted language with strong reflection and reification facilities to achieve this.

    I have written an automated normalizer/simplifier for Maple code, which I proceeded to run on the whole library (including itself); and because I was not too careful in all of my code, the normalizer did modify itself. I also wrote a Partial Evaluator (recently accepted by SCP) called MapleMIX - available on sourceforge - but could not quite apply it fully to itself (that wasn't the design goal).

    0 讨论(0)
  • 2020-12-23 13:44

    I highly recommend Lisp. Lisp data can be read and exec'd as code. Lisp code can be written out as data.

    It is considered one of the canonical self-modifiable languages.

    Example list(data):

    '(+ 1 2 3) 
    

    or, calling the data as code

    (eval '(+ 1 2 3)) 
    

    runs the + function.

    You can also go in and edit the members of the lists on the fly.

    edit:

    I wrote a program to dynamically generate a program and evaluate it on the fly, then report to me how it did compared to a baseline(div by 0 was the usual report, ha).

    0 讨论(0)
  • 2020-12-23 13:46

    Malbolge would be a good place to start. Every instruction is self-modifying, and it's a lot of fun(*) to play with.

    (*) Disclaimer: May not actually be fun.

    0 讨论(0)
  • 2020-12-23 13:48

    Personally, I find it quite strange that you find assembly easier to handle than C#. I find it even stranger that you think that assembly isn't as powerful: you can't get any more powerful than raw machine language. Anyway, to each his/her own.

    C# has great reflection services, but if you have an aversion to that.. If you're really comfortable with C or C++, you could always write a program that writes C/C++ and issues it to a compiler. This would only be viable if your solution doesn't require a quick self-rewriting turn-around time (on the order of tens of seconds or more).

    Javascript and Python both support reflection as well. If you're thinking of learning a new, fun programming language that's powerful but not massively technically demanding, I'd suggest Python.

    0 讨论(0)
  • 2020-12-23 13:48

    May I suggest Python, a nice very high-level dynamic language which has rich introspection included (and by e.g. usage of compile, eval or exec permits a form of self-modifying code). A very simple example based upon your question:

    def label1(a,b,c):
        c=a+b
        return c
    
    a,b,c=10,20,0    
    print label1(a,b,c) # prints 30
    
    newdef= \
    """
    def label1(a,b,c):
        c=a*b
        return c
    """
    exec(newdef,globals(),globals())
    
    print label1(a,b,c) # prints 200
    

    Note that in the code sample above c is only altered in the function scope.

    0 讨论(0)
  • 2020-12-23 13:49

    In high-level languages where you compile and execute code at run-time, it is not really self-modifying code, but dynamic class loading. Using inheritance principles, you can replace a class Factory and change application behavior at run-time.

    Only in assembly language do you really have true self-modification, by writing directly to the code segment. But there is little practical usage for it. If you like a challenge, write a self-encrypting, maybe polymorphic virus. That would be fun.

    0 讨论(0)
提交回复
热议问题