Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!aspect!dave From: dave@aspect.UUCP (Dave Corcoran) Newsgroups: comp.lang.c Subject: Re: initializing to expressions Summary: yet another m4/perl solution Message-ID: <7698@aspect.UUCP> Date: 19 Feb 91 21:29:02 GMT References: <91039.144030BRL102@psuvm.psu.edu> Organization: Aspect Telecommunications, San Jose, Ca Lines: 54 In article <91039.144030BRL102@psuvm.psu.edu>, BRL102@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.