C macros (was comma operator: keep away?)
Niels J|rgen Kruse
njk at freja.diku.dk
Fri Apr 28 09:54:08 AEST 1989
chris at mimsy.UUCP (Chris Torek) writes:
>and/or with each other's). What is missing from C (for operations like
>getchar and putchar) is not better macros, but rather inline functions.
Inline functions are missing one of the main advantages of C
macros over most other mechanisms for abstraction :
polymorphism.
What i would like to see in C is a *let expression* that would
allow me to write
#define swap(a,b) ([f = &(a), s = &(b)][temp = *f] *f = *s, *s = temp)
#define square(x) ([temp = (x)] temp * temp)
#define max(a,b) ([f = (a), s = (b)] f > s ? f : s)
and have
j = max (square (rand()),*++i);
be robust to sideeffects and have both
char *p,*q;
swap (p,q);
and
double x,y;
swap (x,y);
work as intended.
Syntax :
let-expression ::= [ let-list ] result-expression
let-list ::= let-item | let-list , let-item
let-item ::= identifier = expression
Precedence of let-expression below comma-expression
(Result-expression is any kind of expression, i just need to
distinguish it from those in the let-list)
Semantics :
Scope of the identifiers introduced in the let-list is the
result-expression. Their types and values are those of the
expression in the introducing let-item, evaluated prior to
evaluating the result-expression, ie. the ] is a sequence
point. Evaluation order among let-items is undefined. The
identifiers introduced have no location, ie. & (address
operator) can not be applied to them and they can not be
assigned to. The value and type of the let-expression is that
of the result-expression.
This language construct is borrowed from letlisp. A direct
borrow would have
let-expression ::= let let-list in result-expression
but keywords in expressions would be unlike C. A [ without an
expression in front of it is a syntax error in current C, so
the overloading is feasible.
--
Niels J|rgen Kruse
Email njk at diku.dk
Mail Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark
More information about the Comp.lang.c
mailing list