创建DSL与嵌入现有语言

时间:2021-06-08 20:46:10

This often comes up: your application has gotten extensive enough that it's time to add some programmability into it to make it flexible. One example might be a finance application - you want to add a formula editor so that you can create your own custom formulas without having to re-compile the code.

这通常会出现:您的应用程序已经足够广泛,以至于是时候在其中添加一些可编程性以使其灵活。一个示例可能是财务应用程序 - 您希望添加公式编辑器,以便您可以创建自己的自定义公式,而无需重新编译代码。

You have to make a choice: do you create your own tokenizer, parser, and interpreter/compiler chain, something which might take a long time and might be done incorrectly? Or do you just embed another scripting language, which has the problem that it'll likely bloat your code and expose your app to security vulnerabilities.

你必须做出选择:你是否创建了自己的标记器,解析器和解释器/编译器链,这可能需要很长时间并且可能不正确?或者你只是嵌入了另一种脚本语言,它的问题是它可能会使你的代码膨胀并使你的应用程序暴露在安全漏洞中。

How would you balance the trade-offs and make this decision?

您将如何平衡权衡并作出此决定?

4 个解决方案

#1


3  

There are no trades offs -- embed a throughly tested, well documented interpretor. Otherwise, you'll end up with an abomination like MAXScript.

没有交易 - 嵌入经过充分测试,记录良好的解释器。否则,你最终会像MAXScript一样憎恶。

#2


2  

How about a plug-in system? There are a few advantages:

插件系统怎么样?有一些优点:

  • Allow customer developers to develop in the environment in which the source app is developed.
  • 允许客户开发人员在开发源应用程序的环境中进行开发。

  • In modern dev. platforms, you gain a lot of control and security through software contracts.
  • 在现代开发中。平台,您通过软件合同获得了很多控制和安全性。

  • If it's designed correctly, you can properly blame bloat and failure to the plug-in that caused it - like Chrome does when Flash or another third party plug-in crashes.
  • 如果它设计正确,你可以正确地归咎于膨胀和导致它的插件失败 - 就像Chrome在Flash或其他第三方插件崩溃时所做的那样。

  • Easy to add extra security through licensing/certs.
  • 通过许可/证书轻松添加额外的安全性。

  • Easy to merge a plug-in with the app if it's amazing and you want all of your customers to have it.
  • 如果它非常棒并且您希望所有客户都拥有它,那么很容易将插件与应用程序合并。

#3


1  

Unless the DSL is simple enough that the parser/interpreter fits on a single page, I would recommend embedding an existing scripting language.

除非DSL足够简单以至于解析器/解释器适合单个页面,否则我建议嵌入现有的脚本语言。

I recently spent several months working on a project I inherited that contained a completely home grown scripting language. I invested a lot of time in understanding the parser & interpreter, so that I could fix bugs, make it thread safe, extend it, optimize it. Plus there was the time to learn and understand the quirks of this new scripting language that woked almost-like-but-not-quite-the-same-as others I already knew. I would much rather have used that time to embed an existing language like Ruby or Lua, and tuning it to fit our needs.

我最近花了几个月的时间研究一个我继承的项目,其中包含一个完全成熟的脚本语言。我花了很多时间来理解解析器和解释器,这样我就可以修复bug,使线程安全,扩展它,优化它。此外,还有时间学习和理解这种新脚本语言的怪癖,这种语言与我已经知道的其他语言几乎相似但不完全相同。我宁愿用那段时间嵌入像Ruby或Lua这样的现有语言,并根据我们的需要进行调整。

The user would have benefited from a language that was easier to program in, with less quirks and gotcha's. I would have benefited from a deeper understanding of the internals of a well-designed & popular language, instead of gaining relativly worthless expert knowledge in 'myScript'.

用户可以从一种更容易编程的语言中受益,而且更少的怪癖和陷阱。我会从对设计精良和流行语言的内部结构的深入理解中受益,而不是在“myScript”中获得相对无价值的专业知识。

#4


0  

I would create my own interpreter using an existing parser generator (like ANTLR or Haskell/Scala's parser combinators). It's really not as hard as all that, and for simple languages, it's really quite easy. I create the implementation for a devilishly simple DSL in an afternoon and it ran perfectly the first time (no bugs).

我将使用现有的解析器生成器(如ANTLR或Haskell / Scala的解析器组合器)创建自己的解释器。它真的没有那么难,对于简单的语言来说,它真的很容易。我在一个下午创建了一个非常简单的DSL的实现,它第一次完美运行(没有错误)。

With that said, you don't want to have to design your own Turing Complete language. If your needs are that sophisticated, you should probably embed a scripting language. If you're on the JVM, JRuby and Clojure are excellent candidates for this sort of thing, particularly given their own strengths in the area of internal DSLs.

话虽如此,您不希望设计自己的图灵完整语言。如果您的需求那么复杂,您应该嵌入脚本语言。如果你在JVM上,JRuby和Clojure是这类东西的绝佳候选者,特别是考虑到他们在内部DSL领域的优势。

#1


3  

There are no trades offs -- embed a throughly tested, well documented interpretor. Otherwise, you'll end up with an abomination like MAXScript.

没有交易 - 嵌入经过充分测试,记录良好的解释器。否则,你最终会像MAXScript一样憎恶。

#2


2  

How about a plug-in system? There are a few advantages:

插件系统怎么样?有一些优点:

  • Allow customer developers to develop in the environment in which the source app is developed.
  • 允许客户开发人员在开发源应用程序的环境中进行开发。

  • In modern dev. platforms, you gain a lot of control and security through software contracts.
  • 在现代开发中。平台,您通过软件合同获得了很多控制和安全性。

  • If it's designed correctly, you can properly blame bloat and failure to the plug-in that caused it - like Chrome does when Flash or another third party plug-in crashes.
  • 如果它设计正确,你可以正确地归咎于膨胀和导致它的插件失败 - 就像Chrome在Flash或其他第三方插件崩溃时所做的那样。

  • Easy to add extra security through licensing/certs.
  • 通过许可/证书轻松添加额外的安全性。

  • Easy to merge a plug-in with the app if it's amazing and you want all of your customers to have it.
  • 如果它非常棒并且您希望所有客户都拥有它,那么很容易将插件与应用程序合并。

#3


1  

Unless the DSL is simple enough that the parser/interpreter fits on a single page, I would recommend embedding an existing scripting language.

除非DSL足够简单以至于解析器/解释器适合单个页面,否则我建议嵌入现有的脚本语言。

I recently spent several months working on a project I inherited that contained a completely home grown scripting language. I invested a lot of time in understanding the parser & interpreter, so that I could fix bugs, make it thread safe, extend it, optimize it. Plus there was the time to learn and understand the quirks of this new scripting language that woked almost-like-but-not-quite-the-same-as others I already knew. I would much rather have used that time to embed an existing language like Ruby or Lua, and tuning it to fit our needs.

我最近花了几个月的时间研究一个我继承的项目,其中包含一个完全成熟的脚本语言。我花了很多时间来理解解析器和解释器,这样我就可以修复bug,使线程安全,扩展它,优化它。此外,还有时间学习和理解这种新脚本语言的怪癖,这种语言与我已经知道的其他语言几乎相似但不完全相同。我宁愿用那段时间嵌入像Ruby或Lua这样的现有语言,并根据我们的需要进行调整。

The user would have benefited from a language that was easier to program in, with less quirks and gotcha's. I would have benefited from a deeper understanding of the internals of a well-designed & popular language, instead of gaining relativly worthless expert knowledge in 'myScript'.

用户可以从一种更容易编程的语言中受益,而且更少的怪癖和陷阱。我会从对设计精良和流行语言的内部结构的深入理解中受益,而不是在“myScript”中获得相对无价值的专业知识。

#4


0  

I would create my own interpreter using an existing parser generator (like ANTLR or Haskell/Scala's parser combinators). It's really not as hard as all that, and for simple languages, it's really quite easy. I create the implementation for a devilishly simple DSL in an afternoon and it ran perfectly the first time (no bugs).

我将使用现有的解析器生成器(如ANTLR或Haskell / Scala的解析器组合器)创建自己的解释器。它真的没有那么难,对于简单的语言来说,它真的很容易。我在一个下午创建了一个非常简单的DSL的实现,它第一次完美运行(没有错误)。

With that said, you don't want to have to design your own Turing Complete language. If your needs are that sophisticated, you should probably embed a scripting language. If you're on the JVM, JRuby and Clojure are excellent candidates for this sort of thing, particularly given their own strengths in the area of internal DSLs.

话虽如此,您不希望设计自己的图灵完整语言。如果您的需求那么复杂,您应该嵌入脚本语言。如果你在JVM上,JRuby和Clojure是这类东西的绝佳候选者,特别是考虑到他们在内部DSL领域的优势。