Programming languages 用于自修改代码的编程语言? 我最近正在考虑编写自我修改程序,我认为这可能是强大而有趣的。因此,我目前正在寻找一种允许轻松修改程序自身代码的语言 我读过关于C#(作为一种方法)和在运行时编译和执行代码的能力,但这太伤人了 我也在考虑组装。更改正在运行的代码比较容易,但功能不是很强大(非常原始)

Programming languages 用于自修改代码的编程语言? 我最近正在考虑编写自我修改程序,我认为这可能是强大而有趣的。因此,我目前正在寻找一种允许轻松修改程序自身代码的语言 我读过关于C#(作为一种方法)和在运行时编译和执行代码的能力,但这太伤人了 我也在考虑组装。更改正在运行的代码比较容易,但功能不是很强大(非常原始),programming-languages,dynamic-languages,self-modifying,Programming Languages,Dynamic Languages,Self Modifying,您能推荐一种支持在运行时修改代码的强大语言或功能吗? 例子 这就是我在运行时修改代码的意思: Start: a=10,b=20,c=0; label1: c=a+b; .... label1= c=a*b; goto label1; 并且可能正在建立一个说明列表: code1.add(c=a+b); code1.add(c=c*(c-1)); code1. execute(); 这是一个很好的开始。每一条指令都是自我修改的,玩起来很有趣(*) (*)免责声

您能推荐一种支持在运行时修改代码的强大语言或功能吗?

例子 这就是我在运行时修改代码的意思:

  Start:
  a=10,b=20,c=0;
  label1: c=a+b;
  ....
  label1= c=a*b;
  goto label1;
并且可能正在建立一个说明列表:

  code1.add(c=a+b);
  code1.add(c=c*(c-1));
  code1. execute();
这是一个很好的开始。每一条指令都是自我修改的,玩起来很有趣(*)


(*)免责声明:可能并不有趣。

你看过Java吗?Java 6有一个程序集,因此您可以在Java VM中编写代码并进行编译。

就我个人而言,我觉得很奇怪,您发现汇编比C更容易处理。我觉得更奇怪的是,你认为汇编没有那么强大:你不能得到比原始机器语言更强大的功能。不管怎样,每个人都有他/她自己的

C#有很好的反思服务,但如果你不喜欢的话。。如果你对C或C++很满意,你可以编写一个程序,它将C/C++写入到编译器。只有当您的解决方案不需要快速的自重写周转时间(大约几十秒或更长)时,这才是可行的

Javascript和Python都支持反射。如果您正在考虑学习一种新的、有趣的、功能强大但技术要求不高的编程语言,我建议您使用Python。

我可以推荐一种非常高级的动态语言,它包含了丰富的内省功能(例如,通过使用
compile
eval
exec
允许一种形式的自修改代码)。基于您的问题,一个非常简单的示例:

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

请注意,在上面的代码示例中,
c
仅在函数范围内更改。

许多语言允许您在运行时编写代码

  • 口齿不清
  • Perl
  • 蟒蛇
  • PHP
  • 红宝石
  • Groovy(通过GroovyShell)

我强烈推荐Lisp。Lisp数据可以作为代码读取和执行。Lisp代码可以作为数据写入

它被认为是标准的自修改语言之一

示例列表(数据):

或者,将数据作为代码调用

(eval '(+ 1 2 3)) 
运行+函数

您还可以进入并动态编辑列表的成员

编辑:

我编写了一个程序来动态生成一个程序,并动态地对其进行评估,然后向我报告它与基线的比较情况(div by 0是常见的报告,ha)。

就是考虑到这一点而设计的。您也可以尝试,使用反射来修改正在运行的代码并不是未知的


在这两种语言中,您可能要替换整个函数或整个方法,而不是一行代码。Smalltalk方法往往比Lisp函数更细粒度,因此这可能是一个很好的起点。

在运行时编译和执行代码的高级语言中,它并不是真正的自修改代码使用继承原则,您可以替换类工厂并在运行时更改应用程序行为


只有在汇编语言中,您才可以通过直接写入代码段来实现真正的自我修改。但是它几乎没有实际用途。如果您喜欢挑战,请编写一个自加密,可能是多态病毒。这将很有趣。

到目前为止,每个答案都是关于反射/运行时编译的,但是在注释中,您需要修改如果您对在内存中修改自身的实际代码感兴趣

在C#、Java甚至(可移植的)C语言中都无法做到这一点——也就是说,您无法使用这些语言修改加载的内存二进制文件

一般来说,实现这一点的唯一方法是使用汇编,它高度依赖于处理器。事实上,它也高度依赖于操作系统:为了抵御大多数现代操作系统(包括Windows XP+、Linux和BSD)强制执行,这意味着您必须在那些操作系统中编写多态可执行文件,而这些操作系统完全允许多态

在某些解释语言中,程序可能会在运行时修改自己的源代码。但是,Perl、Python(请参阅)和我所知道的每一种Javascript实现都不允许这样做。

I有时,但很少在Ruby中进行自修改代码

有时,您有一种方法不知道您使用的数据是否正确(例如,某些惰性缓存)是否正确初始化。因此,您必须在方法开始时检查数据是否正确初始化,然后可能对其进行初始化。但您实际上只需进行一次初始化,但每次都要进行检查

因此,有时我会编写一个进行初始化的方法,然后用一个不包含初始化代码的版本替换它自己

class Cache
  def [](key)
    @backing_store ||= self.expensive_initialization

    def [](key)
      @backing_store[key]
    end

    @backing_store[key]
  end
end
但老实说,我认为这不值得。事实上,我很不好意思承认,我从来没有真正进行过基准测试,看看这一个条件是否真的有什么不同。(在一个具有积极优化配置文件反馈驱动的JIT编译器的现代Ruby实现上,可能没有。)

请注意,根据您定义“自修改代码”的方式,这可能是您想要的,也可能不是您想要的。您正在替换当前正在执行的程序的某些部分,因此

编辑:现在我想起来了,这种优化没有多大意义。昂贵的初始化无论如何只执行一次。修改可以避免的唯一一件事就是条件优化。
class Cache
  def [](key)
    @backing_store ||= self.expensive_initialization

    def [](key)
      @backing_store[key]
    end

    @backing_store[key]
  end
end
local oldMyFunction = myFunction
myFunction = function(arg)
    if arg.blah then return oldMyFunction(arg) end
    else
        --do whatever
    end
end
#Initialize Variables
x = 1

#Create Code
code = Code()
code + 'global x, code' #Adds a new Code instance code[0] with this line of code => internally             code.subcode[0]
code + "if x == 1:"     #Adds a new Code instance code[1] with this line of code => internally code.subcode[1]
code[1] + "x = 2"       #Adds a new Code instance 0 under code[1] with this line of code => internally code.subcode[1].subcode[0]
code[1] + "del code[1]" #Adds a new Code instance 0 under code[1] with this line of code => internally code.subcode[1].subcode[1]
#Prints
print "Initial Code:"
print code
print "x = " + str(x)
Initial Code:

global x, code
if x == 1:
    x = 2
    del code[1]

x = 1
print "Code after execution:"
code() #Executes code
print code
print "x = " + str(x)
Code after execution:

global x, code

x = 2
class Code:

    def __init__(self,line = '',indent = -1):

        if indent < -1:
            raise NameError('Invalid {} indent'.format(indent))

        self.strindent = ''
        for i in xrange(indent):
            self.strindent = '    ' + self.strindent

        self.strsubindent = '    ' + self.strindent

        self.line = line
        self.subcode = []
        self.indent = indent


    def __add__(self,other):

        if other.__class__ is str:
            other_code = Code(other,self.indent+1)
            self.subcode.append(other_code)
            return self

        elif other.__class__ is Code:
            self.subcode.append(other)
            return self

    def __sub__(self,other):

        if other.__class__ is str:
            for code in self.subcode:
                if code.line == other:
                    self.subcode.remove(code)
                    return self


        elif other.__class__ is Code:
            self.subcode.remove(other)


    def __repr__(self):
        rep = self.strindent + self.line + '\n'
        for code in self.subcode: rep += code.__repr__()
        return rep

    def __call__(self):
        print 'executing code'
        exec(self.__repr__())
        return self.__repr__()


    def __getitem__(self,key):
        if key.__class__ is str:
                for code in self.subcode:
                    if code.line is key:
                        return code
        elif key.__class__ is int:
            return self.subcode[key]

    def __delitem__(self,key):
        if key.__class__ is str:
            for i in range(len(self.subcode)):
                code = self.subcode[i]
                if code.line is key:
                    del self.subcode[i]
        elif key.__class__ is int:
            del self.subcode[key]