开发者

Is there a programming language with semantics close to English?

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:

  1. It's very clear what is going on (self descriptive) - it's plain English after all even kid would understand my program
  2. Hides all complexities which I have to write in C#. But why should I care to learn that if statements, arithmetic开发者_StackOverflow中文版 operators etc since there are already implemented

The disadvantages that I see for a coder who will maintain this program are:

  1. Maybe you would express this program differently from me so you may not get all the information that I've expressed in my sentence
  2. 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:)


Check out:

The Osmosian Order of Plain English Programmers

Code Example:

The background is a picture.

A button has a box and a name.

To clear the status:
  Clear the status' string.
  Show everything.

To create the background:
  Draw the screen's box with the white color.
  Loop.
  Pick a spot anywhere in the screen's box.
  Pick a color between the lightest gray color and the white color.
  Dab the color on the spot.
  If a counter is past 80000, break.
  If the counter is evenly divisible by 1000, refresh the screen.
  Repeat.
  Extract the background given the screen's box. \or Create the background from the screen. Or something.


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:

The foyer is a room.

The apple is in the foyer. It is edible. The description is "This is a ripe, 
green granny smith apple."

The apple core is a thing. The description is "This apple core all that is 
left of that granny smith apple you just consumed."

After eating the apple:  
  now the apple core is in the player;
  say "You gobble down the apple careful not to eat any of those cyanide-
  laced seeds you heard about."

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.


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?

  • spelling mistakes
  • different dialects in different parts of world
  • different dialects in the same part of the world
  • synonyms
  • which part of sentence do you parse first?
  • tear (rip) and tear (from eye) both words spellings are the same but mean two different things.

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?


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:

print{create a string from the file called "README.txt"}
print{save the string "Woohoo!" to a file called "ExampleText.txt"}
print{the first 3 letters of "EngScript"}

This is the output that was generated by the EngScript compiler:

print(pythonFunctions.stringFromTextFile("README.txt"))
print(pythonFunctions.writeStringToFile("ExampleText.txt", "Woohoo!"))
print("EngScript"[0:(3 - 1)+1])


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.


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:

Check if any fruits are Macintosh apples and discount the price by 50%

Use the Google API and I think you have the basis of a natural English programming language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜