为我的脚本语言切换到 DLR 的主要优点是什么?
我已经使用 Antlr 编写了一个 DSL,从我的语法文件生成词法分析器和解析器。 解析器生成一个抽象语法树,其中包含我可以计算的各种节点(例如函数节点)。 在函数节点的代码中,我负责绑定 - 检查函数名称和参数类型是否与函数库中的匹配。 我在这里有一些简单的缓存来优化函数查找(如果我用两个整数调用 A+B,那么下次我使用加运算符时很可能会使用 2 个整数)。
最近我一直在阅读有关 DLR 的内容,它似乎是为适应这种类型的脚本语言实现而设计的。 乍一看,它看起来不像它生成解析器或词法分析器,但它似乎确实有助于实现的其他部分。 我想知道改用 DLR 对我来说主要有什么好处。
I have written a DSL using Antlr to generate a lexer and parser from my grammar file. The parser generates an abstract syntax tree which contains various nodes (e.g. a function node) which I can calculate. In the code for the function nodes I take care of binding - checking function names and parameter types for matches from a library of functions. I have some simple caching here to optimize the function look up (if I call A+B with two ints then there is a strong chance the next time I use the plus operator it will be with 2 ints).
Recently I have been reading about the DLR and it seems to be designed to accomodate this type of scripting language implementation. At first blush it doesnt look to me like it generates the parser or lexer but it seems it does assist with the other parts of the implementation. I was wondering what would be the main advantages to me of switching to using the DLR.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您仔细地实现绑定,DLR 将为您提供非常强大的缓存机制 - 可能比您自己实际能够做到的更优化。 此外,您更有可能获得与其他语言的良好互操作性,因为您将使用“标准”动态对象协议。
例如,C# 4 无需任何额外工作,只需使用
dynamic
类型即可调用您的语言。 为了在没有 DLR 的情况下做到这一点,您必须生成“正常”静态 CLR 类型。很难确定会有多少优势,因为我们不知道您想使用您的语言做什么,也不知道它已经做了多少。 然而,显然有很多非常聪明的人在 DLR 上工作 - 在我看来,如果您要创建一种在 .NET 上运行的动态语言,那么利用他们的工作是有意义的。
If you implement the binding carefully, the DLR will give you a very powerful caching mechanism - probably more heavily optimised than you'd be realistically able to do on your own. Also, you're more likely to get good interoperability with other languages, as you'll be using a "standard" dynamic object protocol.
For example, C# 4 would be able to call into your language without any extra work, just by using the
dynamic
type. In order to do that without the DLR, you'd have to generate "normal" static CLR types.It's hard to know for sure how much advantage there'd be because we don't know what you want to use your language for, or how much it already does. However, there are obviously lots of very smart people working on the DLR - it seems to me that if you're creating a dynamic language to run on .NET, it would make sense to take advantage of their work.
完全访问 .NET 框架是最重要的。
Full access to the .NET framework is the big one.