"arithmetic if":: Re: Feature for the next C version

Chris Torek chris at mimsy.UUCP
Thu Aug 3 04:03:57 AEST 1989


(Short summary: the arithemtic `if' in the three different languages
FIV, F66, and F77---usually each called `FORTRAN'---is syntactically
ugly; various suggestions suggested and promptly ignored :-) .)

Although I have described it here before, I might as well do it again;
USENET has a short collective memory.  The language Mesa (used at Xerox)
has a construct that is a generalisation of C's `switch', called
SELECT (Mesa keywords are in ALL CAPITALS, a practise I find ugly in
the extreme, but fixable :-) ).  One writes, for instance,

	SELECT TRUE FROM -- `FROM' might be `IN'; my Mesa is rusty
	   a < b => signum _ -1;	-- `_' is a left-arrow
	   a = b => signum _ 0;		-- and used for assignment.
	   a > b => signum _ 1;		-- (_ as arrow is `old ASCII')
	END;	-- actually, I have forgotten the proper syntax
		-- for ending a select, but this will serve.

Each argument to select (including the expression to be selected)
is a regular Mesa expression; the first branch that matches (is equal)
is taken.  A C `switch' is thus simply

	SELECT expr FROM
	   1 => case1[];		-- [] is function call
	   2 => case2[];
	   3 => case3[];
	END;

but by putting `true' in the select argument one can select against
various conditions.  A decent compiler will notice that the select
expression is a constant (particularly for `true' or `false') and
will generate the test(s) as the case expressions go by; it is easy
to retain the condition codes across such tests.  The `signum'
example above should compile into

		cmp	a,b
		jge	not_first_case	| skip if not less than
		mov	#-1,signum
		jmp	end_of_select
	not_first_case:
		jne	not_second_case	| skip if not equal
		mov	#0,signum
		jmp	end_of_select
	not_second_case:
		jle	end_of_select	| impossible, but who knows if
					| the compiler will figure that out.
					| one could write `TRUE' for the
					| last expression and eliminate
					| this.
		mov	#1,signum
	end_of_select:
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list