Squirrel (programming language)
From Wikipedia, the free encyclopedia
- This article is about the programming language. For other uses, see Squirrel (disambiguation).
Squirrel is a high level imperative/OO programming language, designed to be a light-weight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games.
Contents |
[edit] Language features
- dynamic typing
- delegation
- classes & inheritance
- higher order functions
- generators
- cooperative threads(coroutines)
- tail recursion
- exception handling
- automatic memory management (primarily reference counting with backup garbage collector)
- weak references
- both compiler and virtual machine fit together in about 6k lines of C++ code.
- optional 16bits characters strings
[edit] 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(vargc >= 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 this.getclass()) 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();
[edit] History
The language was made public in 2003 under the zlib/libpng license. It is developed and maintained by Alberto Demichelis.