Luban a component-oriented programming language

xiaoxiao2021-03-05  30

Luban is a component-oriented programming language. Luban is also a integrated language or a scripting language. There are many buildings in the Luban language to build a wooden, making a furniture, and the house is very similar. So the author named Luban to highlight Luban The architectural technology and the interactive integrity of the Luban language characteristics. It is also a reflection and respect for our own cultural origins.

Some friends may say that now the scripting language has been a lot, do we really need another scripting language? The author of Luban does very highly praise the scripting language. Because it is easy to get started, non-professionals can learn. However, from the practice of the practice There is a very weak point in the existing scripting language, there is no suitable part model (Component Model). It is like doing things on the sand, it is easy to start, it can be done. There may be a friend point out, With object-oriented scripting language, you can organize code. For this, the author considers that the object model is not a scripting language in the scripting language. Because the object model is difficult to grasp for professionals, it is completely exceeded general scripting language users Master the scope. There is no advantage in writing object types with scripting language, but more formal language icons C and Java.

From this point, the scripting language is to beyond the circle of writing a small scriptor, requiring a component model suitable for scripting locale. There is indeed a blank zone in the programming language spectrum. The invent of Luban is focusing on filling this vacancy.

Luban is first is a scripting language, easy to learn, no need to compile. And Luban is a better scripting language because its syntax is more concise, similar to C / java. General programming users can start with new things. Luban write procedures.

Luban Transcendence General Script Language is the Luban Parts Model. The Luban language defines the part as an object, similar to the concept of Java Bean. The definition, classification, storage, and combination of components are the core characteristics of the Luban language. There is a little bit And the object-oriented language is different. If you just write a little footprint with Luban, you don't have to know what is the Luban part. Luban minimizes the requirements for new users.

It is also worth mentioning that the component combination function of Luban. Component combination is a new programming method. The traditional programming is the order of execution of the code, and the component combination defines a data dependency between one module internal part. It is like a wooden. The portfolio of Luban introduced component is because it is simply straightforward. The best proves is Excel. Almost everyone will use Excel. Writing Excel is actually the part combination. Luban is the first official Software programming language for support component combination.

Luban language is a language-centered language. Hope is more people to do things with computer, and can share their work results. Luban is a scripting language, but the author wants it to go further. So It is also called it integrates the language.

By the way, the introduction of each chapter of this book is to pick it with the author. Some is the lyrics, some are some people who are free to say. It is not directly related to the Luban technical details. These things are difficult to translate, I am not bad. If you want to see, you can watch it, don't want to see it.

Script Luban

In The Beginner's Mind There Many Possibilities,

In The Expert's Mind There Are Few. - Shunryu Suzuki, ZEN MASTER

This chapter tells the basic Luban order to perform statement constructs, including variables, constants, expressions, and statements. After reading this chapter, you can write a general script program with Luban. Have a friend of C / Java, use five minutes You can read this chapter. Because Luban's statements and expressions and C / Java are basically similar, it is just the Luban expansion and different parts. The part is also very simple, see clearly.

2.1 First Luan Process

The most effective way to learn a new language is too much to look at the program example. Good language always start with a "world, hello" program example. Luban is no exception. Below is the "world, hello" program of Luban .

Std :: Println (Obj = "Hello, World");

Tap the above code into a file called "HelloWorld.lbn". Then on your operating system command to knock the following command.

MyComputer> Luban HelloWorld.lbn

Hello, World!

This Luban has a "HelloWorld" program that is C. inside the code is to call a Luban component called "std :: println". When calling the string "Hello, World" to a "Obj" Enter an attribute. The execution result of the program is printed on the terminal "Hello, World" string. 2.2 Luban script basic structure

Luban's script is composed of a series of Luban statements or expressions. These statements and expressions are separated by semicolons. The wraps and blank characters will be ignored. Such structures and C / Java are the same. Here is A simple Luban script program.

X = 1;

Y = 2;

Z = x y;

Std :: Println (Obj = z);

Enter the above program into a file called "OnePlustWo.lbn" and then in the command line into the following command:

MyComputer> Luban Oneplustwo.lbn

3

From the above program, you can see the probably look of the Luban script.

2.3 constants, variables and expressions

Luban's expression and C / Java are basically the basis. It consists of constants, variables, and operators. The basic construction of the expression is listed below.

The constant itself is an expression. The constant of Luban has the following.

3.1415; 2; "Hello"; 'a'; '/ n'; true; false;

Variables are also expressions. The Luban variable is the same as the C language, and the following is a legal variable name.

VARX; _VARX_; V123;

Data Type Object Construction and Parts Construction.

[1, 2, 3, X, Y, Z, A B]; // Array Construction

{"One": 1, "Two": 2}; // Dictionary construction

{"One", "Two"}; // Collection

Double ("3.1415"); // Double precision floating point construction

Std :: File ("mydatafile", 'r'); // Open file

MyNamespace :: mycomponentx (); // Component construction

MyNamespace :: MyComponetx (Prp1 = 100); // Part Type Call

Container type members Access, object member function call, component property access, component call.

MyMap ["one"]; // Dictionary find

X [1] = y [2]; // Dictionary or array member reading and assignment

MyMap.Remove ("one"); // Object member function call

Linevector = allines.split ('/ n'); // Object member function call, decomposition string

Mycalculator (Input1 = 1.1, INPUT2 = 2.2); // Part object call

Single operator operation,

i; --i; i ; I -; // Operation change the value of the variable I

Y = -x; // variable X value constant, the calculation result gives Y

Common arithmetic class operation, addition and reduction and removal

1 2; 10.0 * 3.0; 50% 3; 50/3; "Hello," "World!";

Data type check operation

Objx isa string; / / Check Objx Word String Type

TypeOf (objx) == String; / / Check if Objx is string type

Equal relationship and sequential relationship check

X == Y; X! = Y; X> Y; x = y; x <= y;

Logical expression

True or false; x and y; not true; not a and not b;

Trimed condition expression

X> y? x: y;

Assignment expression

X = 1.23; x = 1; x [index] = 2; x.Property = 3; x * = 2; x / = 3; x = y = z;

Some constructs in the above expressions include type check and component call expressions will also be described in later chapters. 2.4 Luban order executive statement

Lucan's order executing statements and C / C / Java are basically the basics. Just add several Luban own statements. The following is listed in all Luban order executing statements.

Assigning statements: Assigning statements are also expressions, here again as statements.

X = 1; x = 1; x [index] = 2; x.property = 3; x * = 2; x / = 3; x = y = z = 1;

Conditional statements

IF (a> b)

Result = a;

Conditions, otherwise statement

IF (a> b)

Result = a;

Else

Result = B;

Condition cycle statement

While (x <100) x;

Step cycle statement

FOR (i = 0; i <100; i)

Std :: console (). WriteLine (VEC [i]);

For (;;); // dead cycle

Enumerate loop statement

Foreach (Key, Value In themap)

Std :: console (). WriteLine (key, '', value);

Jump out the loop statement

For (i = 0;; i )

IF (i> 100) Break;

Continue loop statement, jump back to the starting point

For (i = 0;; i )

IF (i <= 100)

CONTINUE;

Else

Break;

Blank statement

;

End the statement, terminate a script or component

Finish;

Waiting statement: Waiting for all start-up threads

Wait;

Specific statement: Waiting and wait for variables to end

Waitfor X, Y, Z;

Cancellation statement: Undo all started threads and wait for them to end

Cancel;

The details related to the above threads are described later.

The phrase. Take one or more statements with the curly brackets, be a group statement.

{X = 1; y = 2; z = x y;}

IF (z == 3) {z = z-3; x = 0; y = 0;}

Multiple properties set statements, set multiple attribute values ​​for a part. It is indeed a specific detail to define a chapter.

XComponent. (Prp1 = 1, Prp2 = 2, Prp3 = 3);

Note statement. LUBAN's annotation statement is the same as C / Java

// one comment

/ * Another comment * /

2.5 Luban Data Type

Luban language provides a range of most common data types to users. These types include integers, double precision floating point numbers, strings, logical types, character types, arrays, dictionaries, collection. Here, you will introduce them one by one.

2.5.1 integer and double precision floating point

Integers and double precision floating point numbers are two different data types. But they can be freely mixed. The calculation rules and C language are the same. Integer type keywords are int, floating point type keyword is Double.

10/3; / / The result is an integer 3

10.0 / 3.0; / / The result is a floating point number 3 3.333333 ...

10 / 3.0; / / The result is a floating point number 3 3.33333 ... floating point number mixed integer, result is a floating point number

0 == 0.0; / / The result is TRUE, floating point number and integer can be mixed

2> 0.1; // The result is TRUE, floating point number and integer can be mixed comparison

X = 1.23456789;

y = x.format (". 4"); // Transform the floating point number into a string according to the specified format, keep 4 digits after the decimal point

/ / The result is "1.2346"

z = int (x); // z = 1, convert the floating point number into an integer

XString = "1.23456e7"; xdouble = double (xstring); // x = 1.23456x10 ^ 7 converts strings into floating point numbers

2.5.2 Logical Type

The logical type is simple, it can only be true or false. The keywords for the logical type are Bool, True, False. Logical types can be used for logical expressions. Logical expression operators have "or", ",", " Non-"three, corresponding keywords or, and, not. Conditions of Luban and Conditional expressions in loop statements must be logical types. Otherwise, Luban will terminate the current components. This is different from C language. C implicit integer types into logical types, while Luban does not do implies conversion. From integers to logical types, there is a clear code. The following is examples.

Truth = true or false; / / result is true

Truth2 = BOOL (1); // Truth2 = TRUE

Truth3 = BOOL (13); // Truth3 = TRUE

fiction = bool (0); // fiction = false

Truth4 = truth and truth2 and truth3 or fiction; // Truth4 = TRUE

Users who are familiar with C language can still use C-style logical operators, with && ||! Instead of and, or, not.

2.5.3 Character Type

The character type is equivalent to the single-byte character in the C language. Here are some type of character type operation example. The character type keyword is char.

For (OneChar = 'A'; Onechar <= 'Z'; Onechar)

Std :: Print (obj = onechar);

Running the above Ruan program is printed on the screen on the screen for twenty-six letters of the AbcDefghijz.

NineTyseven = INT ('a'); // nineTyseven = 97, character converted to ASCII integer

Lettera = char (97); // lettera = 'a', integer ASCII converts to characters

2.5.4 String Type

String Type Keywords are string. The following is a relatively complete example.

s = "Hello"; c = s [1]; // c = 'e' left a second character

s = "hello"; s [0] = 'h'; // s becomes "Hello"

S = "Hello"; Foreach (c in s) std :: console (). Write (c); // Print Hello on the screen

s = "Hello" * 2; // Result = "Hellohello"

s = - "Hello"; // Result = "Olleh"

s = "Hello" "world"; // Result = "HelloWorld"

// Below Are Me MEMBER FUNCTION EXAMPLES

//

Split! Similar to the most common SPLIT function of Perl

s = "Hello World"; Words = S.Split (); // Decompose the string into an array, space is division

// Words = ["Hello", "World"]

// Remove the head of the head

S = "Hello";

S.trimfront (); // Go to the opening blank S = "Hello"

S = "Hello / N / T";

S.trimBack (); // go to the end of white S = "Hello"

// Find subfragings

S = "Hello";

Index = S.Find ("Hell"); // Index = 0, "Hello" begins "Hell" index2 = S.Find ("Hell", 2); // index = null, Find failed,

// From the left of "Hello", there is no "hell" string from the third character.

Index3 = S.Find ("Heaven"); // Index3 = NULL Find failed

Truth = S.Contains ("Hell"); // Truth = true "Hello" contains "hell"

// Read the subster

Sub = s.substr (2, 3); // Sub = "ll" sub-string from serial number 2 to 3

// change the string content

S = "Hello";

S.Replace ("Hell", "Heaven"); // s = "Heaveno" replaced "Hell" in the string to "Heaven"

s.Lower (); // s = "Heaveno" becomes lower

S.upper (); // s = "Heaveno" gets larger

S.Remove (6); // s = "heaven", delete serial number 6 characters 'o'

S.Remove ("a"); // s = "heven"; delete all characters 'a'

S.insert (1, "a"); // s = "Heaven"; in the serial number 1 Insert Character 'A'

S.clear (); // s = "" Clear all content

2.5.5 arrays, dictionaries and collections

The keywords for the array type are Vector. The following is an operation code example.

X = 1; y = 2; z = 3; v = [x, y, z, 4]; // v = [1, 2, 3, 4]

v [0] = 10; // v = [10, 2, 3, 4]

Total = 0; Foreach (Element in v) Total = Element; // Total = 10 2 3 4 = 19

v.sort (); // v = [2, 3, 4, 10] Sort

v.append (11); // v = [2, 3, 4, 10, 11] Add an element to the end

V2 = V [11, 12, 13]; // v2 = [2, 3, 4, 10, 11, 12, 13] combined

v2.remove (0); // v2 become [3, 4, 10, 11, 12, 13] Delete serial number 0 element

v2.removeobj (10); // v2 become [3, 11, 11, 12, 13] Delete elements of all values ​​10

V3 = [v2, v2]; // v3 is a two-dimensional array = [[3, 4, 11, 12, 13], [3, 4, 11, 12, 13]]]

/ / Stack Operation, Pressing and Pophang (Push and POP)

X = [1, 2];

X.pushfirst (0); // x = [0, 1, 2] is pressed into the beginning

X.pushlast (3); // x = [0, 1, 2, 3]

y = x.Popfirst (); // y = 0, x = [1, 2, 3]; pop-up start

z = x.poplast (); // y = 3, x = [1, 2] pops up end

The keyword of the dictionary type is Map. Finding keywords in the dictionary cannot be repeated. The following is an example of a code.

X = 1; y = 2; z = 3;

Word2Number = {"one": x, "two": y, "three": z}; // constructing dictionary

Word2Number ["four"] = 4; // Insert or replace dictionary elements

Word2Nubmer.Remove ("one"); // Delete Dictionary Elements "One"

Word2Number.insert ("Two", 2.0); // Dictionary content is unchanged, because "TWO" is already in the dictionary Union = Word2Number {"FIVE": 5, "Six": 6}; // Dictionary merge

Foreach (Key, Val in Word2Number)

Std :: console (). WriteLine (key, '', val); // Enumerate all keywords and elements values ​​in the Dictionary

Emptymap = {:}; // Construct a blank dictionary

The keywords for the collection type are set. The elements in the collection cannot be repeated. Collection is equivalent to a dictionary with only the keyword. The following is an example of a code.

OneSet = {1, "Hello", 3.14}; // Construction Collection

Another = {1, 2}; // Construct a collection

Union = OneSet Another; // Union = {1, 2, "Hello", 3.14} merge set

Minus = OneSet - Another; // minus = {"Hello", 3.14}, delete set common elements

Joint = OneSet - minus; // joint = {1}, the result is equal to the intersection of OneSet and Another

Foreach (Val in Oneset)

Std :: console (). WriteLine (VAL); // Enumerates All elements

EmptySet = {}; // Construct a blank collection

An example of a group, a dictionary and a collection of mutual conversion examples:

OneSet = {1, 2, 3}; OneVec = Vector (OneSet); // Collection to an array

KeyVec = ['a', 'b']; valvec = ['a', 'b'];

OneMAP = MAP (KeyVec, Valvec); // Two arrays construct a dictionary

2.6 Luban data type universal operation

This section describes the operations that are generally used in different Luban types.

2.6.1 Class Arithmetic Operation

Simply put, universal arithmetic operations icon addresses and subtractions apply to the general Luban data type. But the same calculation has different data types with different results. This is consistent with the operator in the object-oriented model. If the operator does not apply the object to be operated, the result is an error type. The following is exemplified.

// addition operation

Numtwo = 1 1; // NumTwo = 2

StroneOne = "1" "1"; // stroneOne = "11"

VEC = [1] [1]; // vec = [1, 1]

S = {1} {1}; // s = {1}, merge collection

m = {1:10} {2:20}; // m = {1:10, 2:20}, merged dictionary

Err = "1" 1; // Err = error, error, integer and string can not be added

Err2 = true false; // err = error error, logical type cannot be added

// - subtracting operation

ZERO = 1 - 1; // ZERO = 0

Varset = {1, 2, 3} - {3, 4, 5}; // varset = {1, 2} Delete Collection Common Elements

Err = [1] - [1]; // Err = error, error, array type cannot be subtracted

// - Google operation

X = -100; // x = -100

VEC = - [1, 2, 3]; // vec = [3, 2, 1] array for negative equal to reversing order Str = - "abc"; // str = "CBA" string for negative equal to reversing order

Truth = -false; // Truth = True logic type is equal to reversing true and false

Err = - 'a'; // err = error, error, character types can not be negative

// * Multiplication

X = 2 * 2; // x = 4

Str = "ab" * 2; // str = "abab" string multiplier is equal to self-replication

VEC = [1, 2] * 2; // vec = [1, 2, 1, 2] array multiplier is equal to self-replication

// division operation / only for numbers

X = 100/3; // x = 33 Integer Division

Xfloat = 100.0 / 3.0; // xfloat = 33.3333 .... float number

// Removal operation% only for integers

XMOD = 100% 3; // xmod = 1

// plus 1 or ask the next object

X = 0;

Y = x; // x = 1, y = 1, X value increase 1 give y

Z = y ; // z = 1; y = 2; y gives Z then Y value increases 1

X = 'a';

x; // x = 'b' 'a' Next is 'b'

- The operator is just the reverse of . Other arithmetic additive operators have the following examples.

X = 10; // is equivalent to x = x 10;

X - = 10; // is equivalent to x = x-10;

X / = 10; // equivalent to x = x / 10;

X * = 10; // Is equivalent to x = x * 10;

X% = 10; // is equivalent to x = x% 10;

It should be pointed out that the calculation add-in operator is much faster than the equivalent operation. Because the steps are simplified.

It can be seen that Luban's arithmetic operation operations and C are basically the same, but extensions are extended to include non-numerical types.

2.6.2 Enumeration loop statement (FOREACH)

Enumeration loop statements Suitable for container types, including arrays, dictionaries, collection, and string types. Use enumeration loops for non-container types that will cause LUBAN to perform current part code. By following:

X = 3.1415;

Foreach (y in x) // error! The program terminates this

Std :: Println (OBJ = Y);

The following is an example of a correct enumeration loop.

Foreach (x in [10, 20, 30) std :: println (obj = x);

The above code execution will print 10, 20, 30 each number one line on the screen.

2.6.3 Universal member functions

All Luban Data Types have a member function to call. The specific Luban data type defines its own member functions. But the following member functions apply to all or part of the data type.

Allfuncs () member function. Returns all member function names and calls.

x = [];

Funcmap = x.allfuncs ();

Foreach (FuncName, FuncDesc in Funcmap)

Std :: console (). WriteLine (FuncName, '', FuncDesc);

The above program prints the names and description of all array types of member functions on the screen.

Hash () member function, this function is called to determine the storage sequence number when the object is added to a dictionary or a collection.

X = "Hello";

h = x.hash (); // default value 0

The invoke () member function is part of the Luban reflection mechanism. Used to dynamically call member functions

X = "Hello";

H = x.invoke ("hash");

The execution result of the above code is the same as the Direct call hash () member function.

The Serialize () member function converts the object to a serial string.

X = {"One": 1, "Two": 2};

XS = x.Serialize ();

// The following code recovers the serial string to an object.

Samex = std :: des (stream = xs) .Obj;

IF (Samex == X)

Std :: Println (Obj = "Hey, IT Works);

The above code will print "Hey, IT Works" on the screen. A tip is to call a standard component "std :: des" when the string is restored to an object. Place the serial string to "stream" input Attribute terminal, then remove the recovered object from the "OBJ" output.

The following member functions apply only to strings and container types.

Size () member function: Returns the number of elements or string lengths in the container.

X = [10, 20, 30]; s = x.size (); // s = 3 x has 3 elements

Contains () Member Function: Check if the container contains the specified element, or whether the string has a specified sub-string.

x = [10, 20, 30]; HAS20 = X.Contains (20); // HAS20 = True X has element 20

H = "Hello"; Hashell = H.Contains ("Hell"); // Hashell = True H contains substrings "HELL"

Clear () member function: Clear all characters in the container or string in the string.

X = [10, 20, 30]; x.clear (); // x = [];

H = "Hello"; h.clear (); // h = "";

Finally, it is important to note that all member functions are called, if an error, the error type object will be returned.

2.7 Null Value NULL and Error Type Error

Null NULL is a special constant value that is represented by keyword null. Null NULL can be used in the expression like a general constant, like the following example.

ThisaRray = [null, null]; // Defines an array with two null elements

There are two points of null null. First of all, all variables and component properties are null null before no assignment. The second is that null null does not belong to any type.

Error Type Error is a special type. All Luban expressions and operations return an error type value when an error occurs. Applying any operation on the error type object will still return an error type. LUBAN code can also directly construct an error type object. Is an example.

Errval = 1/0; // Generate a zero error error

Errval ISA Error

Std :: console (). WriteLine (Errval);

Expliciterr = Error ("Hey, I am A Born Error");

Std :: console (). WriteLine (Expliciterr);

Running the above code will be printed on the screen:

Error: Error for - * /% Operation: Divid by Zero

Error: Hey, I am A Born Error

2.8 Advanced Luban Script Technology: Multi-threaded parallel and coordination

Multi-threaded parallel and coordination is part of the Luban language. Starting threads and waiting threads in Luban language is very simple, as shown in the following example

Myjobs :: Dothis (arg = 100) & // Start a thread Run Myjobs :: Dothis

Myjobs :: Dothat (arg = 200) & // Start another thread run MYJOBS :: Dothat

{C.DOONE (); d.dotwo ();} & // Start the statement in the curly brackets in a thread

Wait; // Waiting for all threads to end

You can also selectively wait for the thread to end. The Luban thread can be identified by thread-related variables. The Lubsan thread correlation variable defines the left variable of the last assignment statement in the thread code. The following example shows how to use thread-related variables to distinguish and wait LUBAN Thread .x = y.domagic () & // Start a thread, related variable x

A = B.Dogood () & // Start another thread, related variables a

Waitfor X; // Waiting for the thread x end

{x = y.domagic (); a = b.dogood ();} & // Start a thread, related variable A

{c = d.doevil (); f = g.wastetime ();} & // Start a thread, related variable f

WaitFor F; // Waiting for the thread F end

For the same variable for multiple Luban threads, Luban guarantees that only one thread is only one thread at any time. This ensures that the integrity of the object in the variable is not operated by multi-threads simultaneously.

Another problem is how to deal with other sub-threads that may be running at the end of the Luban script. Luban's handling method is to wait for all sub-threads to exit. It is like a WAIT statement is the same.

Another thread-related statement is a CANCEL statement to cancel all start-up threads.

2.9 Thread Traps Example

Example 1:

For (i = 0; i <1000: i) {x = i;} &

The above seemingly simple procedures will be almost certain will fail. Because it will try to start 10,000 threads. There are very few machines to launch 10,000 threads in a process. To put a loop into a thread correctly Write as follows:

{for (i = 0; i <1000: i) {x = i;}} &

Example 2:

For (i = 1; i <= 10; i)

{

Std :: Println (Obj = i) &

}

The first look looks, the above program should print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 per number of rows on the screen. In fact, the above procedures only guarantee the printing of ten one Better number ten. But what is the number of sequences that is not sure. The specific reason is this. This program will launch ten threads to print the contents of the variable "I", and change the contents of the "I" by the main thread loop. After each change, start a thread. This ten adds a thread execution order is uncertain. So it is not necessarily printed in the order of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Small Test: This program is printed with 1, 1, 1, 1, 1, 1, 1, 1, 1 ten "1"?

How to better deal with similar problems, in the later chapter.

2.10 Luban simple world: Everything is an object

Luban and other object-oriented language icons C / Java's difference is that Luban does not have a pointer and a referencer. All variables can only store object itself. Each assignment is equivalent to a new object. As shown in the following code:

a = [1, 2, 3];

B = a; // b Get a new object

B = [4, 5]; // b = [1, 2, 3, 4, 5] a = [1, 2, 3] B, A now different values

Luban's "everything is object" design is mainly for simplicity. Increase the address type will increase the difficulty of users. Another important reason is that simple object transfer makes the interaction between Luban components make it simpler and Vigification, such as the network remote call. Details will be described later.

转载请注明原文地址:https://www.9cbs.com/read-34113.html

New Post(0)