Brief Introduction to Small

This section describes the basics of Small, as compiled and interpreted with Embryo.

This summary assumes that you are familar with C. For a full list of differences between C and Small, again, see the full documentation.

Variables

Types

There is only one type, known as the "cell", which can hold an integer.

Scope

The scope and usage of a variable depends on its declaration.

  • A local variable is normally declared with the new keyword. E.g.
    new variable
  • A static function variable is defined within a function with the static keyword.
  • A global static variable is one that is only available within the file it was declared in. Again, use the static keyword, but outside of any function.
  • A stock variable is one that may not be compiled into a program if it is not used. It is declared using stock.
  • A public variable is one that can be read by the host program using embryo_program_variable_find. It is declared using public keyword.

Remember that the keywords above are to be used on their own. That is, for example:

public testvar

not:

new public testvar

Constants

You can declare constants in two ways:

  • Using the preprocessor macro #define.
  • By inserting const between the keyword and variable name of a variable declaration. For example, to declare the variable var1 constant, you type
    new const var1 = 2
    Now var1 cannot be changed.

Arrays

To declare an array, append square brackets to the end of the variable name. The following examples show how to declare arrays. Note the use of the ellipsis operator, which bases the array based on the last two declared values:

new msg[] = "A message."
new ints[] = {1, 3, 4}
new ints2[20] = {1, 3} // All other elements 0.
new ints3[10] = {1, ... } // All elements = 1
new ints4[10] = {10, 20, ... } // Elements = 10 -> 100.
// The difference can be negative.
new ints5[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Note
Array initialisers need to be constant.

Function Calls

A typical function declaration is as follows:

testfunc(param) {
// Do something ...
// over a couple of lines.
}

You can pass by reference. That is, the parameter you pass is changed outside of the function. For example:

testfunc(&param) {
param = 10
// The passed variable will be set to 10 outside of the function.
}

To pass an array:

testfunc(param[]) {
// Do something to the array
}
Note
Arrays are passed by reference.

Control Structures.

Small has the following control structures, which similar to their C counterparts:

  • if (expression) statement1 else statement2
  • switch (expression) {
    case 0:
    statement1 // Can only be one statement. Look Ma, no breaks!
    case 1..3: // For values between 1 and 3 inclusive.
    statement2
    default: // Optional
    statement3
    }
  • while(expression) statement
  • do statement while (expression)
  • for (init_expression; before_iter_test_expression; after_iter_expression) statement

Preprocessor

The following preprocessor directives are available:

  • #assert constant_expression
  • #define pattern replacement
  • #define pattern(%1,%2,...) replacement
  • #include filename
  • #if constant_expression
    // Various bits of code
    #else
    // Other bits of code
    #endif
  • #undef pattern