Hello Squirrel

The bipscript language is superset of the squirrel programming language, bipscript adds the bipscript API and also extends the language with new operator types specifically for audio and midi connections.

Squirrel Language Background

The Squirrel language is an object-oriented programming language used as the scripting language in several video game titles. It was designed to be friendly to real-time applications, this makes it ideal for use in video games and now audio scripting as well.

Basic Types and Data Structures

Primitive types in squirrel include types for boolean values, floating point numbers, integers and strings. Here we declare some primitive types using the local keyword which designates a variable with local scope:



local foo = "I'm a string"
local n = 10
local x = 0.125


More complex data structures include arrays and tables. Arrays are a zero-indexed sequence of any type:



local anArray = [2, "string", 3.03]
print(anArray[1] + "\n") // prints "string" (plus a newline)


Tables are containers that associate each value with a key:



local bob = {
    name = "Bob Johnson",
    active = true,
    instruments = ["bass", "keyboards"]
}
print(bob.instruments[0] + "\n") // prints "bass"


New Slot Operator

When using the table data structure note that attempting to access a table index that does not exist will result in an error, here we use try/catch statements to trap the error:


try {
	print(bob.age)
} catch(e) {
	print(e + "\n")
}
// prints "the index 'age' does not exist"


The same error will be thrown when trying to assign to an unknown index, in this case use the new slot operator:



bob.age <- 42

print(bob.age + "\n")


Note also there is an implicit table associated with the script we can assign to its slots as well:



scriptVar <- "hello"


Conditional Statements and Looping

Basic conditional branching is implemented with an if statement similar to other languages:


local x = 5
if(x > 3) {
    print(x + " is greater than 3\n")
}

There are also while loops which work as expected:


local y = 1
while(y <= 10) {
    print("y is now " + y + "\n")
    y++
}

An alternative to the above is the standard for loop:


for(local z = 1; z <= 10; z++) {
    print("z is now " + z + "\n")
}

There is also a foreach loop for arrays and tables:


foreach(key, val in bob) {
    print("key '" + key + "' has value: " + val + "\n")
}


More Information



Complete Script

local foo = "I'm a string"
local n = 10
local x = 0.125

local anArray = [2, "string", 3.03]
print(anArray[1] + "\n") // prints "string" (plus a newline)

local bob = {
    name = "Bob Johnson",
    active = true,
    instruments = ["bass", "keyboards"]
}
print(bob.instruments[0] + "\n") // prints "bass"

try {
	print(bob.age)
} catch(e) {
	print(e + "\n")
}
// prints "the index 'age' does not exist"

bob.age <- 42

print(bob.age + "\n")

scriptVar <- "hello"

local x = 5
if(x > 3) {
    print(x + " is greater than 3\n")
}

local y = 1
while(y <= 10) {
    print("y is now " + y + "\n")
    y++
}

for(local z = 1; z <= 10; z++) {
    print("z is now " + z + "\n")
}

foreach(key, val in bob) {
    print("key '" + key + "' has value: " + val + "\n")
}


List of All ExamplesDemo Applications


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.