Propeller Manual

Table Of Contents
2: Spin Language Reference – Operators
...to be equal to 49; that is, 8 * 4 = 32, 6 / 2 = 3, and 20 + 32 – 3 = 49. If you wish the
expression to be evaluated differently, use parentheses to enclose the necessary portions of
the expression.
For example:
X = (20 + 8) * 4 – 6 / 2
This will evaluate the expression in parentheses first, the 20 + 8, causing the expression to
now result in 109, instead of 49.
Table 2-10 indicates each operator’s level of precedence from highest (level 0) to lowest
(level 12). Operators with a higher precedence are performed before operators of a lower
precedence; multiply before add, absolute before multiply, etc. The only exception is if
parentheses are included; they override every precedence level.
Intermediate Assignments
The Propeller chip’s expression engine allows for, and processes, assignment operators at
intermediate stages. This is called “intermediate assignments” and it can be used to perform
complex calculations in less code. For example, the following equation relies heavily on X,
and X + 1.
X := X - 3 * (X + 1) / ||(X + 1)
The same statement could be rewritten, taking advantage of the intermediate assignment
property of the increment operator:
X := X++ - 3 * X / ||X
Assuming X started out at -5, both of these statements evaluate to -2, and both store that value
in
X when done. The second statement, however, does it by relying on an intermediate
assignment (the
X++ part) in order to simplify the rest of the statement. The Increment
operator ‘
++’ is evaluated first (highest precedence) and increments X’s -5 to -4. Since this is
a “post increment” (see Increment, pre- or post- ‘+
+’, page 152) it first returns X’s original
value, -5, to the expression and then writes the new value, -4, to
X. So, the “X++ - 3…” part
of the expression becomes “-5 – 3…” Then the absolute, multiply, and divide operators are
evaluated, but the value of
X has been changed, so they use the new value, -4, for their
operations:
-5 – 3 * -4 / ||-4 -5 – 3 * -4 / 4 -5 – 3 * -1 -5 – -3 = -2
Occasionally, the use of intermediate assignments can compress multiple lines of expressions
into a single expression, resulting in slightly smaller code size and slightly faster execution.
Propeller Manual v1.1 · Page 147