有没有一种语义接近英语的编程语言?
大多数语言允许“调整”语法的某些扩展部分(C++、C#) 和/或您将在其中使用的语义您的代码(Katahdin,lua)。但我还没有听说过一种语言可以完全定义代码的外观。那么,是否已经存在某种语言具有覆盖所有语法和语法的功能?定义语义 ?
我想要做的示例基本上来自下面的 C# 代码:
foreach(Fruit fruit in Fruits)
{
if(fruit is Apple)
{
fruit.Price = fruit.Price/2;
}
}
我希望能够用我的完美语言编写上述代码,如下所示:
Check if any fruits are Macintosh apples and discount the price by 50%.
从编码员的角度来看这个“想象”的优点语言是:
- 发生的事情非常清楚(自我描述) - 毕竟它是简单的英语,即使孩子也会理解我的程序
- 隐藏我必须用 C# 编写的所有复杂性。但我为什么要关心学习这个 if 语句、算术运算符等,因为已经实现了。
我认为维护此程序的编码员的缺点是:
- 也许您会以与我不同的方式表达此程序,因此您可能无法获得所有 我在句子中表达的信息
- 程序可能非常冗长且难以调试,但如果可能的话,甚至接近上面这种类型的语法,也许更多的人会开始编程,对吧?我想那真是太棒了。我可以去上班,然后写一篇文章在 winform 上画一个正方形,如下所示:
创建一个名为 MyGreetingForm 的表单。画一个正方形,中间有 MyGreetingForm 的一边为 100 分。在方块中间用 Arial 字体写“Hello!单击此处继续”。
在上面的代码中,解析器基本上必须猜测我想使用 前一句中的未命名方块,我想编写这样一个智能解析器会很困难,但我想做的事情却很简单。
如果用户单击 MyGreetingForm 中间的方块,则显示 MyMainForm。
在上面的代码中,“基本上”编译器必须:1)生成一个事件处理程序 2)检查中间是否有任何方块表单,如果有 - 3)隐藏表单并显示另一个表单
这看起来很难做到,但在我看来并非不可能,至少近似于此(我个人可以生成一个解析器来执行 np 以上的 3 个步骤& 即使在 c# a.MyEvent=+handler;
中添加,它也必须以任何方式执行,所以我在这里没有看到问题)所以我想也许有人已经做了这样的事情?或者创建这样一种我看不到的“论文风格”编程语言是否存在一些实际的复杂性负担?我的意思是,如果解析器不是那么好,会发生什么更糟糕的情况? - 你的程序会崩溃,所以你必须重新措辞:)
Most languages allow to 'tweek' to certain extend parts of the syntax (C++,C#) and/or semantics that you will be using in your code (Katahdin, lua). But I have not heard of a language that can just completely define how your code will look like. So isn't there some language which already exists that has such capabilities to override all syntax & define semantics ?
Example of what I want to do is basically from the C# code below:
foreach(Fruit fruit in Fruits)
{
if(fruit is Apple)
{
fruit.Price = fruit.Price/2;
}
}
I want do be able to to write the above code in my perfect language like this:
Check if any fruits are Macintosh apples and discount the price by 50%.
The advantages that come to my mind looking from a coder's perspective in this "imaginary" language are:
- It's very clear what is going on (self descriptive) - it's plain English after all even kid would understand my program
- Hides all complexities which I have to write in C#. But why should I care to learn that
if statements, arithmetic operators etc since there are already implemented
The disadvantages that I see for a coder who will maintain this program are:
- Maybe you would express this program differently from me so you may not get all the
information that I've expressed in my sentence - Programs can be quite verbose and hard to debug but if possible to even proximate this type of syntax above maybe more people would start programming right? That would be amazing I think. I can go to work and just write an essay to draw a square on a winform like this:
Create a form called MyGreetingForm. Draw a square with in the middle of
MyGreetingFormwith a side of 100 points. In the middle of the square write "Hello! Click here to continue" in Arial font.
In the above code the parser must basically guess that I want to use
the unnamed square from the previous sentence, it'd be hard to write such a smart parser I guess, yet it's so simple what I want to do.
If the user clicks on square in the middle of MyGreetingForm show MyMainForm.
In the above code 'basically' the compiler must: 1)generate an event handler 2) check if there is any square in the middle of the form and if there is - 3) hide the form and show another form
It looks very hard to do but it doesn't look impossible IMO to me at least approximate this (I can personally generate a parser to perform the 3 steps above np & it's basically the same that it has to do any way when you add even in c# a.MyEvent=+handler;
so I don't see a problem here) so I'm thinking maybe somebody already did something like this ? Or is there some practical burden of complexity to create such a 'essay style' programming language which I can't see ? I mean what's the worse that can happen if the parser is not that good? - your program will crash so you have to re-word it:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看:
渗透秩序
简单英语程序员
代码示例:
Check out:
The Osmosian Order
of Plain English Programmers
Code Example:
一些互动小说设计师使用与英语极其接近的语言语法。这里有一些 Inform 7 代码,您可以在线玩:
我辅导了一门使用 Inform 7 的课程。其中一位导师的印象是作业是设计,而不是编写游戏。因此,他通过阅读来标记程序,但没有意识到它们是实际的程序。
Some Interactive fiction designers use a language syntax extremely close to the English language. Here's some Inform 7 code, which you can play online:
I tutored a course that used Inform 7. One of the tutors had the impression the assignment was to design, not write a game. So he marked the programs by reading them, without realising they were actual programs.
我不认为这将是一项容易的任务,也不认为它会让调试变得更容易。
您将如何处理这些问题?
带回COBOL或者你还记得“向西走”、“检查门”、“推门”吗、“开门”、“用钥匙开门”:)
编辑 - 您将如何强式输入此内容?
I don't think that this would be an easy task nor do I think it is going to make life easier for debugging
How would you deal with these issues?
Bring back COBOL or can you remember "Walk West", "Examine Door", "Push Door", "Open Door", "Use key on door" :)
edit - how would you strongly type this?
我编写了一个可扩展的英语到 Python 编译器,名为 EngScript,它将结构化英语转换为工作的 Python 代码。
这是 EngScript 代码的示例:
这是 EngScript 编译器生成的输出:
I have written an extensible English-to-Python compiler called EngScript, which converts structured English into working Python code.
This is an example of EngScript code:
This is the output that was generated by the EngScript compiler:
实时代码!
有一些“自然语言”、高级、类似英语的编程或脚本语言。可能所有这些都受到了最古老的 COBOL 的启发。我个人最喜欢这些语言是 LiveCode。 LiveCode 是 MetaCard 的后代,MetaCard 是 Apple 现已不复存在的 HyperCard 的 Linux 克隆版本,它使用一种名为 HyperTalk 的类似英语的脚本语言,该语言受到 SmallTalk 的启发,进而启发了 JavaScript(以及整个万维网)。 HyperTalk 是另一种类似英语的脚本语言 AppleScript(以及后来的 AppleScriptObjC)的基础,该语言至今仍随 macOS 一起提供。 LiveCode 使用一种称为 LiveCodeScript 或 LCS 的语言,与已存在多年的其他 HyperCard 克隆(SuperCard、Adobe 的 Lingo/Flash ActionScript、Open Xion、Oracle 的 Toolbook 等)一样,它的核心与 HyperTalk 非常相似,通常称为 X-Talk 语言。 LiveCode 有几个优点;它仍然在生产中,它有双重许可证(开源和商业版本),引擎是跨平台的(Mac、Win、Linux、HTML5、iOS、Android 和服务器版本),并且像 HyperCard 一样也是一个 GUI 工具包,并且它是可扩展的。 LiveCode 团队目前正在开发一种新的低级编程语言,称为 LiveCode Builder(LCB)。 LCB 也是类似英语的,虽然 LCB 的可读性比 LCS 差一些,但它的目标是具有与 C++、Objective C 等较低级语言相同的功能,允许使用代码库扩展 LiveCode 平台以及其他编程语言库生成的框架,并最终允许 LiveCode IDE 用它自己的语言编写。
LiveCode!
There are a few "natural language", high-level, English-like programming or scripting languages. Probably all of them were inspired by the oldest, COBOL. My personal favorite of these languages is LiveCode. LiveCode is a decendent of MetaCard, a Linux clone of Apple’s now defunct HyperCard that used an English-like scripting language called HyperTalk, which was inspired by SmallTalk, and in turn inspired JavaScript (as well as the entire World-Wide-Web). HyperTalk was the basis for another English-Like scripting language called AppleScript (and later AppleScriptObjC), which still comes with macOS to this very day. LiveCode uses a language called LiveCodeScript, or LCS which, like other HyperCard clones and that have existed over the years (SuperCard, Adobe’s Lingo/Flash ActionScript, Open Xion, Oracle’s Toolbook, etc.), is very similar to HyperTalk at it’s core, often referred to as an X-Talk language. LiveCode has several advantages; it’s very much still in production, it has a dual license (open source and commercial versions), the engine is cross-platform (Mac, Win, Linux, HTML5, iOS, Android, and a server version), and like HyperCard it is also a GUI toolkit and it is extensible. The LiveCode team is currently working on new a lower-level programming language called LiveCode Builder, or LCB. LCB is also an English-like, although LCB is a bit less readable than LCS, it has a goals of having capabilities on par with lower-level languages like C++, Objective C, etc., allowing for extending the LiveCode platform with code libraries and frameworks produced by other programming language libraries, and ultimately allowing for the LiveCode IDE to be written in it’s own language.
尝试使用名为“Google”的编程语言 - 它具有自然的英语界面,并且您的代码片段会返回您建议的所有答案。有趣的是,在您提出这个问题六分钟后,这个页面就成为该查询的#1:
使用 Google API,我认为您已经具备了自然英语编程语言的基础。
Try using the programming language called 'Google' - it has a natural English interface and your code fragment throws back all the answers you are suggesting. Interestingly just six minutes after you asked this question, this very page is #1 for the query:
Use the Google API and I think you have the basis of a natural English programming language.