Menu

Evaluations

Csaba Skrabák

Evaluation order matters a lot if the condition has side effects or the return value depends on time.
Think of the expression RND(5). It returns random value every time it's evaluated. It does matter if it is evaluated once or at every loop iteration in a FOR I=1 TO 10 STEP RND(5)

No short-circuit

See [Internals] page on the postfix Polish notation. A consequence of the postfix Polish notation is that the logical AND/OR operators can't be short-circuited:

  • Both sides of the AND are evaluated even if the left side evaluates false.
  • Both sides of the OR are evaluated even if the left side evaluates true.

READ and DATA

  • When the runner reaches a DATA instruction, it skips evaluation of the items due to the _DEFER token in the program array.
  • There is an initially empty list, read data buffer in the runner.
  • Whenever the runner reaches a READ with an empty read data buffer , evaluations happen for the next DATA instruction. All items in one DATA instruction evaluate at once. The value of each item goes to the read data buffer. The first value is taken from it, which is assigned to READ's parameter.
  • Whenever the runner reaches a READ with one or more values in the read data buffer , no evaluation happens. Only a value is taken from buffer.
    As a consequence, DATA X:DATA Y and DATA X, Y may or may not mean the same.

FOR Loops

Naming the parameters a loop instruction has: FOR {loop-counter-reference} = {counter-initializing-expression} TO {to-expression} STEP {step-expression}
When the FOR instruction is reached:

  1. {counter-initializing-expression} is evaluated (will be assigned to the loop counter)
  2. {to-expression} is evaluated (will be stored as end)
  3. {step-expression} is evaluated (will be stored as step)

When NEXT is reached:

  1. The loop counter is incremented by step.
  2. The loop counter is checked if it exceeded the end value.
  3. Staying in loop, the program counter goes to the instruction after the FOR. Exiting the loop, the program counter advances to the instruction after NEXT. The loop counter keeps its just incremented value in both cases.

Related

Wiki: Expressions
Wiki: Home
Wiki: Internals

Discussion

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB