initializing to expressions

Dave Corcoran dave at aspect.UUCP
Wed Feb 20 08:29:02 AEST 1991


In article <91039.144030BRL102 at psuvm.psu.edu>, BRL102 at psuvm.psu.edu (Ben Liblit) writes:
> I want to initialize an array with values that are constant, but are in the
> form of expressions.  For example,
> 
>    double  value[ 2 ] = { sqrt( 2 ) / 2, cos ( sqrt( 5 ) ) };
> 
> Is there any way to accomplish this?  I know I could do this in some sort of
> 

I tried mailing but it bounced.

I have been toying with the idea of obtaining a better C preprocessor,
something that would allow fairly complex manipulations of text, something
akin to an macro assembler. The marriage of m4 and perl seem to give me what
I want, though a little ugly.

include file: perlm.m
-----------------8<----------------------
define(`pe_',
	`syscmd(perl -e ' '`
	$@
	''`
)')
-----------------8<----------------------


source file: tst.c.m
-----------------8<----------------------
include(perlm.m)


/* use any perl script to generate numbers */
double x [] = {pe_(
	for $i (1..5) {
		$out=join(",",$out,cos($i),log($i));
	}
	($out=~s/,//); # note the parens; needed to overide comma
                       # as m4 arg separator
	print $out;
	)};

/* create 2 new macros in terms of perl macro */
define(cs_,`pe_(printf ("%2.2f,%2.2f",sin(cos($1)),cos($2));)')
/*                     ^--- note parens again ------------^ */

define(x,`pe_(print cos($1);)')

/* invoke them */
double y [] = {cs_(1,4),x(22)};
-----------------8<----------------------
-- 
David Corcoran		      -@@
uunet!aspect!dave	        ~
In a society where anything goes eventually everything will.



More information about the Comp.lang.c mailing list