← Fundamentals
Fundamentals

Evaluation order

Multiplication groups the duration. Does it also schedule the cue?

Separate how an expression groups its operations from when those operations run.

Outcome

Separate grouping from evaluation order, identify the native guarantees and make the intended cue order and skipped work explicit.

Terminology (4)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Precedence
Which operators bind more tightly when the expression’s grouping is formed. It does not generally determine operand execution order.
Associativity
How operators at the same precedence group. Here a - b - c groups as (a - b) - c.
Evaluation order
The order in which operand computations and their effects take place, as specified by the native construct.
Short-circuit evaluation
A construct can determine its result without evaluating an unused operand or branch.

Grouping and evaluation order

Operator precedence decides how different operators group; associativity settles grouping among operators at the same precedence. Parentheses make grouping explicit. For example, adding a duration to twice another duration groups the multiplication as one operand of the addition.

Evaluation order is a separate question: when are operands read and helper calls performed? That matters when a call prints a marker or changes a cursor. Some constructs also skip evaluation of an unneeded operand. Determine the selected language’s guarantees for the exact expression before using a numerical calculation as evidence about its sequence of effects.

01Grouping and cue order

Does the multiplication make rest run first?

A show-control editor lets an operator inspect a cue timeline before playback. The preview calculates phrase lengths, places cue markers and highlights the span of a selected cue. Alongside the duration, it must preserve marker order and leave the timeline untouched when a count-in is disabled.

The first reduced preview records each helper call as a letter: tap prints T and returns 2 beats; rest prints R and returns 5. It adds the tap duration to twice the rest duration. Multiplying the returned duration does not call rest twice. Predict the letters separately from the final number.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function tap(): number {
    console.log("T");

    return 2;
}

function rest(): number {
    console.log("R");

    return 5;
}

const beats = tap() + rest() * 2;

console.log(beats);
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit && node main.ts

Use the selected language’s existing setup and replace its main file for each experiment. All objects stay alive through their calls and no reference escapes.

Which complete result does the language guarantee for the shown program?

02Where the cursor is read

Which cursor position reaches each marker?

The duration is settled, but the editor also needs the beat at which each marker was placed. In this reduced check, mark returns the current cursor position and advances it by one beat. The program prints the two supplied positions with showMarks, then the final cursor. Inspect the exact marker calls and cursor reads in the selected source. Predict the full set the language permits.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Cursor = { tick: number };

function mark(cursor: Cursor): number {
    const value = cursor.tick;
    cursor.tick += 1;

    return value;
}

function showMarks(first: number, second: number): void {
    console.log(first, second);
}

const cursor: Cursor = { tick: 20 };
showMarks(mark(cursor), mark(cursor));
console.log(cursor.tick);
Which outcome set follows from this exact native source?

03Choose before the calls run

Does choosing a duration skip the other cue?

The operator selects a tap rather than a rest for the next cue. An ordinary choose helper returns the selected duration. Predict whether the preview also performs only the selected work.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function tap(): number {
    console.log("T");

    return 2;
}

function rest(): number {
    console.log("R");

    return 5;
}

function choose(flag: boolean, left: number, right: number): number {
    return flag ? left : right;
}

const beats = choose(true, tap(), rest());

console.log(beats);
choose receives true. Which helper work has happened by the time it returns?
Loading Eager arguments, then selection…

The editor also offers an optional count-in rest before the requested cue. That rest must run only when its duration is positive; zero means no count-in event. The highlight feature needs two saved positions around the requested cue. Its start is the cursor after any enabled count-in; its end is the cursor after the requested cue. For example, starting at beat 7 with a 2-beat count-in and a 4-beat rest highlights beats 9 through 13. The preview records RR: one count-in and one requested rest. Without the count-in it highlights 7 through 11 and records one R. The supplied Span type stores these boundaries as start and end; reading the cursor at another point can leave the total advancement right while the highlight is wrong.

04When choices contain sequences

An unused branch can contain a whole sequence

The editor can also preview an alternate cue plan before the operator commits to playback. For this feature, the complete ordered transcript matters. A cue is one tap or rest. A sequence runs its complete left child, then its complete right child, and adds their durations. A choice runs one child selected by its flag. A child can itself be a sequence or choice, so an unused child may contain several effects.

For example, a sequence can contain a choice on its left and a final rest on its right. If that choice selects a tap and skips a pair of other cues, execution records only T followed by the final R. Running both children first and choosing a number afterward would perform the skipped pair too.

The supplied input reader constructs a small array of nodes. Each node has a kind, left/right child indices, a flag and cue duration fields. Children come before parents and the final node is the root. A cue flag chooses tap or rest; a choice flag selects left or right. All types and decoding are supplied.

The existing plan function calls itself for a child: that call applies the same rule to the child’s entire subtree. At each sequence, preserve the required order of those child calls; at each choice, preserve which child call is allowed to happen. Fixing only the outermost node is insufficient when either kind is nested inside the other. The function’s returned duration and the machine’s T/R transcript expose different parts of the repair.

Check this idea later
In the ordinary arithmetic expression first() + second() * 3, both helpers have visible effects. Does multiplying second’s result make second run first?
A plan’s root is a choice selecting its right child: a sequence containing a rest then a tap. The unused left child contains three cues. How many cues execute, and in which order?

05Repair the timeline preview

Calculate the length, place the highlight, then preview alternatives

Easy corrects the looped phrase length. Medium supplies that calculation and the working tap/rest selector, then asks for the highlighted span after an optional count-in. Hard uses the same phrase and cue primitives to preview a nested plan. It reports the plan’s duration and transcript; individual span highlighting is a separate editor feature. Selection and ordering must hold inside every child of that plan.

The lab opens in your primary reading language and has its own language control. Input decoding, types and the complete caller are in each file. tick is a small integer position; T and R describe actual cue calls, not simulated elapsed time.

Repair the timeline preview

Extend the timeline preview from phrase arithmetic to cue highlights and nested alternatives.

  1. easy · F-EVALUATION-V4-1 Group the repeated phrase
  2. medium · F-EVALUATION-V4-2 Highlight the requested cue span
  3. hard · F-EVALUATION-V4-3 Preserve a nested plan’s execution
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.