Squirrel (programming language)
Computer programming language
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.
01Language features
- Dynamic typing
- Delegation
- Classes, inheritance
- Higher-order functions
- Generators
- Cooperative threads (coroutines)
- Tail recursion
- Exception handling
- Automatic memory management (mainly reference counting with backup garbage collector)
- Weak references
- Both compiler and virtual machine fit together in about 7k lines of C++ code
- Optional support for UCS-2 wide strings
02Development state
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:
- Microsoft Visual C++ 6.0, 7.0, 7.1, 8.0, 9.0, and 10.0 (x86 and x64)
- MinGW gcc 3.2 (mingw special 20020817-1)
- Cygwin gcc 3.2
- Linux gcc 3.x
- Linux gcc
- xIllumos
- xXCode 4
03Values and Data types, Lexical Structure
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.
04Integer
An Integer represents a 32 bit (or better) signed number.:
local a = 123 //decimal local b = 0x0012 //hexadecimal local c = 075 //octal [6]05Float
A float represents a 32 bit (or better) floating point number.:
local a=1.0 local b=0.23406String
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 characters07Null
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 = null08Bool
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;09Table
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; } }10Array
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];11Function
Functions are similar to those in other C-like languages with a few key differences (see below).
12Class
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.
13Class Instance
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.
14Generator
Generators are functions that can be suspended with the statement ‘yield’ and resumed later (see Generators).
15Userdata
Userdata objects are blobs of memory or pointers defined by the host application but stored within Squirrel variables (See Userdata and UserPointers).
16Thread
Threads are objects representing a cooperative thread of execution, also known as coroutines.
17Weak Reference
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 |
18Syntax
squirrel's syntax is similar to C/C++/Java etc... but the language has a very dynamic nature like Python/Lua etc...
Factorial in Squirrel
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) } }Generators
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");Classes and inheritance
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();19Applications
Applications using Squirrel
- Code::Blocks, integrated development environment
- Enduro/X, cluster application server
- Electric Imp, an end-to-end IoT platform
Games using Squirrel
- Alien Swarm
- Antinomy of Common Flowers
- Apex Legends
- Battle Brothers
- Contagion
- Counter Strike: Global Offensive
- CRSED: F.O.A.D.
- Final Fantasy Crystal Chronicles: My Life as a King
- Gothic Online, a mod for Gothic II
- GTA IV's IV-MP
- Left 4 Dead 2
- Liberty Unleashed
- Mafia II's M2-Multiplayer
- Melty Blood: Type Lumina
- Nuclear Dawn
- OpenTTD
- Portal 2
- Tom Clancy's Rainbow Six Siege's Heated Metal
- Shadow Warrior
- Simutrans
- Sonic Unleashed (PS2/Wii)
- SuperTux
- Team Fortress 2
- Thimbleweed Park
- Thief II (unofficial NewDark engine update)
- Titanfall
- Vice City Multiplayer, a mod for Grand Theft Auto: Vice City
- Under Night In-Birth
- War Thunder
- Zero no Tsukaima - Maigo no Period to Ikusen no Symphony (PS2)
- Zero no Tsukaima - Muma ga Tsumugu Yokaze no Nocturne (PS2)
- Zero no Tsukaima - Shou-akuma to Harukaze no Concerto (PS2)
20History
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.
Sources and credits
This article is adapted from the Wikipedia article “Squirrel (programming language)”, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.
Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.