Still more new operators (was: == vs =)

Karl Heuer karl at haddock.ISC.COM
Thu Jan 28 06:26:23 AEST 1988


In article <9998 at ccicpg.UUCP> nick at ccicpg.UUCP (Nick Crossley) writes:
>While we are inventing other types of and syntax for assignment,
>what about the [Algol68C] 'displacement' operator [":=:=", which stores
>a new value but returns the old value]?

Well, I suppose it's time for me to plug my double-comma operator again.

Some years ago I invented the hypothetical notation "e1 ,, e2", which would be
like the comma operator in that it evaluates its left operand before its
right operand, but (unlike comma) the result would be the value of the left
operand.  Look what this buys us:
	a,, a=b		/* displacement operator, like a :=:= b */
	a=(b,, b=a)	/* a simple swap */
	x,, ++x		/* same as x++; but generalizable */
	free(stack,, stack=stack->next)	/* pop stack */
	stack->value,, pop(stack)	/* pop and return stacked value */

There's some code in the terminfo routines that wants to do
  #define getshi() *((short *)ip)++
but since this is illegal on two counts, it instead uses
  #define getshi() getsh(ip); ip += 2
(this kludge depends on the fact that it is always called via "x = getshi();",
which will expand into "x = getsh(ip); ip += 2").  Given a reverse sequential
evaluation operator, this could have been written
  #define getshi() getsh(ip,, ip += 2)
or, if ip is known to be aligned,
  #define getshi() *((short *)ip,, ip += 2)
, either of which would be usable in any expression context.

Of course, the code generated for ",," is almost as simple as for ","; the
straightforward approach just needs a temporary (a scratch register, or top of
stack).  Of course, if the ultimate destination is already free, the temporary
can be optimized out: f(x,,y) becomes push(x); eval(y); call(f).

Using an HLL means never having to declare a temporary...

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list