t3x.org / t3x / t3x-manual / 26A.html
 
T3X - A Minimum Procedural Language
Version 8.1.5, Online Edition
Copyright(C) 1996-2004
Nils M Holm
 
Previous:
2.6.9 Constant Expressions
TOC | Index | Back Next:
2.7 Statements

2.6.10 Order of Evaluation

While the associativity and precedence rules specify which operation is to be performed first, the order of evaluation determins, which factor is to be evaluated first. For example, in the expression

A * B

A may be evaluated before B or vice versa. The order of evaluation becomes important, if both A and B have side effects. If A had the side effect of printing 'A' on the terminal screen and B would print 'B', the terminal output of above expression could be "AB" as well as "BA".

The order of evaluation is undefined in most operations, but there are exceptions: the conjunction, disjunction, and conditional operators are defined by their orders of evaluation. Therefore, the order of evaluation of an expression like

A() /\ B()

is unambiguous. The lefthand side is always evaluated first and the righthand side is only evaluated, if the value of the lefthand side is non-zero. Given the above side effects, this expression would print "AB", if A() is non-zero and "A", if it is zero. It would under no circumstances print "BA".

The other exception is the order of evaluation of procedure call arguments and nested procedure calls. Procedure call arguments are guaranteed to be evalauted from the left to the right and nested calls are evaluated inside-out. The expression

P( A(), B() )

would invariably be evaluated in the order A, B, P. Therefore, it is safe, for example, to format a string in a procedure call argument and compute the length of the formatted string in a following argument. The statement

t.write(T3X.SYSOUT, str.format(buf, "%S/.txrc", [(path)]), str.length(buf));

would print the string formatted in str.format correctly.

Notice: A T3X programmer should never rely on any order of evaluation not explicitly specified in this subsection! Even if precedence rules may suggest a specific order of evaluation, it may in fact be different and, even worse, it may change without breaking any rules, when turning optimizations on or off or using a different compiler version.

Previous:
2.6.9 Constant Expressions
TOC | Index | Back Next:
2.7 Statements