Natural language programming for Chinese: L scripting language
L Why design a new programming language?
In the process of programming I always think, computer programming this thing takes so long time of study process, can make this job easier? There are so many programming languages, each with its own different grammatical rules and limitations, can it be possible to implement a programming language.
If we have a simple syntax, and also support Chinese programming language is not able to make programming easier this also can make programming more consistent with the general language expression habits.
To do this, I started to implement such a scripting language from scratch, because the scripting language is easier to implement than a statically compiled language like c,c++. This scripting language is named L language
L L scripting language is designed to support Chinese natural language programming
That is to say L scripting language wants to be more close to the language we usually speak and write articles, rather than computer-literate machine language, and it is built on the basis of Chinese, do not need to master English can be used
L L scripting language syntax for actions: objects, Parameters
Let's take a look at the meaning of a C language code and its textual expression
|
C Language Code |
Explain |
|
|
int num;
num = 10;
num=num+1;
printf ("%d", num); |
Defines an integer num;
Assigns a value of 10 to the integer num;
The value of NUM plus 1 is re-assigned to NUM;
Print output num; |
|
Why do we need to write the code on the left to let the computer run, why not write the description on the right to get the computer to work? Most computer software developers answer: Because the natural language on the right is not easy for computers to recognize.
But is that really the case?
I have been thinking, is it possible to allow computers to recognize natural language?
Let's observe that in computer programming, each line of code is a command, and the core of each command is a verb, and other information can be interpreted as a parameter.
In natural language, there are not only verbs, but also nouns, adjectives, quantifiers and so on.
All they have in common is a core verb.
So if we re-adjust the format of the natural language we write, is it easy for the computer to recognize it?
To verify the idea, I made a hypothesis that the programming language has only a command + parameter format
, such a grammar, that is, close to natural language, but also easy to make computer recognition
L in the L scripting language, only actions, objects, and strings
We use the L scripting language to implement the above-mentioned C language code
|
L Script language Code 1 |
L Script Language Code 2 |
|
Definitions: integers, num,10
Calculation: Formula, exp1,num=num+1
Display: num |
Definitions: integers, num,10
Calculation: num=num+1
Display: num |
Look, is not very simple, I think, do not need to have programming experience can read the above this section of the Code of the L scripting language
Moreover, this simple grammatical structure makes it easy for the computer to recognize
Is programming simple?
Of course, the preceding piece of code does not have much practical significance, and our L scripting language needs to be more powerful.
To do this, we need to define more language features.
L L scripting language Syntax description
In the code of the L scripting language, a line of code is a text string, a text string may be a command, it may be an object, and possibly the original text string
It seems a little confusing, but there are clear rules.
In one line of code, the beginning of a line must be an action: an object
So, the starting position of the line to ":" is the action name, ":" followed by the object name
Object names can be followed by arguments, or not with
If followed by a parameter, then use "," to separate the
Delimiter: Or: Preceded by a command or action, followed by an object
A separator, or, used to separate the parameter areas
Parameters can be either a string or an object
That's easy enough! There are not too many identifier elements in the scripting language, and there are only two separators, commas and colons, so the syntax is easy to remember.
What is an object? For a friend who has no contact with programming, this need to explain that the object is not "Object" object is translated from English object, the English Dictionary of the meaning of object is interpreted as: objects, objects. In fact, this is easy to understand, that is, we do not consider each of the calculation instructions when thinking about the problem of programming, but consider what action on an object.
Scripting language is also an object-oriented programming language!
L Other language features
There are some global objects built into the scripting language, but also allow the user to define their own instance of the object, and the interpretation engine of the scripting language will identify what the type of the parameter is.
We can also interpret actions as functions
A "Result" object is built into the scripting language, and the result object holds the result of the previous statement, which can be any object, but it is easily lost, and if we want to preserve the objects saved in the results, you can do so by using the capture command
The support for conditional statements is built into the scripting language, where the conditional statement is actually an action called the "if" statement
The script language also has built-in support for looping statements, where loop statements are also an action called "when" statements
Well, the light says no fake bashi. Let's look at the features of the L scripting language.
L L scripting language currently supports:
Support String, string comparison support substring assignment find substring
Support files, support file Open, read, write, get file size and other operations
Supports numeric operations, including arithmetic, logic, bitwise, and shift operations for integers and floating-point numbers
Support "Table", the table is actually borrowed from the Lua language concept, you can use it to implement a dynamic array, the table element can be any object, can be a function, of course, it can be a table, the size of the table is variable
Regular expressions are supported and strings can be matched by a built-in regular expression
Support functions, functions can have any number of arguments, parameters can be arbitrary objects, of course, it can be a function
Supports "Memory" objects to support memory comparisons
debugger, support single step execution, support breakpoint, support object view, support source Edit
Support for invoking operating system C language API
Support Library and library import mechanism, through the import library mechanism of L scripting language, can realize the extension of language itself, because can invoke the C language API provided by the operating system, so it can be implemented by users to implement various enhanced functions
Support network communication, support TCP,UDP protocol, can use the L scripting language to develop a simple network program, of course, can be a server, can also be a client
Support time, date, time comparison
Support Directory Management
Support for simple interactive input boxes
Support for generation of random numbers
Support for Registry editing
Support for custom classes, and instantiation of classes
Wow! It seems that our L scripting language is really capable, there are so many features!
L Let's see what I can do with the scripting language.
Support for Strings:
Because the L scripting language: And, is the delimiter so if you want to use it in a string: And, you need to enclose the string with "".
But the action and object names cannot be enclosed in "".
Why? This is simply because it's easier to implement, so it's a rule
Support for functions:
For a function to be defined (declared) before it can be called
The definition function has a clear format:
The function name can be any string, the formal parameter can be any string, the number of formal parameters can be any number of
function body can be any valid L script language script statement
function body starts with: function, format of function name
To end: The function, the format of the function name ends
Function arguments that need to be passed in to match the number defined when the function is called
The argument to the function must be an object
A function's arguments are objects, and the function itself is an object, so the function's arguments can also be functions
Does this make recursive calls possible?
In the L scripting language, the parameters of a function are passed by reference, that is, the formal parameter inside the function is just an alias of the actual parameter, and the interpreter engine is responsible for de-alias mapping at the end of the function call and exits the function body, so the modification of the formal parameter inside the function is also reflected on the actual parameter. Therefore, you need to consider this when designing the functions of the L scripting language to avoid accidentally modifying the values of the actual parameters.
L Example Program
The introduction of so much, as if still not clear, in the end how to program it?
Do not worry, I have implemented an L scripting language interpretation engine and a simple debugger, as long as we have a "notepad" or any kind of text editing software, you can implement the L scripting language programming, eh! The interpreter and debugger are developed in the C + + language.
Let's take a look at examples of programming and explain the syntax structure of the program as well, so it's easier to understand
In the L scripting language, the command format for mathematical operations is:
A calculation: The name of a formula, the content of a formula
Or
Calculation: The contents of a formula
Lines beginning with # are comments, and all comments are ignored by the interpreter
The beginning of the code line starts with #scp
The code has an. scp extension, which can be any extension, but it's just a convention.
The code file must be saved in an ANSI-encoded format, yes, or to make it easier
Example 1: Numerical operations
|
#scp
#L脚本语言中支持的算术运算符号 +-*/%
#L脚本语言中支持的逻辑运算符号 < <= = = > >=! !=
#L脚本语言支持赋值运算符 = + = = *=/=%=
#数值也是对象, in the L scripting language, values are integers or floating-point numbers
#算式是包含数学计算表达式及其计算结果的对象
#不支持--+ + This self-increment operation
#目前只支持简单的算式
#对于运算符的优先级没有进行深入处理
#所以现在书写的表达式必须加括号
#很绕吗? Actually, it's not complicated.
Definition: integer, I am an integer, 1001
Definition: integer, I am another integer, 1
Definition: integer, I am the result of the calculation, 0
#逻辑 "Non" operation
Calculation: formula, formula17,! I'm the result of calculation
Display: Formula17
#逻辑 "Not Equal to"
Calculation: Formula, FORMULA16, I am the result of the calculation! = I am an integer
Display: Formula16
#位运算 "Bitwise AND"
Calculation: formula, equation 1, I am an integer & I am another integer
Display: Calculation 1
#位运算 "Bitwise OR"
Calculation: formula, equation 2, I am an integer | I'm another integer.
Display: Calculation 2
#位运算 "Bitwise XOR"
Calculation: formula, Equation 3, I am an integer ^ i am another integer
Display: Calculation 3
#逻辑 "and"
Calculation: formula, equation 4, I am the result of calculation && I am another integer
Display: Calculation 4
#逻辑 "or"
Calculation: calculation, equation 5, I am the result of computation | | I'm another integer.
Display: Calculation 5
#按位左移
Calculation: formula, equation 6, I am an integer << I am another integer
Display: Calculation 6
#按位右移
Calculation: formula, equation 7, I am an integer >> I am another integer
Display: Calculation 7
#赋值
Calculation: Computation, FORMULA10, I am the result = I am an integer
Display: FORMULA10
#逻辑 "equals"
Calculations: calculation, FORMULA11, I am the result = = I am an integer
Display: FORMULA11
# "Less than"
Calculation: Formula, FORMULA12, I am the result of calculation < I am an integer
Display: Formula12
# "Less than equals"
Calculation: Formula, Formula13, I am the result of the calculation <= I am an integer
Display: Formula13
Greater than
Calculation: Formula, FORMULA14, I am the result of calculation > I am an integer
Display: FORMULA14
"Greater than equals"
Calculation: Formula, FORMULA15, I am the result of the calculation >= I am an integer
Display: FORMULA15
#复杂一点的整数四则运算
Calculations: calculation, FORMULA9, I am the result = (I am an integer * (-2) * (-2) -1-9+6)/100
Display: FORMULA9
Definitions: integers, number1,8
Definitions: integers, number2,9
Definitions: integers, number3,100
#各种加减乘除运算
Calculation: Formula, formula1,number1* (NUMBER2+NUMBER3)-100
Calculation: Formula, FORMULA2, (number1* (Number2+number3))-(100*102)
#计算: Formula, Formula2,100*number4
Calculation: Formula, Formula3,number1+number2*number1
Calculation: Formula, formula4,number1-number2-365
Calculation: Formula, Formula5,number1*number2
Calculation: Formula, Formula6,number1/number2
Calculation: Formula, Formula7,number1%number2
Calculation: Formula, Formula8,number1+number2*number2-number1*number3
#一些简单的函数以及函数调用
Definition: function, print and subtract one, parameter 1
Start: function, print and subtract one
Display: Parameter 1
Calculation: Equation, exp1, parameter 1 = parameter 1-1
End: function, print and subtract one
Definition: Integer, change factor, 100
When: Change Factor >0, call: function, print and subtract one, change factor
Definitions: integers, rid,100
Definition: function, the circumference of the circle, radius
Start: function to find the perimeter of a circle
Calculation: formula, ret,2*3.14* radius
Display: RET
End: function to find the circumference of a circle
Show: "Now the circumference of the circle"
Call: function, round perimeter, RID
Definitions: integers, rid2,10
Definition: function, area of circle, radius
Start: function to find the area of a circle
Calculation: Formula, ret,3.14* (RADIUS * radius)
Display: RET
End: function to find the area of a circle
Show: "Now the area of the circle"
Call: function, Rid2 area of the circle,
Definition: floating point, float1,1.5
Definition: floating point, float2,155
Calculation: Formula, FORMULA111,FLOAT1*FLOAT2
Display: formula111
Definition: function, exponentiation, NUM1
Start: function, exponentiation
Calculation: Formula, RET,NUM1*NUM1
Display: RET
End: function, exponentiation
Definition: function, square, NUM1
Start: function, square
Calculation: Formula, RET,NUM1*NUM1
Display: RET
End: function, square
Definition: function, three times, NUM1
Start: function, three-time Square
Calculation: Formula, RET,NUM1*NUM1*NUM1
Display: RET
End: function, three-time Square
Definition: function, remainder, num1,num2
Start: function, find remainder
Calculation: Formula, ret,num1%num2
Display: RET
End: function, seek remainder
#定义: Value, anynum1,65535
#定义: Value, anynum2,3.14159
Display: Number1
Display: Number2
Display: FLOAT1
Display: FLOAT2
Display: Formula1
Display: FORMULA2
Display: FORMULA3
Display: FORMULA4
Display: FORMULA5
Display: FORMULA6
Display: Formula7
Display: Formula8
Invoke: Function, exponentiation, number1
Call: function, seek remainder, Number2,number1
Call: function, three-time Square, number1
#一些赋值运算
Calculation: I am an integer + = I am another integer
Show: I am an integer
Calculation: I am an integer-= I am another integer
Show: I am an integer
Calculation: I am an integer *= I am another integer
Show: I am an integer
Calculation: I am an integer/= I am another integer
Show: I am an integer
Calculation: I am an integer%= I am another integer
Show: I am an integer
|
In the previous example, I introduced how to use the L scripting language programming to achieve numerical operations, as well as the writing of simple functions
Now let's look at how to use the L scripting language to implement string processing and string operations
Example 2: String processing
|
#scp
Definition: function, function1, parameter 1, parameter 2
Start: function, function1
Popup: Message box, parameter 1, parameter 2
End: function, function1
Definition: String, str1, "This is a string"
Definition: String, str2, "caption"
Definition: String, STR3, "This is another string"
Fetch substring: str4,str1,1,3;
Display: STR4
Call: function, FUNCTION1,STR1,STR2
Call: function, FUNCTION1,STR3,STR2
Connection: String, STR1,STR3
Call: function, FUNCTION1,STR1,STR2
Definition: function, function2
Start: function, function2
Display: HelloWorld
Popup: Message box, HelloWorld
End: function, function2
Call: function, function2
Definition: function, Function3, parameter 1
Start: function, Function3
Definition: String, STR4, "function Call function"
Definition: String, STR5, "message"
Call: function, Parameter 1,STR4,STR5
End: function, Function3
Call: function, Function3,function1
Definition: function, Function4, parameter 1, parameter 2
Start: function, Function4
Open: File, file1, parameter 1
Write: File, file1, Parameter 2
End: function, Function4
Definition: string, filename, "C:\222.txt"
Call: function, FUNCTION4,FILENAME,STR1
Display: filename
Empty: string, filename
Display: filename
|
Well, in the previous example, we know how to initialize string and various string manipulation functions, and we also implemented the built-in function to make the operating system pop up a message window, is it very simple?
Well, the operation of the string is not just a connection, a substring, a flush, it can be found and replaced, and the L script has built-in support for regular expressions, and we can find matching strings within the string through regular expressions
Example 3: String lookup substitution and use of regular expressions
|
#scp
Definition: Regular expression, REGEXP1, "\d\d\d"
Definition: String, STR2, "ABC"
Definition: String, str1,123
Matching: Regular expressions, REGEXP1,STR1
Capture: RESULT1
Display: RESULT1
If: result1== match, popup: message box, regular expression matches to string, hint
If: result1== does not match, popup: message box, regular expression does not match to string, hint
Matching: Regular expressions, REGEXP1,STR2
Capture: Result2
Display: RESULT2
If: result2== match, popup: message box, regular expression matches to string, hint
If: result2== does not match, popup: message box, regular expression does not match to string, hint
Definition: string, string1, "123456ABCDEFG"
Definition: String, string2, "ABC"
Find: Strings, string1,string2
Capture: RESULT3
Display: RESULT3
If: result3== found, popup: Message box, find string, prompt
If: result3== not found, popup: message box, string not found, hint
Replace: String, String1,string2, "XYZ"
Capture: Result4
Display: RESULT4
Display: string, string1
If: result4== found, popup: message box, find and replace string, hint
If: result4== not found, popup: message box, string not found, hint
|
In the L scripting language, the result of each line of code execution is assigned to a "result" object, the contents of which can be any object, but it is easy to lose, if we do not save it, then the next line of code will be the result of the previous line to replace, so, When we need to preserve the results of the code execution, we can save the results of the code execution with a "capture" statement
In the example above, there is also a "if" statement with the syntax of the "if" statement:
If: conditional expression, the code that runs when the conditional expression is true
It's easy to understand.
But we don't have the if else structure like the C language, so what if we want to write the code that should execute when the condition is true and the condition is false? Just write one more "if" statement. Let the condition of the second "if" statement be the opposite of the first one.
I know this looks a little silly, but in order to make it simple, the preliminary design.
Well, after these few examples, we should be able to read the other code of the L scripting language, because the code is in Chinese, and the syntax is consistent, so I believe it is not difficult to understand.
In addition to numeric operations and string processing, can we also access files stored on disk?
Example 4: File processing and memory processing
|
#scp
Open: File, File1,c:\1.txt
displaying: Files, file1
Copy: File, C:\1.txt,c:\456.txt
Copy: File, C:\456.txt,c:\789.txt
Mobile: Files, C:\789.txt,c:\777.txt
Delete: File, C:\777.txt
Definitions: integers, size1,0
Take size: File, file1,size1
Display: Size1
Application: Memory, mem1,1024
Application: Memory, mem2,1024
Replication: Memory, MEM1,MEM2
Write: Memory, Mem1,number1
Definition: String, str1, this is a test string
Write: File, file1,mem1
Write: File, file1,str1
Release: Memory, MEM1
Definitions: integers, size2,0
Take size: Memory, Mem2,size2
Display: Size2
Release: Memory, MEM2 |
Well! We've been able to manipulate files and applications and use memory, but is it a bit tedious to work on? Can we have some interaction with the computer?
Example 5: User interaction input----a simple calculator
|
#scp
#我们用scp来开发一个计算器吧!
Definition: string, string1
Definition: function, func1
Start: function, func1
Wait: User input, string1
Calculation: Formula, formula1,string1
Display: Formula1
End: function, func1
When: 1, call: function, func1
|
Ha ha! The original implementation of a calculator just a few lines of code Ah!
If you can only calculate some numeric expressions, handle some simple strings, and do simple file access it seems that it is not very interesting ah! Would it be more interesting to communicate with others through the computer network?
Of course, L scripting language can also implement basic network communication
Now let's make our own network communication!
Example 6:TCP echo Server and TCP client
|
#scp
#这是一个TCP回显服务器的例子
#它不断监听一个网络连接
#如果有网络连接到来
#就通过新建立的网络连接接收一个字符串, print and send back to client
Definition: string, string1, "Hello"
Definition: String, string2, ""
Definition: Address, addr2,127.0.0.1,27015
Definition: integer, number of bytes, 0
Definition: Network connection, CONN2,TCP
Definition: function, routine
Start: function, routine
Listening: CONN2,ADDR2
Receive: Conn2,string2
Fetch size: String, string2, number of bytes
If: Number of bytes >0, display: string2
If: Number of bytes >0, send: Conn2,string2
End: function, routine
When: 1, call: function, routine
|
|
#scp
#这是一个通过TCP协议向服务器发送字符串的例子
#它先发起对一个网络上的服务器的连接
#如果连接建立, send a string to the server
#如果服务器返回消息
#就将消息打印输出
#这个过程重复100次
Definition: string, string1, "Hello"
Definition: String, string2, ""
Definition: Address, addr1,127.0.0.1,27015
Definition: integer, number of bytes, 0
Definitions: integers, count,100
Definition: Network connection, CONN1,TCP
Connection: CONN1,ADDR1
Definition: function, function1
Start: function, function1
Send: Conn1,string1
Receive: Conn1,string2
Fetch size: String, string2, number of bytes
If: Number of bytes >0, display: string2
Calculation: Formula, exp1,count=count-1
End: function, function1
When: count>0, call: function, function1
|
The original network communication is so simple AH! It seems that it is not too difficult for us to implement a simple chat tool ourselves! Only need to combine user input can realize real-time chat! You can try it yourself!
I have a lot of flaws in scripting language from grammar design to implementation, and the implementation of these features is only a short time, I hope the L scripting language can help those who want to learn programming, and programmers can use it to do some simple repetitive work.
The scripting language supports invoking the operating system API functions of the C language interface, and supports the import mechanism of libraries and libraries, so the L scripting language is extensible, and it is hoped that interested people can participate in the development of the L scripting language Library. Because I want to let the zero-based friends to learn the L script, and the development of the library needs some C language programming and operating system knowledge, here I do not go into the L Script library in-depth introduction.
I hope that interested friends can give me some suggestions about the errors and flaws that may exist in the L scripting engine, but if there is a better grammatical structure design and function point can also tell me.
Interested friends can download the L Script language interpretation engine and debugger installation package to try, the installation package contains the command line interpreter engine and a simple debugger, a grammar manual and some simple script examples.
CSDN Resources http://download.csdn.net/detail/itmes/8656133
Hope everyone programming Happy!
Natural language programming for Chinese