This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
| Squirrel | |
|---|---|
| Paradigm | Multi-paradigm: scripting, imperative, functional, object-oriented |
| Designed by | Alberto Demichelis |
| First appeared | September 6, 2003 |
| Stable release | |
| Typing discipline | Dynamic |
| OS | Cross-platform |
| License | MIT license |
| Filename extensions | .nut |
| Website | squirrel-lang |
| Influenced by | |
| C++, JavaScript, Lua, Python | |
| Influenced | |
| MiniD (since renamed Croc) | |
Squirrel is a high level imperative, object-oriented programming language, designed to be a lightweight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games, released in September 6, 2003.
MirthKit, a simple toolkit for making and distributing open source, cross-platform 2D games, uses Squirrel for its platform. It is used extensively by Code::Blocks for scripting and was also used in Final Fantasy Crystal Chronicles: My Life as a King. It is also used in Left 4 Dead 2, Portal 2, Team Fortress 2, Thimbleweed Park, and War Thunder for scripted events and in NewDark, an unofficial Thief 2: The Metal Age engine update, to facilitate additional, simplified means of scripting mission events, aside of the regular C scripting.
Version 2.0 was released on April 3, 2005. The current stable release is 3.2, released on February 10, 2022.
The project can compile and run on Windows (x86 and x64), Linux, Illumos (x86 and x64), Android, iOS, macOS, and FreeBSD.
It has been tested with the following compilers:
While Squirrel is a dynamically typed language and variables do not have a type, different operations may interpret the variable as containing a type. Squirrel’s basic types are integer, float, string, null, table, array, function, generator, class, instance, bool, thread and userdata.
An Integer represents a 32 bit (or better) signed number.:
local a = 123 //decimal local b = 0x0012 //hexadecimal local c = 075 //octal
A float represents a 32 bit (or better) floating point number.:
local a=1.0 local b=0.234
strings are an immutable sequence of characters. In order to modify a string is it necessary create a new one.
Squirrel’s strings are similar to strings in C or C++. They are delimited by quotation marks(") and can contain escape sequences (\t, \a, \b, \n, \r, \v, \f, \\, \", \', \0, \x<hh>, \u<hhhh> and \U<hhhhhhhh>).
Verbatim string literals do not interpret escape sequences. They begin with @" and end with the matching quote. Verbatim string literals also can extend over a line break. If they do, they include any white space characters between the quotes:
local a = "I'm a wonderful string\n" // has a newline at the end of the string local x = @"I'm a verbatim string\n" // the \n is literal, similar to "\\n" in a regular string.
However, a doubled quotation mark within a verbatim string is replaced by a single quotation mark:
local multiline = @"
this is a multiline string
it will ""embed"" all the new line
characters
The null value is a primitive value that represents the null, empty, or non-existent reference. The type Null has exactly one value, called null.:
local a = null
Bool is a double-valued (Boolean) data type. Its literals are true and false. A bool value expresses the validity of a condition (tells whether the condition is true or false).:
local a = true;
Tables are associative containers implemented as a set of key/value pairs called slots.:
local t={}
local test=
{
a=10
b=function(a) { return a+1; }
}
Arrays are simple sequence of objects. Their size is dynamic and their index always starts from 0.:
local a = ["I'm","an","array"] local b = [null] b[0] = a[2];
Functions are similar to those in other C-like languages with a few key differences (see below).
Classes are associative containers implemented as sets of key/value pairs. Classes are created through a ‘class expression’ or a ‘class statement’. class members can be inherited from another class object at creation time. After creation, members can be added until an instance of the class is created.
Class instances are created by calling a class object. Instances, as tables, are implemented as sets of key/value pairs. Instance members cannot be dynamically added or removed; however the value of the members can be changed.
Generators are functions that can be suspended with the statement ‘yield’ and resumed later (see Generators).
Userdata objects are blobs of memory or pointers defined by the host application but stored within Squirrel variables (See Userdata and UserPointers).
Threads are objects representing a cooperative thread of execution, also known as coroutines.
Weak References are objects that point to another (non-scalar) object but do not own a strong reference to it. (See Weak References).
The following words are reserved and cannot be used as identifiers:
| base | break | case | catch | class | clone |
| continue | const | default | delete | else | enum |
| extends | for | foreach | function | if | in |
| local | null | resume | return | switch | this |
| throw | try | typeof | while | yield | constructor |
| instanceof | true | false | static | __LINE__ | __FILE__ |
| rawcall |
Identifiers start with an alphabetic character or the symbol ‘_’ followed by any number of alphabetic characters, ‘_’ or digits ([0-9]). Squirrel is a case sensitive language meaning that the lowercase and uppercase representation of the same alphabetic character are considered different characters. For instance, “foo”, “Foo” and “fOo” are treated as 3 distinct identifiers.
Squirrel recognizes the following operators:
! |
!= |
|| |
== |
&& |
>= |
<= |
> |
<=> |
+ |
+= |
- |
-= |
/ |
/= |
* |
*= |
% |
%= |
++ |
-- |
<- |
= |
& |
^ |
| |
~ |
>> |
<< |
>>> |
As for Significant tokens:
{ |
} |
[ |
] |
. |
: |
:: |
' |
; |
" |
@" |
A comment is text that the compiler ignores but that is useful for programmers. Comments are normally used to embed annotations in the code. The compiler treats them as white space.
A comment can be /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. This syntax is the same as ANSI C:
/* any text between will be ignored */
Squirrel accepts integer numbers, floating point numbers and string literals.
34 |
Integer number(base 10) |
0xFF00A120 |
Integer number(base 16) |
0753 |
Integer number(base 8) |
'a' |
Integer number |
1.52 |
Floating point number |
1.e2 |
Floating point number |
1.e-2 |
Floating point number |
"I'm a string" |
String |
@"I'm a verbatim string" |
String |
@" I'm a multiline verbatim string " |
String |
squirrel's syntax is similar to C/C++/Java etc... but the language has a very dynamic nature like Python/Lua etc...
local function factorial(x) // Getting function which is factorial, then getting value which is x
{
if (x <= 1) { // if x is less than 1
return 1; // then the code will just return 1
}
else { // if that doesn't work
return x * factorial(x-1); // then the code will return x times factorial(x-1)
}
}
function not_a_random_number_generator(max) {
local last = 42;
local IM = 139968;
local IA = 3877;
local IC = 29573;
for(;;) { // loops forever
yield (max * (last = (last * IA + IC) % IM) / IM);
}
}
local randtor = not_a_random_number_generator(100);
for(local i = 0; i < 10; i += 1)
print(">"+resume randtor+"\n");
class BaseVector {
constructor(...)
{
if(vargv.len() >= 3) {
x = vargv[0];
y = vargv[1];
z = vargv[2];
}
}
x = 0;
y = 0;
z = 0;
}
class Vector3 extends BaseVector {
function _add(other)
{
if(other instanceof ::Vector3)
return ::Vector3(x+other.x,y+other.y,z+other.z);
else
throw "wrong parameter";
}
function Print()
{
::print(x+","+y+","+z+"\n");
}
}
local v0 = Vector3(1,2,3)
local v1 = Vector3(11,12,13)
local v2 = v0 + v1;
v2.Print();
The language was made public in 2003 under the zlib/libpng license. In November 2010, the license was changed to the MIT license to enable the project to be hosted on Google Code. It is developed and maintained by Alberto Demichelis.