Squirrel is the language you use to create Electric Imp applications.
There are many similarities between Squirrel and other languages. For example, all of the standard mathematical, logical and bitwise operators, plus
?:, the ternary operator, are available in Squirrel.
Single-line comments are marked with the familiar
// token, or with
*/ to, respectively, begin and end blocks of multi-line comments.
Statements may be completed with a semi-colon, but this is not required by Squirrel. However, we recommend the use of semi-colons nonetheless to avoid ambiguity in certain circumstances that are discussed in our code style guide.
Functions can be defined with both required and optional input parameters and single a return value.
Squirrel supports standard program-control structures:
if… else if… else;
do… while; and
switch… case. The slightly less commonplace
foreach loop mechanism is provided too; please see the Program Control section if it’s unfamiliar to you. It works exactly like
for… in… structures in other languages.
Squirrel also provides features that are unique. This guide is therefore primarily concerned with the points at which Squirrel diverges from other languages you may be familiar with. How distant it is from your own experience will depend on which particular language or languages you use.
This guide covers Squirrel as it is specifically implemented on the Electric Imp Platform. Electric Imp’s implementation of the language is a slightly modified form of Squirrel 3.0.4, though it incorporates bug fixes implemented in later versions of standard Squirrel. Other Squirrel implementations, including the language’s standard version, many include features absent in the Electric Imp version, and vice versa. If you have experience working with Squirrel on other platforms, please see ‘Electric Imp Squirrel and Standard Squirrel’ for more guidance on how implementations of the language may vary.
We’re always keen to hear about specific gotchas and points of similarity we may have missed, so please tell us about any you find. Visit the Electric Imp Forum to inform us and your fellow Squirrel users (registration required).
The guide includes the following chapters:
Electric Imp provides a full selection of functions that you can make use of to extend your own code. You will need some of these functions to interact with the imp embedded in your product and to access the hardware connected to the imp. Other functions help you manage agent operations, such as communicating with remote Internet resources. Further functions have more general uses: you can use them to add extra functionality to your own application. All of these functions are accessed in the form of Squirrel class instances and their methods.
Electric Imp also provides impCentral™, a web-based application development and testing environment. You’ll use impCentral to develop your application, and you can use it to enter and run the examples in this guide. To do so you will need to create an Electric Imp developer account and add at least one imp-enabled development device to it. For assistance with this process, please see our Getting Started Guide which will walk you through setting up you account, getting your imp development hardware online and becoming familiar with impCentral, which is the subject of a full user guide on this site.
impCentral’s code editor view also feature a log into which messages from the device and its agent are posted. Many of the examples included in this guide make use of this log to view the results of program operations. They do so through the imp API method server.log() which is Electric Imp Squirrel’s equivalent of the print() function found an many other languages.
Squirrel generally treats
# as an alternative single-line comment marker, not to indicate a preprocessor or compiler directive, for example
#import, as other languages do. Use the
const keyword in place of
There are two exceptions to this rule. Electric Imp’s impCentral™ uses
#require to load code libraries and
#line to set the line number of the code following the directive, to aid debugging. For example:
#line 5000 throw ("Error!");
will generate the following output in the log:
2018-01-15 14:30:03.205 "Action": [agent.error] ERROR: Error! 2018-01-15 14:30:03.205 "Action": [agent.error] ERROR: in main agent_code:5000
This directive allows you to mark sections of your code by line number, the better to help you locate the code causing errors.