After using Squirrel for a while, I also learned about the new language. I decided to write an introduction to the language itself. I am currently using Squirrel 2.2. This is a stable version and the documentation is complete. Squirrel 3.0 Alpha has been released, but there are no documents. I just took a look at it.
1: Reserved Words:
The reserved words of Squirrel are almost the same as those of C ++, but Squirrel has no pointer. The memory is managed by virtual machines and does not need to be manually applied for or released. Therefore, no new or delete keywords exist. Squirrel is of a weak type, so there are no keywords such as char, float, int, and double. It is worth noting that Squirrel does not support double, and its floating point is float, this may be related to Squirrel primarily for game design.
Squirrel adds some keywords, mainly includingLocal, typeof, instanceof, resum, yield,Delegate and parent, among which delegate and parentKeyword inSquirrel3.0AlphaThe version seems to have changed. I will discuss it later,Resum, yieldThe "coroutine" used for Squirrel can be understood as a non-preemptive internal thread (Squirrel virtual machine is a single thread, and a virtual machine does not support system threads, which is similar to Lua ).
Delegate and parent are used for Class "delegation" and parent class access, which can be ignored for the moment. In addition, Squirrel is almost the same as C ++.
It is worth noting that the local keyword. Variables in Squirrel need to be defined. Like C ++, you can define them anywhere in the program, but you must use the local keyword when defining them. Using local indicates that the variable is created in the stack, and is automatically destroyed when the function is left. This is consistent with how C ++ defines the variable in the function.
2: code blocks and expressions
What Lua hates is do... end, which indicates the code block. I am not used to it anyway. Squirrel is considerate and uses {...}, which is quite pleasing to the eye.
For expressions, squirrel is almost the same as C/C ++. You can use the plus sign (+ =), minus sign (-=), or even minus sign (-= ).
? : Ternary operator. Squirrel 3.0 supports lambda expressions, but I have never used them.
3: control loop statements
Squirrel's control loop statement is also consistent with C/C ++, which is one of the reasons why I use squirrel. Lua's for loop is hard to use. If... else ..
It is not concise enough.
The only difference between squirrel loop control statements and C/C ++ is that the foreach loop is added, and this foreach does not need any "closure" like Lua, which can be used very easily. For example
Foreach (Local
I in arrayvar) {}, which completes the traversal of an array
Foreach (Local
Key, value in tablevar) {}, which completes the traversal of an expression