Squirrel programming language
Encyclopedia
Squirrel is a high level imperative
Imperative programming
In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state...

, object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

, designed to be a light-weight scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

 that fits in the size, memory bandwidth, and real-time requirements of applications like video games. 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
Code::Blocks
Code::Blocks is a free and open source, cross-platform IDE which supports multiple compilers including GCC and MSVC. It is developed in C++ using wxWidgets as the GUI toolkit. Using a plugin architecture, its capabilities and features are defined by the provided plugins.Currently, Code::Blocks is...

 for scripting and was also used in Final Fantasy Crystal Chronicles: My Life as a King
Final Fantasy Crystal Chronicles: My Life as a King
Final Fantasy Crystal Chronicles: My Life as a King is a video game developed for the WiiWare service of the Nintendo Wii console by Square Enix...

It is also used in Left 4 Dead 2
Left 4 Dead 2
Left 4 Dead 2 is a cooperative first-person shooter video game. It is the sequel to Valve Corporation's award-winning Left 4 Dead. The game launched on November 17, 2009, for Microsoft Windows and Xbox 360 in the United States and November 20 in Europe; in 2010, Left 4 Dead 2 was made available to...

and Portal 2
Portal 2
Portal 2 is a first-person puzzle-platform video game developed and published by Valve Corporation. The sequel to the 2007 video game Portal, it was announced on March 5, 2010, following a week-long alternate reality game based on new patches to the original game...

for scripted events.

Language features

  • Dynamic typing
  • Delegation
    Delegation (programming)
    In object-oriented programming, there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement...

  • Classes
    Class (computer science)
    In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

    , inheritance
    Inheritance (object-oriented programming)
    In object-oriented programming , inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support...

  • Higher order functions
  • Generators
    Generator (computer science)
    In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values...

  • Cooperative threads (coroutines)
  • Tail recursion
  • Exception handling
    Exception handling
    Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....

  • Automatic memory management (mainly reference counting
    Reference counting
    In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource...

     with backup garbage collector
    Garbage collection (computer science)
    In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

    )
  • Weak references
  • Both compiler
    Compiler
    A compiler is a computer program that transforms source code written in a programming language into another computer language...

     and virtual machine
    Virtual machine
    A virtual machine is a "completely isolated guest operating system installation within a normal host operating system". Modern virtual machines are implemented with either software emulation or hardware virtualization or both together.-VM Definitions:A virtual machine is a software...

     fit together in about 7k lines of C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

     code
  • Optional 16-bit
    16-bit
    -16-bit architecture:The HP BPC, introduced in 1975, was the world's first 16-bit microprocessor. Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816. The Intel 8088 was program-compatible with the Intel 8086, and was 16-bit in that its registers were 16...

     character strings
    String (computer science)
    In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....


Syntax

Squirrel uses a C-like syntax.

Factorial in Squirrel:

function factorial(x)
{
if (x 0) {
return 1;
}
else {
return x * factorial(x-1);
}
}

Random numbers using generators:

function gen_random(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 = gen_random(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;

History


The language was made public in 2003 under the zlib/libpng license
Zlib License
The zlib License is a permissive free software license which defines the terms under which the zlib and libpng software libraries can be distributed. It is also used by other free software packages....

.
In November 2010, the license was changed to MIT license
MIT License
The MIT License is a free software license originating at the Massachusetts Institute of Technology . It is a permissive license, meaning that it permits reuse within proprietary software provided all copies of the licensed software include a copy of the MIT License terms...

 to enable the project to be hosted on Google Code
Google Code
Google Code is Google's site for developer tools, APIs and technical resources. The site contains documentation on using Google developer tools and APIs—including discussion groups and blogs for developers using Google's developer products....

.
It is developed and maintained by Alberto Demichelis.

See also


  • Lua
  • Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

  • Ruby
    Ruby (programming language)
    Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...


External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK