自定义编程语言:如何?
希望这个问题不会太复杂或含糊。我知道我脑子里想要什么,所以祈祷我能用文字表达出来。
我正在寻找一种具有我自己规范的语法的语言,因此我认为我需要自己创建一种语言。在过去的几天里,我一直在阅读有关编译器、词法分析器、解析器、汇编语言、虚拟机等的内容,并且我正在努力根据实现目标所需的内容来整理所有内容(底部附加的文件一些规格)。本质上,我对我需要使用哪些工具来继续前进感到非常困惑。
一点背景知识:所制作的语言有望用于实现多人游戏、基于文本的 MUD 服务器。因此,它需要简单的内置功能来创建/维护客户端 TCP/IP 连接、非阻塞 IO、通过 SQL 进行数据库访问或类似功能。我还对安全性感兴趣,因为我不希望为这种语言编写的代码在没有专业软件的情况下被普通公众窃取和使用。这可能意味着它应该编译为目标代码
那么,创建适合 这些规范
我的结论如下。这只是我最有根据的猜测,所以如果您认为我朝错误的方向前进,请与我争论。我主要只是想看看当专家们发表评论时我是多么困惑。
为了代码安全,我应该想要一种可以在虚拟机中编译和运行的语言。如果我这样做的话,我会有很多工作要做,不是吗?自己写一个虚拟机、汇编语言,然后在高层,自己处理IO、套接字等的代码库,而不是使用现有的模块?
我只是很困惑。
我不确定我是否有道理。
如果有人能让我的大脑安定一点点,我会真诚地感激!或者,如果我偏离了路线,并且有更简单的方法可以做到这一点,请告诉我!
Hopefully this question won't be too convoluted or vague. I know what I want in my head, so fingers crossed I can get this across in text.
I'm looking for a language with a syntax of my own specification, so I assume I will need to create one myself. I've spent the last few days reading about compilers, lexers, parsers, assembly language, virtual machines, etc, and I'm struggling to sort everything out in terms of what I need to accomplish my goals (file attached at the bottom with some specifications). Essentially, I'm deathly confused as to what tools specifically I will need to use to go forward.
A little background: the language made would hopefully be used to implement a multiplayer, text-based MUD server. Therefore, it needs easy inbuilt functionality for creating/maintaining client TCP/IP connections, non-blocking IO, database access via SQL or similar. I'm also interested in security insofar as I don't want code that is written for this language to be able to be stolen and used by the general public without specialist software. This probably means that it should compile to object code
So, what are my best options to create a language that fits these specifications
My conclusions are below. This is just my best educated guess, so please contest me if you think I'm heading in the wrong direction. I'm mostly only including this to see how very confused I am when the experts come to make comments.
For code security, I should want a language that compiles and is run in a virtual machine. If I do this, I'll have a hell of a lot of work to do, won't I? Write a virtual machine, assembler language on the lower-level, and then on the higher-level, code libraries to deal with IO, sockets, etc myself, rather than using existing modules?
I'm just plain confused.
I'm not sure if I'm making sense.
If anyone could settle my brain even a little bit, I'd sincerely appreciate it! Alternatively, if I'm way off course and there's a much easier way to do this, please let me know!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
设计自定义的特定领域编程语言是解决问题的正确方法。实际上,几乎所有问题都可以通过 DSL 更好地解决。您可能想在 Google 上搜索的术语是:
领域特定语言
和面向语言的编程
。有人会说设计和实现编译器是一项复杂的任务。这根本不是真的。实现编译器是一件微不足道的事情。有大量可用的高质量编译器,您所需要做的就是定义从您自己的语言到另一种语言或其他语言的组合的简单转换。你需要一个解析器 - 如今这不是什么大问题,有 Antlr 和大量自制的基于 PEG 的解析器生成器。您需要一些东西来定义您的语言的语义 - 现代函数式编程语言在这个领域大放异彩,您所需要的只是支持 ADT 和模式匹配的东西。您需要一个目标平台。有很多可能性:JVM 和 .NET、C、C++、LLVM、Common Lisp、Scheme、Python 以及其他由文本字符串组成的东西。
有现成的框架可以用来构建您自己的语言。从字面上看,任何 Common Lisp 或 Scheme 实现都可以用作这样的框架。 LLVM 也拥有您需要的所有东西。 .NET 工具箱还可以——有很多可用的代码生成选项。有一些专门的框架,例如这个,用于构建具有复杂语义的语言。
选择您喜欢的任何方式。这很容易。比你想象的容易得多。
Designing a custom domain-specific programming language is the right approach to a problem. Actually, almost all the problems are better approached with DSLs. Terms you'd probably like to google are:
domain specific languages
andlanguage-oriented programming
.Some would say that designing and implementing a compiler is a complicated task. It is not true at all. Implementing compilers is a trivial thing. There are hordes of high-quality compilers available, and all you need to do is to define a simple transform from your very own language into another, or into a combination of the other languages. You'd need a parser - it is not a big deal nowdays, with Antlr and tons of homebrew PEG-based parser generators around. You'd need something to define semantics of your language - modern functional programming langauges shines in this area, all you need is something with a support for ADTs and pattern matching. You'd need a target platform. There is a lot of possibilities: JVM and .NET, C, C++, LLVM, Common Lisp, Scheme, Python, and whatever else is made of text strings.
There are ready to use frameworks for building your own languages. Literally, any Common Lisp or Scheme implementation can be used as such a framework. LLVM has all the stuff you'd need too. .NET toolbox is ok - there is a lot of code generation options available. There are specialised frameworks like this one for building languages with complex semantics.
Choose any way you like. It is easy. Much easier than you can imagine.
编写自己的语言和工具链来解决看似标准的问题听起来是错误的方法。您最终将开发另一种语言,而不是编写 MUD。
许多游戏开发者采取使用脚本语言来描述自己的游戏世界的方法,例如参见:http://www.gamasutra.com/view/feature/1570/reflections_on_building_third_.php
另请参阅:https://stackoverflow.com/questions/356160/which-game-scripting-language-is-better-to-use-lua-or -python 用于在本例中使用现有语言(Pythong 和 LUA)进行游戏内脚本编写。
Writing your own language and tool chain to solve what seems to be a standard problem sounds like the wrong way to go. You'll end up developing yet another language, not writing your MUD.
Many game developers take an approach of using scripting languages to describe their own game world, for example see: http://www.gamasutra.com/view/feature/1570/reflections_on_building_three_.php
Also see: https://stackoverflow.com/questions/356160/which-game-scripting-language-is-better-to-use-lua-or-python for using existing languages (Pythong and LUA) in this case for in-game scripting.
因为您对编译器和创建计算机语言了解不多:不要。世界上大约有五个人擅长于此。
如果你还想尝试:创建一门好的通用语言至少需要 3 年时间。全职。这是一项巨大的任务。
因此,您应该尝试一种现有的语言,它已经解决了几乎所有问题,除了“自定义”部分之外。但也许该语言的表现比您想象的要好,并且您根本不需要“自定义”部分。
这里有两个选项:
Python,一种漂亮的脚本语言。虚拟机将为您将语言编译为字节代码,无需浪费时间使用编译器。语法非常灵活,但由于 Python 中的所有内容都有充分的理由,因此它不太灵活。
Java。借助新的 Xtext 框架,您可以在几分钟内创建自己的语言。这并不意味着您可以在几分钟内创建一种好的语言。只是一种语言。
Python 附带了很多库,但如果您需要其他任何东西,空气很快就会变得稀薄。从积极的一面来看,您可以在短时间内编写大量优秀且可靠的代码。一行Python通常等于10行Java。
Java 并没有太多的装饰,但实际上有数以百万计的框架,它们可以完成您能想象到的所有事情……以及许多您无法完成的事情。
也就是说:为什么要限制自己只使用一种语言?使用 Jython,您可以在 Java VM 中运行 Python 源代码。因此,您可以用 Java 编写核心(Web 服务器、SQL 等),用 Python 编写灵活的 UI 部分、冒险等内容。
Since you don't know a lot about compilers and creating computer languages: Don't. There are about five people in the world who are good at it.
If you still want to try: Creating a good general purpose language takes at least 3 years. Full time. It's a huge undertaking.
So instead, you should try one of the existing languages which solves almost all of your problems already except maybe the "custom" part. But maybe the language does things better than you ever imagined and you don't need the "custom" part at all.
Here are two options:
Python, a beautiful scripting language. The VM will compile the language into byte code for you, no need to waste time with a compiler. The syntax is very flexible but since there is a good reason for everything in Python, it's not too flexible.
Java. With the new Xtext framework, you can create your own languages in a couple of minutes. That doesn't mean you can create a good language in a few minutes. Just a language.
Python comes with a lot of libraries but if you need anything else, the air gets thin, quickly. On a positive side, you can write a lot of good and solid code in a short time. One line of python is usually equal to 10 lines of Java.
Java doesn't come with a lot of frills but there a literally millions of frameworks out there which do everything you can image ... and a lot of things you can't.
That said: Why limit yourself to one language? With Jython, you can run Python source in the Java VM. So you can write the core (web server, SQL, etc) in Java and the flexible UI parts, the adventures and stuff, in Python.
如果您确实想创建自己的小语言,一个更简单且通常更快的解决方案是查看诸如 lex 和 yacc 以及类似系统 (ANTLR 是一种流行的替代方案),然后您可以为现有虚拟机生成代码或自己制作一个简单的代码。
自己制作这一切是一次很棒的学习经历,并将帮助您了解其他虚拟机幕后发生的事情。
If you really want to create your own little language, a simpler and often quicker solution is to look at tools like lex and yacc and similar systems (ANTLR is a popular alternative), and then you can generate code either to an existing virtual machine or make a simple one yourself.
Making it all yourself is a great learning-experience, and will help you understand what goes on behind the scenes in other virtual machines.
理解编程语言设计和实现概念的绝佳来源是计算机的结构和解释麻省理工学院出版社的程序。对于任何想要设计和实现一种语言的人,或者任何希望成为一名更好的程序员的人来说,这都是一本很好的读物。
An excellent source for understanding programming language design and implementation concepts is Structure and Interpretation of Computer Programs from MIT Press. It's a great read for anyone wanting to design and implement a language, or anyone looking to generally become a better programmer.
据我所知,您想知道如何开发自己的编程语言。
如果是这样,您可以通过不同的方法来完成此任务。几分钟前我刚刚完成了自己的工作,并使用 HTML 和 Javascript(以及 DOM)来开发自己的工作。我使用了很多 x.split 和 x.indexOf("code here")!=-1 来这样做......我没有太多时间给出例子,但是如果你使用 W3schools 并搜索“indexOf”和“分裂”我相信你会找到你可能需要的东西。
我真的很想向您展示我做了什么并通过了下面的代码,但由于我的作品可能被盗和索赔,我不能这样做。
我在这里只是想说,你可以使用 HTML 和 Javascript 来创建你自己的编程语言,这样你和其他人就不会抱有太低的希望。
我希望这对大多数事情都有帮助......
From what I can understand from this, you want to know how to develop your own programming language.
If so, you can accomplish this by different methods. I just finished up my own a few minutes ago and I used HTML and Javascript (And DOM) to develop my very own. I used a lot of x.split and x.indexOf("code here")!=-1 to do so... I don't have much time to give an example, but if you use W3schools and search "indexOf" and "split" I am sure that you will find what you might need.
I would really like to show you what I did and past the code below, but I can't due to possible theft and claim of my work.
I am pretty much just here to say that you can make your own programming language using HTML and Javascript, so that you and other might not get their hopes too low.
I hope this helps with most things....