Squirrel Programming Language By Nandini Bhatta CS537 Summer 2008 Contents Introduction Introduction Started in the year 2003 High level OO programming language Light weight programming language featuring higher- order functions, classes/inheritance Powerful scripting tool that fits in the size, memory bandwidth Real-time requirements of applications like games It has a C-like syntax Features Open Source Inheritance Exception Handling Polymorphism Compiles on both 32-bit & 64-bit architectures Tail Recursion Dymanic Typing Automatic memory management Development State Two versions : 1.01 & 2.2.1 The current stable release is 2.2.1 It can be Compiled and run on Windows and Linux Squirrel is known to run also on MacOS X, GameCube, PSP and XBOX Keywords & Operators Similar to keywords & operators in Java Keyword examples : case, catch, class, constructor, parent, yield, while, return, resume, null default, delegate, delete, else, enum, switch, true, false, throw, try…. Operators examples : ! ,!=, ||, ==, &&, <=, => >, + +,/ , /=, *, *=, %, %=, ++, --, <-, =, &, ^……… Data types Squirrel is a dynamically typed language so variables have no types They refer to a value that does not have a type Squirrel basic types include integer, float, string, null, table, array, function, generator, class, instance, Boolean Functions, Class & Class instance Functions similar to those in C language Classes created through class expression or class statement. Class members are inherited from another class object at creation time. After creation, members can be added until an instance of the class is created. Variables Two types of variables in squirrel : local and global variables. Global variables stored in tables and hence referred to as table slots Local & Global variables…. Squirrel first checks if an identifier is local variable (function arguments) If not it checks if it is a member of the environment object (this) For example : function testy(arg) { local a=10; print(a); return arg; } will access to local variable 'a' and prints 10. function testy(arg) { local a=10; return arg+foo; } In this case ’foo’ is treated as ‘this.foo’ Global variables Global variables are stored in root table Environment object is the root table To explicitly access global table from another scope, the slot name must be pre fixed with ‘::’ Example: Accessing global variable To access global variable ‘foo’ function testy(arg) { local a=10; return arg +::foo; } Squirrel statements Squirrel program is a simple sequence of statements Squirrel statements are similar to C statements such as assignment, function calls, program flow control structures etc. Statements can be separated with new lines or using ‘;’ stats := stat [';'|'\n'] stats Function declaration Syntax: funcname := id [ '::' id] stat:= 'function' id [ '::' id]+ '(' args ')'[ ':' '(' args ')'] stat Example: T <- { } function T::ciao(a,b,c) { return a+b-c; } Class declaration To create a new class : Syntax: memberdecl : = id '=' exp [ ';'] | '[ ' exp ']' '=' exp [ ';'] | functionstat | 'constructor' stat:= 'class' derefexp ['extends' derefexp] '{' [memberdecl] '}‘ contd… Example : class Foo { //constructor constructor(a) { testy = ["stuff",1,2,3]; } //member function function PrintTesty() { foreach(i,val in testy) { ::print("idx = "+i+" = "+val+" \n"); } } //property testy = null; } Array Constructor Syntax: exp := ‘[’ [explist] ‘]’ Example: a <- [ ] //creates an empty array Arrays can be initialized with values during the construction Example: a <- [1,"string!",[ ],{ }] elements //creates an array with 4 Groovy Vs Squirrel Both are object oriented and dynamic languages. Both can be used as a Scripting Language for the Java Platform. Groovy syntax is similar to Java where as Squirrel syntax is similar to C. Syntax Comparision The following presents a side-by-side comparison of Groovy with Squirrel: Example 1: Print Statement: Groovy: println "hello world" Squirrel: Local a= “hello world”; Print (a) ; Example 2 Groovy: def x = 1..10 x.each{println it} Squirrel: local a=0; do { print(a+"\n"); a+=1; } while(a>10 Example 3 Groovy: if (a>b) { assert c == a-b println c } else { assert c == b-a println c } Squirrel: if(a>b) { c= a-b; print (c); } else { c=b-a; Print (c); } Inheritance Example class Foo { function DoSomething() { ::print("I'm the base"); } }; class SuperFoo extends Foo { //overridden method function DoSomething() { //calls the base method ::Foo.DoSomething(); ::print("I'm doing something"); } } Work in progress Comparision with Groovy and Java Exploring Squirrel with game programming Exploring Squirrel SQL database References www.squirrel-lang.org/doc/squirrel2.pdf www.wikipedia.org www.sourceforge.net www.experiencefestival.com/squirrel_programming_l anguage_-_syntax