Source §3

Source §3 is a small programming language, designed for the third chapter of the textbook Structure and Interpretation of Computer Programs, JavaScript Adaptation (SICP JS).

What names are predeclared in Source §3?

On the right, you see all predeclared names of Source §3, in alphabetical order. Click on a name to see how it is defined and used. They come in these groups:

  • AUXILIARY: Auxiliary constants and functions
  • MISC: Miscellaneous constants and functions
  • MATH: Mathematical constants and functions
  • LISTS: Support for lists
  • PAIRMUTATORS: Mutating pairs
  • ARRAYS: Support for arrays
  • STREAMS: Support for streams

What can you do in Source §3?

You can use all features of Source §2 and all features that are introduced in chapter 3 of the textbook. Below are the features that Source §3 adds to Source §2.

Variable declaration statements

In Source §3, variables are declared as in:

let my_variable = x * 4;

The scope of names declared with let is the same as the scope of names declared with const: the closest surrounding block. The difference is that variables can be used in assignment statements.

Variable assignment statements

Variables can be assigned to as in:

let x = 1;
display(x); // x is still 1
x = x + 1;
diplay(x);  // now x is 2
Read more on variable declaration and assignment in section 3.1.1 Local State Variables of the textbook.

While loops

A while loop repeatedly evaluates a predicate and if the predicate returns true, evaluates a given block. The evaluation terminates when the predicate returns false. Example:

let x = 0;
while (x < 10) {
    display(x);
    x = x + 1;
}

will display the numbers from 0 to 9.

While loops are not covered in the textbook.

For loops

The pattern of repeatedly testing and changing a particular variable is supported by for loops. The same program can be written shorter as:

let x = 0;
for (x = 0; x < 10; x = x + 1) {
    display(x);
}

The increment statement x = x + 1 is always evaluated after the body of the loop.

You can limit the scope of the variable to just the for loop, by writing let after the parenthesis:

for (let x = 0; x < 10; x = x + 1) {
display(x);
}
For loops are not covered in the textbook.

Arrays, array access and array assignment

Arrays are created using literal array expressions, as follows:

const my_array = [10, 20, 30];

The constant my_array now refers to an array with three elements. The elements in such a literal array expressions have implicit keys. The first element has key 0, the second has key 1, the third has key 2 and so on.

An array can be accessed using array access expressions, with a given key:

my_array[0] + my_array[1] + my_array[2]; // 60

Like pairs, arrays can be changed in Source §3. This is done using array assignment:

my_array[1] = 200;

Array assignment and array access in Source §3 are restricted to integers (numbers with no fractional component) larger than or equal to 0 and less than 232-1. We call such numbers array indices.

You can use any array index in array assignment; the array will automatically adjust its size. Accessing an array at an array index that has not been assigned yet (using a literal array expression or an array assignment) will return undefined.

Arrays are not covered in the textbook.

You want the definitive specs?

For our development team, we are maintaining a definitive description of the language, called the Specification of Source §3. Feel free to take a peek!