[comp.lang.c] C macros

chris@mimsy.UUCP (Chris Torek) (04/26/89)

In article <1317@ns.network.com> ddb@ns.network.com (David Dyer-Bennet) writes:
>[deleted referent] is an example of the weakness of the macro facility
>in C (one of the most light-weight macro facilities I've ever had foisted
>on me).  Without taking a position on the general value of the comma
>operator -- the correct solution to this one is to improve the macro
>facility, not provide a way to kludge some (but not all) cases.

C's macro preprocessor is intended to be weak.  Fancier macro expanders
(such as m4) are available (although their syntaxes often clash with C's,
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 behave exactly like true functions, with the exception
that they may not be recursive, and (unless the compiler is clever) they
cannot contain static variables.  The multiple argument evaluation problem
vanishes; stdio.h could have putc defined as

	inline int putc(char c, FILE *fp) {
		/* if there is room, just stuff it in, unless \n */
		if (--fp->_count >= 0 &&
		    ((fp->_flags & _LINEBUFFERED) == 0 || c != '\n'))
			*fp->_ptr++ = c;
			return c;
		}
		/* no room, or line buffered newline */
		return _put_and_flush(c, fp);
	}
	#define putchar(c) putc(c, stdout) /* simpler as a macro */

[Yes, I know gcc has inline functions.]
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

moore%cdr.utah.edu@wasatch.utah.edu (Tim Moore) (04/26/89)

In article <17109@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
}...  What is missing from C (for operations like
}getchar and putchar) is not better macros, but rather inline functions.
}Inline functions behave exactly like true functions, with the exception
}that they may not be recursive,

Inline functions can be tail recursive.


Tim Moore                     moore@cs.utah.edu {ut-sally,hplabs}!utah-cs!moore
"Y'know, sometimes I wonder - does existence precede essence? Or what?"
		-Cherry Poptart
"`Race' is, at best, a fuzzy concept about typical physical characteristics of 

ddb@ns.network.com (David Dyer-Bennet) (04/28/89)

In article <17109@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
:In article <1317@ns.network.com> ddb@ns.network.com (David Dyer-Bennet) writes:
:>(one of the most light-weight macro facilities I've ever had foisted
:>on me).  Without taking a position on the general value of the comma
:>operator -- the correct solution to this one is to improve the macro
:>facility, not provide a way to kludge some (but not all) cases.
:
:C's macro preprocessor is intended to be weak.  Fancier macro expanders
:(such as m4) are available (although their syntaxes often clash with C's,
: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.

Yes, the correct / better solution to that particular problem is inline
functions.  I let myself follow a bad example down a dead end.  I still
think a "real" macro facility is important / very useful in and of itself,
for various things particularly including static table initialization.
(I got exposed to powerful macros in dec's MACRO-10/20, then BLISS.  Even
the stuff in MASM for a pc is pretty useful (and looks remarkably like
macro 10/20).)

-- 
David Dyer-Bennet, ddb@terrabit.fidonet.org, or ddb@ns.network.com
or ddb@Lynx.MN.Org, ...{amdahl,hpda}!bungia!viper!ddb
or ...!{rutgers!dayton | amdahl!ems | uunet!rosevax}!umn-cs!ns!ddb
or Fidonet 1:282/341.0, (612) 721-8967 9600hst/2400/1200/300

njk@freja.diku.dk (Niels J|rgen Kruse) (04/28/89)

chris@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@diku.dk
Mail     Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark

campbell@redsox.bsw.com (Larry Campbell) (04/28/89)

In article <1333@ns.network.com> ddb@ns.UUCP (David Dyer-Bennet) writes:
-
-                                                             ...I still
-think a "real" macro facility is important / very useful in and of itself,
-for various things particularly including static table initialization.
-(I got exposed to powerful macros in dec's MACRO-10/20, then BLISS.  Even
-the stuff in MASM for a pc is pretty useful (and looks remarkably like
-macro 10/20).)

I prefer the UNIX approach, which is to keep the text processing tools
separate.  The BLISS macro facility is nice (although very difficult to
learn completely), but can only be used in BLISS programs.  On UNIX, you can
do everything the BLISS macros allowed (and more) with a combination of m4,
sed, and awk, and you can use these tools with any language, not just C.
-- 
Larry Campbell                          The Boston Software Works, Inc.
campbell@bsw.com                        120 Fulton Street
wjh12!redsox!campbell                   Boston, MA 02146