We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 3 months ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
这个问题的范围并不完全清楚,但我有一种预感,PyPy、嵌入其他完整语言和类似的解决方案可能有点矫枉过正。听起来 iamgopal 可能真的对 解释器模式 或 小语言。
如果您想要支持的语言非常小(请参阅解释器模式链接),那么您自己在 Python 中手动编码不会太难。您可以编写一个简单的解析器(Google 周围;这里有一个示例),然后遍历 AST 并评估用户表达式。
但是,如果您预计它会被长期使用或被很多人使用,那么可能值得使用真正的语言来解决该问题。 (如果您的用户已经熟悉基本的 Python 语法,我建议使用 Python 本身)。
The question isn't completely clear on scope, but I have a hunch that PyPy, embedding other full languages, and similar solutions might be overkill. It sounds like iamgopal may really be interested in something more like Interpreter Pattern or Little Language.
If the language you want to support is really small (see the Interpreter Pattern link), then hand-coding this yourself in Python won't be too hard. You can write a simple parser (Google around; here's one example), then walk the AST and evaluate user expressions.
However, if you expect this to be used for a long time or by many people, it may be worth throwing a real language at the problem. (I'd recommend Python itself if your users are already familiar with basic Python syntax).
Ren'Py 是对基于 Python 本身构建的 Python 语法的修改,使用 stdlib 中的语言工具。
Ren'Py is a modification to Python syntax built on top of Python itself, using the language tools in the stdlib.
为了您的用户的利益,不要使用基于 XML 的语言 - XML 对于编程语言来说是一个糟糕的基础,您的用户会因此讨厌您。
这是一个建议。使用适合您的语言的 Python 严格子集。使用编译器模块将其代码转换为抽象语法树并遍历树在将 AST 转换为 python 字节码之前验证代码是否符合您的子集。
注意,我刚刚检查了文档,发现编译器包在 2.6 中已被弃用,并在 Python 3.x 中被删除。有谁知道这是为什么?
For your user's sake, don't use an XML based language - XML is an awful basis for a programming language and your users will hate you for it.
Here is a suggestion. Use a strict subset of Python for your language. Use the compiler module to convert their code into an abstract syntax tree and walk the tree to to validate that the code conforms to your subset before converting the AST into python bytecode.
N.B. I just checked the docs and see that the compiler package is deprecated in 2.6 and removed in Python 3.x. Does anyone know why that is?
许多模板语言,例如 Cheetah、Django 模板、Genshi、Mako、Mighty 都可以作为示例。
Numerous template languages such as Cheetah, Django templates, Genshi, Mako, Mighty might serve as an example.
为什么不是 Python 本身呢?只要小心一点,您就可以使用
eval
来运行用户代码。解释型脚本语言的好处之一是您不需要另一种额外的脚本语言!
Why not Python itself? With some care you can use
eval
to run user code.One of the good thing about interpreted scripting languages is that you don't need another extra scripting language!
如果您需要的只是简单的 if 语句和表达式,我确信解析每一行并不是一项可怕的任务。就像
只需编写一个类,在解析第一个单词时,检查它是否是“if、elif、else”等,如果是,则检查一个标志并设置一个标志,说明您要么正在执行,要么不执行,直到下一个单词有条件的。如果不是条件,则根据第一个关键字调用一个函数,该函数会以某种方式修改程序状态。
该类可以存储一些本地执行状态(我们是否处于 if 语句中?如果是,我们是否正在执行此分支?)并拥有另一个包含一些全局应用程序状态(可通过 if 语句检查的标志等)的类。
在您的情况下,这可能是错误的做法(它很容易出现错误,如果您不正确处理脚本中的数据,则会很危险),但如果您决定解释自己的迷你脚本,那么这至少是一个开始语言。
不过说真的,如果你尝试这样做,一定要非常非常小心。不要为脚本提供任何它们绝对不需要的功能,因为这样做几乎肯定会打开安全漏洞。
别说我没有警告过你。
If all you need is simple if statements and expressions, I'm sure it wouldn't be an awful task to parse each line. Something like
Just write a class which, while parsing takes the first word, checks if it's "if, elif, else," etc, and if so, check a flag and set a flag saying you either are or are not executing until the next conditional. If it's not a conditional, call a function based on the first keyword that would modify the program state in some way.
The class could store some local execution state (are we in an if statement? If so are we executing this branch?) and have another class containing some global application state (flags that are checkable by if statements, etc).
This is probably the wrong thing to do in your situation (it's very prone to bugs, it's dangerous if you don't treat the data in the scripts correctly), but it's at least a start if you do decide to interpret your own mini-language.
Seriously though, if you try this, be very, very, srs careful. Don't give the scripts any functionality that they don't definitely need, because you are almost certainly opening security holes by doing something like this.
Don't say I didn't warn you.
PLY (Python Lex-Yacc)
是你感兴趣的事情。
PLY (Python Lex-Yacc)
is something of your interest.
Common Lisp(或任何其他 Lisp)可能是该任务的最佳选择。因为 Lisp 可以轻松地使用强大的宏扩展宿主语言并构建 DSL(领域特定语言)。
Possibly Common Lisp (or any other Lisp) will be the best choice for that task. Because Lisp make it possible to easily extend host language with powerful macroses and construct DSL (domain specific language).