Skip to main content

Squirrel Language Reference

Electric Imp applications are programmed for both physical hardware and cloud operation using Squirrel, a high-level, imperative and object-oriented language with garbage collection and a syntax similar to C, C++, C# and JavaScript. All Squirrel programs running on an imp will need to use at least some of the imp API — the objects and classes that encapsulate each imp, its connected hardware and its cloud-hosted agent, which mediates the device’s Internet connection. Squirrel programs may also need parts of the Squirrel standard libraries.

Electric Imp’s implementation of the language is a slightly modified form of Squirrel 3.0.4. Programmers who have used Squirrel before can read about the differences between standard Squirrel and the Electric Imp version here. Standard Squirrel is formally documented at the Squirrel web site.

  • Squirrel Programming Guide describes the language in detail. It also covers the points where Squirrel diverges from other languages and where it offers unique functionality and methodologies.
  • Squirrel application development guidelines lists the key techniques and methodologies you should employ in order to develop and maintain a successful Electric Imp application.
  • You should read the guide Writing Efficient Squirrel to gain valuable insights into improving your Squirrel code. Further Squirrel Developer Guides can be found here.
  • We also have a handy printable two-page Squirrel Cheat Sheet detailing all the key Squirrel features.
  • Sample code and other examples provided by Electric Imp now follow our formal Squirrel Style Guide.

The Squirrel standard library includes the following global objects with methods designed to manipulate these specific data types:

  • array object — an ordered collection of Squirrel data entitles
  • blob object — user-defined storage for binary data
  • bool object — delegate methods for Boolean values
  • float object — delegate methods for floating-point values
  • function object — delegate methods for functions
  • integer object — delegate methods for integer values
  • regexp object — delegate methods for regular expression objects
  • regexp2 object — improved delegate methods for agent-based regular expression objects
  • string object — an ordered sequence of alphanumeric characters
  • table object — a collection of Squirrel data entitles stored in key-value pairs

It also includes the following libraries of functions:

  • math — mathematics functions
  • string — string manipulation and formatting functions
  • system — general-purpose functions

Note On Terminology

The documentation draws a distinction between methods and functions. Methods are functions which are part of an object itself, or a delegate object, and are thus called on the target object or data type using dot syntax:

local a = myString.tointeger();
// tointeger() is a delegate method so uses dot syntax

A function is a standalone entity and is not called using dot syntax:

local strippedString = strip(myString);
// strip() is a function so doesn't use dot syntax

Feedback via the forum is very welcome.