next up previous contents
Next: The precond function Up: Under the hood Previous: Advanced variables: structures   Contents


Advanced variables: compound expressions

Particularly when constructing structures in macros, it is necessary to store intermediate results in variables, before producing the final result. E.g.

#define MACRO(result) \
  $tmp.a = 10; \
  $tmp.b = 20; \
  result = $tmp;
...
MACRO($var);

Unfortunately, ``call by reference''3.1 is a concept that is not used anywhere else in the tcng language, and it would be more natural if the macro could simply return a value, so one could use it as follows:

$var = MACRO;

This is possible with the compound expressions. A compound expression consists of zero or more variable or field assignments, followed by a single expression. All this is enclosed by curly braces.

Example:

#define MACRO \
  { $tmp.a = 10; \
    $tmp.b = 20; \
    $tmp; }

The usual scoping rules related to curly braces apply.

C programmers using gcc's statement expression extension may wish to put compound expressions in parentheses, which yields virtually the same syntax.



Martin A. Brown 2003-11-06