简体   繁体   English

如何为应用程序创建计算机或脚本语言?

[英]How do you create a computer or scripting language for an application?

Duplicate of: 重复:

and others I'm too lazy to find right now. 和其他人我现在懒得找。


I'm not asking how to make an incredibly complex language. 我不是在问如何制作一种非常复杂的语言。 I just wanted to understand the basics. 我只是想了解基础知识。 I would use c# as the underlying language. 我会用c#作为底层语言。 I know it's vague. 我知道这很模糊。 I was hoping for something very basic to direct me. 我希望有一些非常基本的东西可以指导我。

I think I'm mostly interested in creating scripting languages. 我想我最感兴趣的是创建脚本语言。 For example, I see people that write programs but then they have a scripting language for their application. 例如,我看到人们编写程序,但后来他们的应用程序有脚本语言。 I do not want to rewrite a windows scripting language. 我不想重写Windows脚本语言。 Say I had a text file reader and for some reason wanted a scripting language to automate something. 假设我有一个文本文件阅读器,并且出于某种原因想要一种脚本语言来自动化某些东西。 I'm not sure how to ask. 我不知道该怎么问。

Thank you. 谢谢。

EDIT - Thank you for the answers. 编辑 - 谢谢你的答案。 I was looking at it more for the learning not the doing at the moment. 我正在考虑更多的学习而不是现在正在做的事情。 I would probably use LUA, but I am trying to learn more about the concept in general. 我可能会使用LUA,但我试图更多地了解这个概念。

You could take a look at LUA - I've used it to great success each time I asked myself the question "How would I automate insert task here in insert one of my apps here ?" 你可以看一下LUA - 我每次问自己“如何在这里 插入我的一个应用程序时自动插入任务 ?”时,我就用它取得了巨大的成功。

Edit: Here are some examples (taken from the links page , admittedly, unwieldy Lua Wiki ) on how you could embed Lua in your app: 编辑:以下是一些例子(取自链接页面 ,诚然,笨拙的Lua Wiki )关于如何在您的应用程序中嵌入Lua:

You can use an existing language like Python or Javascript. 您可以使用Python或Javascript等现有语言。 For example, for Javascript, there is http://www.mozilla.org/rhino/ for Java apps. 例如,对于Javascript,有http://www.mozilla.org/rhino/用于Java应用程序。 So typically you don't need to actually invent a new language, you would just provide a custom API for a language that already exists. 因此,通常您不需要实际发明新语言,只需为已存在的语言提供自定义API。

Some terminology is in order. 一些术语是有序的。 You may be talking about a domain-specific language . 您可能正在谈论特定域的语言 The two basic ways to transform a text file into an "executable": a compiler or an interpreter . 将文本文件转换为“可执行文件”的两种基本方法: 编译器解释器 An interpreter fits the scripting concept better, as it is easier to build and executes lines one at a time. 解释器更适合脚本概念,因为一次构建和执行一行更容易。 Note that beyond a very simple language both writing a decent parser or a decent interpreter are non-trivial. 请注意,除了一种非常简单的语言之外,编写一个体面的解析器或一个体面的解释器都是非常重要的。 The classic work on interpreters is SICP , but this is quite a hard book for beginners. 关于口译员的经典着作是SICP ,但对于初学者来说这是一本非常难的书。

Scott Hanselman mentioned in his latest hanselminutes podcast that integrating IronPython to allow scripting of an existing application was very easy to do. Scott Hanselman在他最新的hanselminutes播客中提到,集成IronPython以允许编写现有应用程序的脚本非常容易。

If you're interested in the end target of having your application be scriptable, then you should definitely consider using an existing language rather than attempting to write your own. 如果您对使应用程序可编写脚本的最终目标感兴趣,那么您应该考虑使用现有语言而不是尝试编写自己的语言。

If you are more interested in the educational experience of writing your own scripting language, then you should go for it! 如果您对编写自己的脚本语言的教育经验更感兴趣,那么您应该去实现它!

There's no need to create a new scripting language there are several eg. 没有必要创建一种新的脚本语言,例如。 Rhino which is a widely used embeddable javascript ( http://www.mozilla.org/rhino/ ) or Jscript from MS, that you can use directly in your product. Rhino是一种广泛使用的可嵌入式javascript( http://www.mozilla.org/rhino/ )或MS的Jscript,您可以直接在您的产品中使用。

I've gone the way that you are asking - I once created my own scheme interpreter. 我已经走了你要问的方式 - 我曾经创建了自己的方案解释器。 This worked really well, but we re-invented a lot of technology and didn't really get a lot of additional benefit. 这非常有效,但我们重新发明了很多技术,并没有真正获得很多额外的好处。 We would have been far better off just using one of the scheme's that were available. 如果只使用其中一个可用的方案,我们会好得多。 I would not make that decision again even though it was fun and successful. 即使它很有趣也很成功,我不会再做出那个决定。

first you need a lexical parser like lex, then a syntax parser like bison. 首先你需要像lex这样的词法解析器,然后是像bison这样的语法解析器。

then you can work with the syntax parser to create an interpreter to 'execute' the syntax results. 然后你可以使用语法分析器来创建一个解释器来“执行”语法结果。

that's how the most scripting languages do. 这就是大多数脚本语言的作用。

ps: another way is to practice by writing shells - shell scripts (bash, csh, or sh) are highly simplified scripting languages. ps:另一种方法是通过编写shell来练习 - shell脚本(bash,csh或sh)是高度简化的脚本语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM