scala 中的 Brainfuck 编译器
想要制作一些领域特定语言(DSL)进行练习,首先想到的是编写 Brainfuck< 的解释器或编译器/a>. 第一个想法是重写函数,例如它们将表现为 Brainfuck 命令:">"
、"<"
、"+"
、 "-"
、"."
、"、"
、"["
、"]"
。不幸的是,您不能将函数声明为 "."
。
有更好的解决方案用Scala编写吗?
Want to make some Domain Specific Language(DSL) for practice, first idea it is to write interpreter or compiler of Brainfuck.
First idea was to override functions such as they will behave as Brainfuck commands: ">"
, "<"
, "+"
, "-"
, "."
, ","
, "["
, "]"
. Unfortunately you cannot decalare function as "."
.
Is there better solution to write it in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在问题中没有具体说明这一点,但似乎当您说 DSL 时,您的意思是 Internal DSL< /a>?
内部 DSL 很棒,但从根本上讲,您总是受到您尝试使用的语言的语法的限制。 Scala 是一种特别适合编写内部 DSL 的语言,因为它具有简单而灵活的语法。但它并不是无限灵活的。
您可能想要探索的其他途径可能是:
You don't say this specifically in your question, but it seems that when you say DSL, you mean Internal DSL?
Internal DSLs are great, but fundamentally you're always limited by the syntax of the language you're trying to use. Scala is a particularly good language for writing an internal DSL, because it has a simple and flexible syntax. But it's not infinitely flexible.
Other avenues you might want to explore might be:
我想您知道这个示例。
还有这个示例建议评论中的Mikaël Mayer。
I suppose that you are aware of this example.
Also this example suggested by Mikaël Mayer in comments.
我编写了一个 BrainFuck 解释器,它使用 Scala 解析器组合器。源代码位于此处(如果是)可能有帮助。
I wrote a BrainFuck interpreter that makes use of Scala parser combinators. the source code is here if it may help.