在运行时生成字符串并将其作为程序执行 [英] Generating strings and executing them as programs during runtime

查看:156
本文介绍了在运行时生成字符串并将其作为程序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很难回答的问题,我不确定它的正确用语是(如果有的话).我很好奇什么语言允许您在程序执行期间构建"字符串,然后将其作为程序的一部分执行.我所知道的唯一允许您执行此操作的语言是Snobol.

This is a tough question to word and I'm not sure what the proper term for it would be (if any). I'm curious what languages allow you to "build up" a string during program execution, and then execute it as part of the program. The only language that I know of that allows you to do this is Snobol.

但是,阅读Tcl的维基百科条目,听起来好像也可以做到这一点?

Reading the wikipedia entry for Tcl however, it sounds like it may be able to do this also?

我一直认为这是一个很不错的功能,即使它可能很少使用.谢谢.

I always thought this was a nifty feature even if it may not be used much. Thanks.

PS:将使用Snobol,Spitbol对其进行标记,但没有创建新标记的声誉.

PS: Would tag this with Snobol, Spitbol, but don't have the reputation to create new tags.

推荐答案

我很好奇什么语言允许您在程序执行期间构建"字符串,然后将其作为程序的一部分执行.

I'm curious what languages allow you to "build up" a string during program execution, and then execute it as part of the program.

寻找支持eval的语言,或更常见的是运行时元编程.几乎每种语言都支持eval(甚至包括Haskell这样的强类型静态语言).许多运行时是为主要通过字节码实现的语言而构建的 解释(例如类似Lisp的语言,Erlang或Java)支持以下功能: 在运行时插入新的(字节)代码.一旦可以动态插入新代码,就可以编写eval或进行猴子修补".

Look for languages that support eval, or, more generally, runtime meta-programming. Pretty much every language supports an eval (even strongly, statically typed languages like Haskell). Many runtimes built for languages that are primarily implemented via bytecode interpretation (such as Lisp-like languages, Erlang or Java) support the ability to insert new (byte)code at runtime. Once you can insert new code dynamically, you can write eval, or do "monkey patching".

即使在没有具体支持完整元编程甚至动态链接的语言实现中,通常也存在通过反射机制或代码生成支持库(例如LLVM)在程序员控制下动态生成代码的方法.

Even in language implementations without specific support for full meta-programming, or even dynamic linking, there are often ways to dynamically generate code under programmer control, either via reflection mechanisms or code generation support libraries (such as LLVM).

除了简单的一个阶段eval之外,更普遍的是,支持多阶段计算的语言允许针对任意数量的阶段从一个阶段到下一个阶段生成程序,可以安全地任意嵌套evals.

Beyond just a simple one-stage eval, more generally, languages that support multi-stage computation allow for generation of programs from one stage to the next, for arbitrary numbers of stages, making it possible to safely, arbitrarily nest evals.

引用Taha的话,他是关于多阶段编程模型介绍了许多理论.

To quote Taha, who's thesis on multi-stage programming models introduces much of the theory.

程序生成是用于软件开发的强大而普遍的技术.它已被用来提高代码重用性,产品可靠性和可维护性,性能和资源利用率以及开发人员的生产力

Program generation is a powerful and pervasive technique for the development of software. It has been used to improve code reuse, product reliability and maintainability, performance and resource utilization, and developer productivity

您要寻找的语言通常以某种形式或其他形式提供三种原语:

The languages you're looking for usually provide three primitives, in some form or another:

  • 延迟
  • 拼接
  • 运行

用于将计算延迟一级(例如,将片段引用为字符串),将其拼接到正在运行的程序中,然后执行该片段(以Lisp,反引号,逗号和eval表示).

for delaying computation by one stage (e.g. quoting a fragment as a string), splicing it into a running program, and executing that fragment (in Lisp, back-quote, comma, and eval).

Lisp和评估

  • McCarthy,John, LISP的历史,SIGPLAN不是. 1978年.-引入eval
  • McCarthy, John, History of LISP, SIGPLAN Not. 1978. -- introduces eval

将评估推广到多阶段编程

在多阶段编程中:

  • Taha,多阶段编程:其理论和应用程序
  • Nielson,Flemming和Nielson,Hanne Riis,两级功能语言,介绍了两级语言.
  • Taha,Walid和Sheard,Tim,带有显式注释的多阶段编程-支持所有运行时元编程技术的简单运算符.

为多阶段编程提供类型

对多级计算的形式化描述非常棘手,涉及非凡的技术(用于编程语言),如模态逻辑.

Formal descriptions of multi-stage computation are quite tricky, and involve unusual techniques (for programming languages) like modal logic.

为元程序提供类型:

  • Wickline,Philip和Lee,Peter和Pfenning,Frank和Davies,Rowan,Modal类型作为运行时代码生成的暂存规范.

安全问题

将多阶段编程的语义形式化的棘手原因说明了为什么它们经常使系统混乱,以及为什么eval会带来很多安全问题:不清楚何时以及确切地执行什么代码哪些数据被转换为代码.从一个阶段到另一个阶段进行名称捕获非常棘手,从而导致代码注入攻击.这种复杂性对安全性没有帮助.

The trickiness of formalzing the semantics of multi-stage programming explains why they're often confusing systems to work with, and why eval can open up so many security concerns: it becomes unclear what code is executing when, and exactly what data is being turned into code. Getting name capture from one stage to the next is tricky, leading to code injection attacks. Such complexity doesn't help security.

这篇关于在运行时生成字符串并将其作为程序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆