Squirrel Reference Manual - Key Concepts
Introduction
- Squirrel is a high-level, imperative-object-oriented programming language.
- Designed for applications like games, where size, memory, and real-time performance are critical.
- Features include dynamic typing, delegation, higher order functions, generators, tail recursion, exception handling, and automatic memory management.
Lexical Structure
Identifiers
- Begin with an alphabetic character or
_ and may contain digits.
- Case sensitive._
Keywords
- Reserved words include: base, break, class, const, delete, else, enum, extends, function, if, in, local, null, return, switch, this, typeof, while, yield, etc.
Operators
- Arithmetic:
+, -, *, /, %
- Logical:
&&, ||, !
- Relational:
==, !=, <, <=, >, >=
- Assignment:
=, +=, -=
- Special:
<=>, instanceof, typeof*
Tokens and Literals
- Integer, float, and string literals.
- Comments use
/* ... */, //, #.
Values and Data Types
Basic Types
- Integer, Float, String, Null, Bool, Table, Array, Function, Generator, Class, Instance, Userdata, Thread.
Data Type Details
- Integer: 32-bit signed numbers.
- Float: 32-bit floating point numbers.
- String: Immutable sequences of characters, similar to C/C++.
- Null: Represents non-existence; only one value
null.
- Bool: Boolean values
true and false.
Complex Types
- Table: Associative containers (key/value pairs).
- Array: Dynamic sequence of objects.
- Function: First class values, local or global.
- Class and Instance: Similar to tables but support inheritance and encapsulation.
- Generator: Functions that can be suspended and resumed.
- Userdata: Blobs of memory or pointers in host application.
- Thread: Cooperative threads of execution.
- Weak Reference: References that do not influence object lifetime.
Execution Context
- Union of function stack frame, function environment, and root table.
- Variables can be local or slots in tables.
Statements and Control Structures
Control Statements
- if/else: Conditional execution.
- while/do-while: Loops based on condition.
- for/foreach: Iteration constructs.
- switch: Multiple selection of code paths.
- break/continue: Loop control.
- return: Exit function/generator optionally with a value.
- yield: Suspend function, return to caller.
Functions
- Defined via
function keyword.
- Support default parameters and variable argument lists.
- Can be local or global.
- Raw calls: Override default environment binding.
Classes
- Support single inheritance and metamethods for operator overloading.
- Attributes: Metadata for documentation, IDEs, etc.
Generators
- Functions using
yield to pause and resume execution.
Threads
- Created with
newthread(), cooperative, independent stacks.
Weak References
- Create non-owning references to objects.
Delegation
- Tables can have parent tables, allows defining special behaviors for children.
Metamethods
- Customize language semantics through special functions in classes or tables.
Built-in Functions
- Comprehensive set for string manipulation, type conversion, table/array handling, etc.
Embedding Squirrel
- Reference counting and optional garbage collection.
- API allows for script execution, function calls, and data manipulation.
API Reference
- Detailed functions for stack operations, error handling, compiling scripts, etc.
This summary covers the essential concepts and features of the Squirrel scripting language as per the reference manual. It includes syntax, control structures, data types, and advanced features like delegation and metamethods to understand and work with Squirrel effectively.