home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume41 / defunc / part01 < prev    next >
Encoding:
Text File  |  1993-12-14  |  52.2 KB  |  1,853 lines

  1. Newsgroups: comp.sources.misc
  2. From: jinke@sparky.Phy.QueensU.CA (Ke Jin)
  3. Subject: v41i032:  defunc - C library package for runtime function constructing, Part01/02
  4. Message-ID: <csm-v41i032=defunc.225802@sparky.Sterling.COM>
  5. X-Md4-Signature: a0b095960a70d23d3d63ce23a542cd0f
  6. Sender: kent@sparky.sterling.com (Kent Landfield)
  7. Organization: Sterling Software
  8. Date: Tue, 14 Dec 1993 04:58:54 GMT
  9. Approved: kent@sparky.sterling.com
  10.  
  11. Submitted-by: jinke@sparky.Phy.QueensU.CA (Ke Jin)
  12. Posting-number: Volume 41, Issue 32
  13. Archive-name: defunc/part01
  14. Environment: UNIX, yacc
  15.  
  16. This submission includes source code and documentation for defunc 1.2 
  17. (Dynamic Expressible Function Constructing). With this library, one can 
  18. construct functions from runtime inputted expression strings in a handy
  19. and totally transparent way. That is, send your string to a defunc function,
  20. the return on success is a pointer to the constructed function. The package 
  21. has been test on UNIX with sun-cc gcc(2.2.2). The yacc file has been tested 
  22. with yacc and bison.
  23.  
  24. Ke Jin 
  25. ------
  26. #! /bin/sh
  27. # This is a shell archive.  Remove anything before this line, then feed it
  28. # into a shell via "sh file" or similar.  To overwrite existing files,
  29. # type "sh file -c".
  30. # Contents:  README defunc.c dfcparse.y dfcsymtable.c dfctoken.3.UU
  31. #   dfctree.c
  32. # Wrapped by kent@sparky on Mon Dec 13 22:36:49 1993
  33. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH
  34. echo If this archive is complete, you will see the following message:
  35. echo '          "shar: End of archive 1 (of 2)."'
  36. if test -f 'README' -a "${1}" != "-c" ; then 
  37.   echo shar: Will not clobber existing file \"'README'\"
  38. else
  39.   echo shar: Extracting \"'README'\" \(1003 characters\)
  40.   sed "s/^X//" >'README' <<'END_OF_FILE'
  41. X        Copyright (c) 1993  Ke Jin
  42. X
  43. X        Permission to use, copy, modify, and distribute
  44. X        this software and its documentation without fee
  45. X        is granted, provided that the author's name and
  46. X        this copyright notice are retained.
  47. X
  48. X        This package includes 15 files of defunc version 1.2.1:
  49. X
  50. X    README        This file
  51. X    defunc.c     high level module of defunc
  52. X    defunc.h     interface to defunc.c
  53. X    dfctree.c    defunc low level module
  54. X    dfctree.h       interface to defunc.c
  55. X    dfcsymtable.c    defunc external token system
  56. X    dfcsymtable.h     interface to dfcsymtable.c
  57. X    dfcscan.h    defunc lexical scaner
  58. X    dfcparse.y    yacc file of defunc parser
  59. X    y.tab.c        generated from dfcparse.y by yacc
  60. X    Makefile    install make file
  61. X    defunc.3    manual page of defunc
  62. X    dfopen.3     manual page of dfopen() etc. functions 
  63. X    dftoken.3    manual page of defunc token system
  64. X    demo.c        example program of using defunc 
  65. X
  66. X
  67. X    Author Ke Jin.
  68. X    Physics Dept.
  69. X    Queen's Unviersity
  70. X    Kingstion Ontario
  71. X    Canada  K7L 3N6
  72. X
  73. X    (jinke@sparky.phy.queensu.ca)
  74. X
  75. END_OF_FILE
  76.   if test 1003 -ne `wc -c <'README'`; then
  77.     echo shar: \"'README'\" unpacked with wrong size!
  78.   fi
  79.   # end of 'README'
  80. fi
  81. if test -f 'defunc.c' -a "${1}" != "-c" ; then 
  82.   echo shar: Will not clobber existing file \"'defunc.c'\"
  83. else
  84.   echo shar: Extracting \"'defunc.c'\" \(14109 characters\)
  85.   sed "s/^X//" >'defunc.c' <<'END_OF_FILE'
  86. X/*********************************************************
  87. X *
  88. X *    Copyright (c) 1993  Ke Jin 
  89. X *
  90. X *    Permission to use, copy, modify, and distribute 
  91. X *    this software and its documentation without fee
  92. X *    is granted, provided that the author's name and
  93. X *    this copyright notice are retained.
  94. X * 
  95. X * -----------------------------------------------------
  96. X *
  97. X *    defunc.c -- defunc high level module 
  98. X *
  99. X *    public  function : dfopen();
  100. X *                       dfclose();
  101. X *                       dfcloseall();
  102. X *
  103. X *    private function : getfreehdl();
  104. X *                       tdfopen();
  105. X *                       getexprbody();
  106. X *                       getexprname();
  107. X *                       getexprset();
  108. X *                       isfnctexpr();
  109. X *
  110. X *                       brkbalance();
  111. X *
  112. X *                       hdl00() to hdl31();
  113. X *
  114. X *    private variable : tr_arr[];
  115. X *                     : hdl_arr[];
  116. X *
  117. X *********************************************************/
  118. X
  119. X#include <stdio.h>
  120. X#include <malloc.h>
  121. X#include <ctype.h> 
  122. X#include <string.h>
  123. X
  124. X#include "dfctree.h"
  125. X#include "dfcsymtable.h"
  126. X#include "defunc.h"
  127. X
  128. X#ifdef __cplusplus   /* for c++ */
  129. X  extern "C" {
  130. X#endif
  131. X
  132. X#define MAXHANDLE 32
  133. X
  134. Xtypedef Node* Tree;
  135. Xtypedef double (*FP)();
  136. X
  137. X#if NeedFunctionPrototypes
  138. X  static Tree   tr_arr[MAXHANDLE];  /* predeclare static member */
  139. X  static FP     hdl_arr[MAXHANDLE]; /* predeclare static member */
  140. X  static char*  getexprbody(char* inputstr);
  141. X  static char*  getexprname(char* inputstr);
  142. X  static char*  getarguset(char* inputstr, int argidx);
  143. X  static int    isfnctexpr(char* inputstr);
  144. X  static int    brkbalance(char* inputstr);
  145. X#else
  146. X  extern Tree   tr_arr[MAXHANDLE];  /* predeclare static member */
  147. X  extern FP     hdl_arr[MAXHANDLE]; /* predeclare static member */
  148. X  extern char*  getexprbody();
  149. X  extern char*  getexprname();
  150. X  extern char*  getarguset();
  151. X  extern int    isfnctexpr();
  152. X  extern int    brkbalance();
  153. X#endif /* predeclare private function */
  154. X
  155. X#if NeedFunctionPrototypes
  156. X  static int getfreehdl(void)
  157. X#else
  158. X  static int getfreehdl() 
  159. X#endif
  160. X/* return the index of free handle on success. return -1 on fail */
  161. X{
  162. X    int i;
  163. X
  164. X    for(i=0;i<MAXHANDLE;i++)
  165. X    {
  166. X    if(tr_arr[i]==(Node*)0) return i;
  167. X    }
  168. X
  169. X    return -1;    /* if no free handle */
  170. X};
  171. X
  172. X#if NeedFunctionPrototypes
  173. X  static double (*tdfopen(char* exprbody))(double x, double y)
  174. X#else
  175. X  static double (*tdfopen(exprbody))() 
  176. X  char* exprbody;
  177. X#endif
  178. X/* Only accept expression body. Return handle function or NULL */
  179. X{
  180. X    int i, size;
  181. X
  182. X    if(brkbalance(exprbody)!=0)  
  183. X    {
  184. X        exparserror = "unbalanced ( ) in expression";
  185. X    return 0;
  186. X    }
  187. X
  188. X    if((i=getfreehdl())==-1)  return 0; /* no free handle */
  189. X    
  190. X    size = exparse(exprbody);
  191. X    if(size<=0) return 0;
  192. X
  193. X    tr_arr[i] = (Node*)malloc(sizeof(Node)*size); 
  194. X    if(tr_arr[i]==0) 
  195. X    {
  196. X    exparserror = "fail to alloc memory for new parse tree";
  197. X    return 0;
  198. X    }
  199. X
  200. X    getparsetree(tr_arr[i]);
  201. X    reduce(tr_arr[i], 0);
  202. X
  203. X    return hdl_arr[i];
  204. X};
  205. X
  206. X#if NeedFunctionPrototypes
  207. X  double (*dfopen(char* expr))(double x, double y)
  208. X#else
  209. X  double (*dfopen(expr))()
  210. X  char* expr;
  211. X#endif 
  212. X/* Accept full expression as well as expression 
  213. X * body.  Return handle function or NULL */
  214. X{
  215. X    double (*fnctptr)();
  216. X
  217. X    char* exprbody;
  218. X    char* exprname;
  219. X    char  *arg1, *arg2;
  220. X
  221. X    exparserror = 0;
  222. X    if(brkbalance(expr)!=0)
  223. X    {
  224. X    exparserror = "unbalanced ( ) in expression";
  225. X    return 0;
  226. X    }
  227. X
  228. X    exprbody = getexprbody(expr);
  229. X    exprname = getexprname(expr);
  230. X    arg1     = getarguset(expr, 1);
  231. X    arg2     = getarguset(expr, 2);
  232. X
  233. X    if(nameargu(arg1, arg2)==-1)
  234. X    {
  235. X    exparserror = "error on reset argument";
  236. X    return 0;
  237. X    }
  238. X
  239. X    fnctptr = tdfopen(exprbody);
  240. X
  241. X    if(exprname!=0)
  242. X    {
  243. X    if(arg1!=0||isfnctexpr(expr)) 
  244. X    {
  245. X        namefnct(exprname, fnctptr);
  246. X        }
  247. X    else if(fnctptr!=0)
  248. X    {
  249. X        namecnst(exprname, fnctptr());
  250. X        }
  251. X    }
  252. X
  253. X    return fnctptr;
  254. X};
  255. X
  256. X#if NeedFunctionPrototypes
  257. X  int dfclose(double (*fnctptr)())
  258. X#else
  259. X  int dfclose(fnctptr)
  260. X  double (*fnctptr)();
  261. X#endif
  262. X/* close a dynamic function(getten from dfopen) if it not on
  263. X * the global name-function association table. On success,
  264. X * return 0. On fail return -1 
  265. X */
  266. X{
  267. X    int i;
  268. X    if(getsym(getfnctname(fnctptr))!=0) return -1; 
  269. X    /* function still on symbol table can't be closed */ 
  270. X
  271. X    for(i=0;i<MAXHANDLE;i++)
  272. X    {
  273. X    if(fnctptr==hdl_arr[i])
  274. X    {
  275. X        if(tr_arr[i]!=0) free(tr_arr[i]);
  276. X        tr_arr[i]=0;
  277. X        return 0; /* success */
  278. X        }
  279. X    }
  280. X
  281. X    return -1; /* fail to close */
  282. X};
  283. X
  284. X#if NeedFunctionPrototypes
  285. X  int dfcloseall(void)
  286. X#else
  287. X  int dfcloseall()
  288. X#endif
  289. X/* close all (not on the association table) dynamic functions  
  290. X * Return the total number of handler freed by this calling.
  291. X */
  292. X{
  293. X     int i, j;
  294. X
  295. X     for(i=0, j=0; i<MAXHANDLE; i++)
  296. X     {
  297. X     if(tr_arr[i]!=0)  /* unfreed handle */ 
  298. X     {
  299. X         if(dfclose(hdl_arr[i])==0) /* success */
  300. X         {
  301. X           tr_arr[i]=0;
  302. X           j++;
  303. X             }
  304. X         }
  305. X     }
  306. X
  307. X     return j;
  308. X};
  309. X
  310. X#if NeedFunctionPrototypes
  311. X   static char* getexprbody(char* str)
  312. X#else 
  313. X   static char* getexprbody(str)
  314. X   char* str;
  315. X#endif
  316. X/* extract the expression body from an input string */
  317. X{
  318. X    int i;
  319. X
  320. X    for(i=0;i<strlen(str);i++)
  321. X    {
  322. X        if(str[i]=='=') return str+i+1;
  323. X    /* found '=' in str, substring after '=' be returned */
  324. X
  325. X    }
  326. X
  327. X    return str; /* no '=' be found, then return str itself */
  328. X};
  329. X
  330. X#if NeedFunctionPrototypes
  331. X   static char* getexprname(char* str)
  332. X#else
  333. X   static char* getexprname(str)
  334. X   char* str;
  335. X#endif
  336. X/* extract the expression title from an input string */
  337. X{
  338. X    int i, j=0, len;
  339. X    static char* name;
  340. X
  341. X    for(i=0; i<strlen(str);i++)
  342. X    {
  343. X    if(str[i]=='=') break;
  344. X    /* if no '=' be found, then it's a anonymous expression */
  345. X    }
  346. X    if(i==strlen(str)) return 0;
  347. X
  348. X    len = i;
  349. X
  350. X    name = (char*)malloc(len*sizeof(char));
  351. X    if(name == 0) 
  352. X    {
  353. X    perror("malloc in getarguset()");
  354. X    exit(1);
  355. X    }
  356. X
  357. X    for(i=0; i<len; i++)
  358. X    {
  359. X        if(isalnum(str[i])) 
  360. X    {
  361. X        name[j] = str[i];
  362. X        j++;
  363. X        }
  364. X    else break;
  365. X    }
  366. X
  367. X    name[j]= '\0';
  368. X    if(strlen(name)==0) return 0;
  369. X    return name;
  370. X};
  371. X
  372. X#if NeedFunctionPrototypes
  373. X    static char* getarguset(char* str, int argidx)
  374. X#else
  375. X    static char* getarguset(str, argidx)
  376. X    char* str;
  377. X    int   argidx;
  378. X#endif
  379. X/* extract an argument name from input string */
  380. X{
  381. X    int i, j=0, len;
  382. X    char   c;
  383. X    static char *name1;
  384. X    static char *name2;
  385. X
  386. X    for(i=0; i<strlen(str); i++)
  387. X    {
  388. X    if(str[i]=='=') break;
  389. X    /* if no '=' be found, then it's a anonymous expression */
  390. X    }
  391. X    if(i==strlen(str)) return 0;
  392. X    len=i; 
  393. X    /* len is the length of substring (those part in front of '=') */
  394. X
  395. X    for(i=0; i<len; i++)
  396. X    {
  397. X    if(argidx==1)  
  398. X        {
  399. X        name1 = (char*)malloc(len*sizeof(char));
  400. X        if(name1 == 0)
  401. X        {
  402. X            perror("malloc in getarguset()");
  403. X                exit(1);
  404. X            }
  405. X
  406. X        if(str[i] == '(')     /* skim over the expr name */
  407. X        {
  408. X            for(i=i+1;i<len;i++)
  409. X            {
  410. X            c = str[i];
  411. X            if(j==0&&(c==' '||c=='\t')) continue;
  412. X                if(c!=','&&c!=')'&&c!='='&&c!=' '&&c!='\t') 
  413. X            { 
  414. X                name1[j]=c;
  415. X                j++;
  416. X                    }
  417. X            else break;
  418. X                }
  419. X        name1[j]='\0';
  420. X        if(strlen(name1)==0) return 0;
  421. X        return name1; 
  422. X            }
  423. X        }
  424. X
  425. X    if(argidx==2)
  426. X    {
  427. X        name2 = (char*)malloc(len*sizeof(char));
  428. X        if(name2 == 0)
  429. X            {
  430. X            perror("malloc in getarguset()");
  431. X        exit(1);
  432. X            }    
  433. X
  434. X        if(str[i] == ',')     
  435. X              /* skim over the expr and 1st argu names */
  436. X            {
  437. X        for(i=i+1;i<len;i++)
  438. X        {
  439. X            c=str[i];
  440. X            if(j==0&&(c==' '||c=='\t')) continue;
  441. X            if(c!=','&&c!=')'&&c!='='&&c!=' '&&c!='\t')
  442. X            {
  443. X            name2[j]=str[i];
  444. X            j++;
  445. X                    }
  446. X            else break; 
  447. X                }
  448. X        name2[j]='\0';
  449. X        if(strlen(name2)==0) return 0;
  450. X        return name2;
  451. X            }
  452. X        }
  453. X    }
  454. X
  455. X    return 0;
  456. X};
  457. X
  458. X#if NeedFunctionPrototypes
  459. X  static int isfnctexpr(char* expression)
  460. X#else
  461. X  static int isfnctexpr(expression)
  462. X  char* expression;
  463. X#endif
  464. X/* to see the title is in "name(...)=" form or in "name=" form */
  465. X{
  466. X    int i, tag=0;
  467. X    char c;
  468. X
  469. X    if(expression==0) return 0;
  470. X
  471. X    for(i=0; i<strlen(expression); i++)
  472. X    {
  473. X    c=expression[i];
  474. X    if(c=='(') tag=1;
  475. X    if(c=='=') return tag;
  476. X    }
  477. X
  478. X    return tag;
  479. X};
  480. X
  481. X#if NeedFunctionPrototypes
  482. X  static int brkbalance(char* expression) 
  483. X#else
  484. X  static int brkbalance(expression)
  485. X  char* expression;
  486. X#endif
  487. X/* check the balance of '(' and ')'. Return 0 on balance */
  488. X{
  489. X    int  i,j;
  490. X    char c;
  491. X
  492. X    for(i=0, j=0;i<strlen(expression);i++)
  493. X    {
  494. X    c = expression[i];
  495. X    if(c=='(') j++;
  496. X    if(c==')') j--;
  497. X    }
  498. X
  499. X    return j;
  500. X};
  501. X        
  502. X/* ------------------------- private members  ------------------------ */
  503. Xstatic  Tree   tr_arr[MAXHANDLE];
  504. X
  505. X#if NeedFunctionPrototypes
  506. X static double hdl00(double x,double y){return evaluate(tr_arr[ 0],0,x,y);};
  507. X static double hdl01(double x,double y){return evaluate(tr_arr[ 1],0,x,y);};
  508. X static double hdl02(double x,double y){return evaluate(tr_arr[ 2],0,x,y);};
  509. X static double hdl03(double x,double y){return evaluate(tr_arr[ 3],0,x,y);};
  510. X static double hdl04(double x,double y){return evaluate(tr_arr[ 4],0,x,y);};
  511. X static double hdl05(double x,double y){return evaluate(tr_arr[ 5],0,x,y);};
  512. X static double hdl06(double x,double y){return evaluate(tr_arr[ 6],0,x,y);};
  513. X static double hdl07(double x,double y){return evaluate(tr_arr[ 7],0,x,y);};
  514. X static double hdl08(double x,double y){return evaluate(tr_arr[ 8],0,x,y);};
  515. X static double hdl09(double x,double y){return evaluate(tr_arr[ 9],0,x,y);};
  516. X static double hdl10(double x,double y){return evaluate(tr_arr[10],0,x,y);};
  517. X static double hdl11(double x,double y){return evaluate(tr_arr[11],0,x,y);};
  518. X static double hdl12(double x,double y){return evaluate(tr_arr[12],0,x,y);};
  519. X static double hdl13(double x,double y){return evaluate(tr_arr[13],0,x,y);};
  520. X static double hdl14(double x,double y){return evaluate(tr_arr[14],0,x,y);};
  521. X static double hdl15(double x,double y){return evaluate(tr_arr[15],0,x,y);};
  522. X static double hdl16(double x,double y){return evaluate(tr_arr[16],0,x,y);};
  523. X static double hdl17(double x,double y){return evaluate(tr_arr[17],0,x,y);};
  524. X static double hdl18(double x,double y){return evaluate(tr_arr[18],0,x,y);};
  525. X static double hdl19(double x,double y){return evaluate(tr_arr[19],0,x,y);};
  526. X static double hdl20(double x,double y){return evaluate(tr_arr[20],0,x,y);};
  527. X static double hdl21(double x,double y){return evaluate(tr_arr[21],0,x,y);};
  528. X static double hdl22(double x,double y){return evaluate(tr_arr[22],0,x,y);};
  529. X static double hdl23(double x,double y){return evaluate(tr_arr[23],0,x,y);};
  530. X static double hdl24(double x,double y){return evaluate(tr_arr[24],0,x,y);};
  531. X static double hdl25(double x,double y){return evaluate(tr_arr[25],0,x,y);};
  532. X static double hdl26(double x,double y){return evaluate(tr_arr[26],0,x,y);};
  533. X static double hdl27(double x,double y){return evaluate(tr_arr[27],0,x,y);};
  534. X static double hdl28(double x,double y){return evaluate(tr_arr[28],0,x,y);};
  535. X static double hdl29(double x,double y){return evaluate(tr_arr[29],0,x,y);};
  536. X static double hdl30(double x,double y){return evaluate(tr_arr[30],0,x,y);};
  537. X static double hdl31(double x,double y){return evaluate(tr_arr[31],0,x,y);};
  538. X#else
  539. X static double hdl00(x,y) double x,y; {return evaluate(tr_arr[ 0],0,x,y);};
  540. X static double hdl01(x,y) double x,y; {return evaluate(tr_arr[ 1],0,x,y);};
  541. X static double hdl02(x,y) double x,y; {return evaluate(tr_arr[ 2],0,x,y);};
  542. X static double hdl03(x,y) double x,y; {return evaluate(tr_arr[ 3],0,x,y);};
  543. X static double hdl04(x,y) double x,y; {return evaluate(tr_arr[ 4],0,x,y);};
  544. X static double hdl05(x,y) double x,y; {return evaluate(tr_arr[ 5],0,x,y);};
  545. X static double hdl06(x,y) double x,y; {return evaluate(tr_arr[ 6],0,x,y);};
  546. X static double hdl07(x,y) double x,y; {return evaluate(tr_arr[ 7],0,x,y);};
  547. X static double hdl08(x,y) double x,y; {return evaluate(tr_arr[ 8],0,x,y);};
  548. X static double hdl09(x,y) double x,y; {return evaluate(tr_arr[ 9],0,x,y);};
  549. X static double hdl10(x,y) double x,y; {return evaluate(tr_arr[10],0,x,y);};
  550. X static double hdl11(x,y) double x,y; {return evaluate(tr_arr[11],0,x,y);};
  551. X static double hdl12(x,y) double x,y; {return evaluate(tr_arr[12],0,x,y);};
  552. X static double hdl13(x,y) double x,y; {return evaluate(tr_arr[13],0,x,y);};
  553. X static double hdl14(x,y) double x,y; {return evaluate(tr_arr[14],0,x,y);};
  554. X static double hdl15(x,y) double x,y; {return evaluate(tr_arr[15],0,x,y);};
  555. X static double hdl16(x,y) double x,y; {return evaluate(tr_arr[16],0,x,y);};
  556. X static double hdl17(x,y) double x,y; {return evaluate(tr_arr[17],0,x,y);};
  557. X static double hdl18(x,y) double x,y; {return evaluate(tr_arr[18],0,x,y);};
  558. X static double hdl19(x,y) double x,y; {return evaluate(tr_arr[19],0,x,y);};
  559. X static double hdl20(x,y) double x,y; {return evaluate(tr_arr[20],0,x,y);};
  560. X static double hdl21(x,y) double x,y; {return evaluate(tr_arr[21],0,x,y);};
  561. X static double hdl22(x,y) double x,y; {return evaluate(tr_arr[22],0,x,y);};
  562. X static double hdl23(x,y) double x,y; {return evaluate(tr_arr[23],0,x,y);};
  563. X static double hdl24(x,y) double x,y; {return evaluate(tr_arr[24],0,x,y);};
  564. X static double hdl25(x,y) double x,y; {return evaluate(tr_arr[25],0,x,y);};
  565. X static double hdl26(x,y) double x,y; {return evaluate(tr_arr[26],0,x,y);};
  566. X static double hdl27(x,y) double x,y; {return evaluate(tr_arr[27],0,x,y);};
  567. X static double hdl28(x,y) double x,y; {return evaluate(tr_arr[28],0,x,y);};
  568. X static double hdl29(x,y) double x,y; {return evaluate(tr_arr[29],0,x,y);};
  569. X static double hdl30(x,y) double x,y; {return evaluate(tr_arr[30],0,x,y);};
  570. X static double hdl31(x,y) double x,y; {return evaluate(tr_arr[31],0,x,y);}; 
  571. X#endif
  572. X
  573. Xstatic FP     hdl_arr[MAXHANDLE] ={ 
  574. X              hdl00, hdl01, hdl02, hdl03, hdl04, hdl05, hdl06, hdl07, 
  575. X              hdl08, hdl09, hdl10, hdl11, hdl12, hdl13, hdl14, hdl15, 
  576. X              hdl16, hdl17, hdl18, hdl19, hdl20, hdl21, hdl22, hdl23,
  577. X              hdl24, hdl25, hdl26, hdl27, hdl28, hdl29, hdl30, hdl31 };
  578. X
  579. X#ifdef __cplusplus
  580. X  }        /* end for c++ */
  581. X#endif
  582. END_OF_FILE
  583.   if test 14109 -ne `wc -c <'defunc.c'`; then
  584.     echo shar: \"'defunc.c'\" unpacked with wrong size!
  585.   fi
  586.   # end of 'defunc.c'
  587. fi
  588. if test -f 'dfcparse.y' -a "${1}" != "-c" ; then 
  589.   echo shar: Will not clobber existing file \"'dfcparse.y'\"
  590. else
  591.   echo shar: Extracting \"'dfcparse.y'\" \(8520 characters\)
  592.   sed "s/^X//" >'dfcparse.y' <<'END_OF_FILE'
  593. X/****************************************************************
  594. X *
  595. X *    Copyright (c) 1993  Ke Jin
  596. X *
  597. X *    Permission to use, copy, modify, and distribute
  598. X *    this software and its documentation without fee
  599. X *    is granted, provided that the author's name and
  600. X *    this copyright notice are retained.
  601. X *
  602. X * ------------------------------------------------------------
  603. X *
  604. X *   dfcparse.y -- the yacc file of defunc parser 
  605. X *
  606. X *   public  function : yyparse();
  607. X *                      yyinit();
  608. X *                      getparsetree();
  609. X *
  610. X *   public  variable : exparserror;
  611. X *  
  612. X *   private function : addnode();
  613. X *                      yyreverse();
  614. X *                      yyerror();
  615. X *                      yywrap();
  616. X *
  617. X *   private variable : yyparsetree;
  618. X *                      yytreesize;
  619. X *                      newnode;
  620. X *
  621. X ****************************************************************/
  622. X
  623. X%{
  624. X#include <stdio.h>
  625. X#include <malloc.h>
  626. X#include <math.h>
  627. X#include <string.h>
  628. X#include "dfctree.h"
  629. X#include "dfcsymtable.h"
  630. X
  631. Xchar*         exparserror;
  632. Xstatic char   ermsgbuff[128];
  633. Xstatic Node*  yyparsetree; 
  634. Xstatic int    yytreesize;
  635. Xstatic Node   newnode;
  636. X
  637. X#if NeedFunctionPrototypes
  638. X  int yyparse(void);
  639. X  static int yyreverse(void);
  640. X#else
  641. X  extern int yyparse();
  642. X  extern int yyreverse();
  643. X#endif
  644. X
  645. X#if NeedFunctionPrototypes
  646. X  static int addnode(Node *ptr)
  647. X#else
  648. X  static int addnode(ptr)
  649. X  Node *ptr;
  650. X#endif
  651. X{
  652. X    yytreesize++;
  653. X
  654. X    if(yytreesize==1)
  655. X    {
  656. X        yyparsetree = (Node*)malloc(sizeof(Node));
  657. X    }
  658. X    else
  659. X    {
  660. X        yyparsetree = (Node*)realloc((Node*)yyparsetree, 
  661. X                                 yytreesize*sizeof(Node));
  662. X    }
  663. X    if(yyparsetree==0)
  664. X    {
  665. X    perror("malloc/realloc in addnode()");
  666. X    exit(1);
  667. X    }
  668. X
  669. X    if(yyparsetree == 0) 
  670. X    {
  671. X        fprintf(stderr, "fail to allocate memory in add node\n");
  672. X        exit(1);
  673. X    }
  674. X
  675. X    /* yyparsetree[yytreesize-1] = *ptr; */
  676. X    memcpy(yyparsetree+yytreesize-1, ptr, sizeof(Node)); 
  677. X
  678. X    return yytreesize-1;
  679. X};
  680. X
  681. X%}
  682. X
  683. X%union {
  684. X    int    nodeidx;
  685. X    int    argidx;
  686. X    double value;
  687. X    double (*fnctptr)();
  688. X    char   name[32];
  689. X}
  690. X
  691. X%token <value>   CONST  /* constant              */
  692. X%token <argidx>  ARG    /* function arguments    */
  693. X%token <fnctptr> FNCT   /* intrinsic function    */
  694. X%token <name>    SYM    /* new symbol            */
  695. X%type  <nodeidx> expr   /* function expression   */
  696. X
  697. X%left  '-' '+'
  698. X%left  '*' '/'
  699. X%left  SIG
  700. X%right '^'
  701. X
  702. X%% /*  ------------------- syntax rules ------------------------ */ 
  703. Xinput : '\n'               {  
  704. X                  return 0;
  705. X                } 
  706. X      | ';'                {
  707. X                  return 0;
  708. X                           }
  709. X      | expr '\n'          {  
  710. X                              return yyreverse(); 
  711. X                           } 
  712. X      | expr ';'           {
  713. X                  return yyreverse();
  714. X                           }
  715. X      | error '\n'         {  
  716. X                              return -1;
  717. X                           } 
  718. X      ;
  719. X
  720. Xexpr  : CONST              {  
  721. X                              newnode.type = const_node;
  722. X                              newnode.content.value = $1;
  723. X
  724. X                              $$ = addnode(&newnode); 
  725. X                           }
  726. X      | SYM                {
  727. X                  sprintf(ermsgbuff, 
  728. X                  "unknow token \"%s\"", $1);
  729. X                  exparserror = ermsgbuff;
  730. X                  return -1;
  731. X                           }
  732. X      | CONST '(' ')'      {
  733. X                  newnode.type = const_node;
  734. X                  newnode.content.value = $1;
  735. X
  736. X                  $$ = addnode(&newnode);
  737. X                           }
  738. X      | CONST '(' expr ')' {
  739. X                  newnode.type = const_node;
  740. X                  newnode.content.value = $1;
  741. X
  742. X                  $$ = addnode(&newnode);
  743. X               }
  744. X      | ARG                {
  745. X                              newnode.type = arg_node;
  746. X                  newnode.content.argidx = $1;
  747. X                              $$ = addnode(&newnode);
  748. X                           }
  749. X      | FNCT '(' expr ')'  { 
  750. X                              newnode.type = simplex_fnct_node;
  751. X                              newnode.content.fnctptr = $1;
  752. X                              newnode.right = $3;
  753. X
  754. X                              $$ = addnode(&newnode);
  755. X                           }
  756. X      | FNCT '(' expr ',' expr ')' {
  757. X                              newnode.type = duplex_fnct_node;
  758. X                              newnode.content.fnctptr = $1;
  759. X                              newnode.left = $3;
  760. X                              newnode.right= $5;
  761. X
  762. X                              $$ = addnode(&newnode);
  763. X                           }
  764. X      | FNCT '(' expr ',' expr ',' expr ')' {
  765. X                  exparserror 
  766. X                = "not support triplex function yet";
  767. X                      return -1;    
  768. X                       } 
  769. X      | expr '+' expr      {
  770. X                              newnode.type = binary_op_node;
  771. X                              newnode.content.op = op_sum;
  772. X                              newnode.left = $1;
  773. X                              newnode.right= $3;
  774. X
  775. X                              $$ = addnode(&newnode);
  776. X                           }
  777. X      | expr '-' expr      {
  778. X                              newnode.type = binary_op_node;
  779. X                              newnode.content.op = op_sub;
  780. X                              newnode.left = $1;
  781. X                              newnode.right= $3;
  782. X
  783. X                              $$ = addnode(&newnode);
  784. X                           }
  785. X      | expr '*' expr      {
  786. X                              newnode.type = binary_op_node;
  787. X                              newnode.content.op = op_mul;
  788. X                              newnode.left = $1;
  789. X                              newnode.right= $3;
  790. X
  791. X                              $$ = addnode(&newnode);
  792. X                           }
  793. X      | expr '/' expr      {
  794. X                              newnode.type = binary_op_node;
  795. X                              newnode.content.op = op_div;
  796. X                              newnode.left =$1;
  797. X                              newnode.right=$3;
  798. X
  799. X                              $$ = addnode(&newnode); 
  800. X                           }
  801. X      | '-' expr %prec SIG {
  802. X                              newnode.type = unary_op_node; 
  803. X                  newnode.content.op = op_neg;
  804. X                              newnode.right = $2;
  805. X
  806. X                              $$ = addnode(&newnode);
  807. X                           }
  808. X      | '+' expr %prec SIG { 
  809. X                              $$ = $2;
  810. X                           } 
  811. X      | expr '^' expr      {
  812. X                              newnode.type = duplex_fnct_node;
  813. X                              newnode.content.fnctptr = pow;
  814. X                              newnode.left = $1;
  815. X                              newnode.right= $3;
  816. X
  817. X                              $$ = addnode(&newnode);
  818. X                           } 
  819. X      | '(' expr ')'       { 
  820. X                              $$ = $2; 
  821. X                           }
  822. X      ;
  823. X%% /* --------------------------------------------------------- */
  824. X#include "dfcscan.h"
  825. X
  826. X#if NeedFunctionPrototype
  827. X  int yyinit(char* expr)
  828. X#else
  829. X  int yyinit(expr)
  830. X  char *expr; 
  831. X#endif
  832. X{
  833. X    initargu();
  834. X
  835. X    yyexpr = expr;
  836. X    yyexprlen = strlen(yyexpr);
  837. X    yypos = 0;
  838. X    yytreesize = 0;
  839. X
  840. X    return 0;
  841. X};
  842. X
  843. X#if NeedFunctionPrototypes
  844. X  static int yyreverse(void)
  845. X#else
  846. X  static int yyreverse()  
  847. X#endif
  848. X/* yyparse() use a LALR(1) bottom-up algorithem to construct the
  849. X   parse tree. Thus the result tree is upsetdown, i.e. the root
  850. X   is place on the end of the yyparsetree[]. yyreverse make it
  851. X   in right order, i.e. yyparsetree[0] be the root */
  852. X{
  853. X    int   i;
  854. X    Node* buff;
  855. X
  856. X    if(yytreesize==0) return 0;
  857. X
  858. X    buff = (Node*)malloc(sizeof(Node)*yytreesize);
  859. X    if(buff==0) 
  860. X    {
  861. X    perror("malloc in reverse()");
  862. X    exit(1);
  863. X    }
  864. X
  865. X    for(i=0; i<yytreesize; i++) /* reverse */
  866. X    {
  867. X    
  868. X     /* buff[i] = yyparsetree[yytreesize-1-i]; */
  869. X    memcpy(buff+i, yyparsetree+yytreesize-1-i, sizeof(Node));
  870. X    buff[i].left = yytreesize - 1 - buff[i].left;
  871. X    buff[i].right= yytreesize - 1 - buff[i].right;
  872. X
  873. X    }
  874. X
  875. X    for(i=0; i<yytreesize; i++) /* put it back */ 
  876. X    {
  877. X    yyparsetree[i] = buff[i];
  878. X    }
  879. X
  880. X    free(buff);
  881. X
  882. X    return yytreesize;
  883. X};
  884. X
  885. X#if NeedFunctionPrototypes
  886. X  int getparsetree(Node* buff)
  887. X#else
  888. X  int getparsetree(buff)
  889. X  Node* buff;
  890. X#endif
  891. X/* copy the parse into buff */
  892. X{
  893. X    int i;
  894. X
  895. X    for(i=0; i<yytreesize; i++)
  896. X    {
  897. X    buff[i] = yyparsetree[i];
  898. X    }
  899. X
  900. X    return yytreesize;
  901. X};
  902. X
  903. X#if NeedFunctionPrototypes
  904. X  static void yyerror(char* s)
  905. X#else
  906. X  static yyerror(s)
  907. X  char *s;
  908. X#endif
  909. X{
  910. X    exparserror=s;
  911. X};
  912. X
  913. X#if NeedFunctionPrototypes
  914. X  static int yywrap(void)
  915. X#else
  916. X  static int yywrap()
  917. X#endif
  918. X{
  919. X   return 1;
  920. X};
  921. END_OF_FILE
  922.   if test 8520 -ne `wc -c <'dfcparse.y'`; then
  923.     echo shar: \"'dfcparse.y'\" unpacked with wrong size!
  924.   fi
  925.   # end of 'dfcparse.y'
  926. fi
  927. if test -f 'dfcsymtable.c' -a "${1}" != "-c" ; then 
  928.   echo shar: Will not clobber existing file \"'dfcsymtable.c'\"
  929. else
  930.   echo shar: Extracting \"'dfcsymtable.c'\" \(11913 characters\)
  931.   sed "s/^X//" >'dfcsymtable.c' <<'END_OF_FILE'
  932. X/*********************************************************
  933. X *
  934. X *    Copyright (c) 1993  Ke Jin
  935. X *
  936. X *    Permission to use, copy, modify, and distribute
  937. X *    this software and its documentation without fee
  938. X *    is granted, provided that the author's name and
  939. X *    this copyright notice are retained.
  940. X *
  941. X * -----------------------------------------------------
  942. X *
  943. X *    dfcsymtable.c -- symbol table of defunc
  944. X *
  945. X *    public  function : getsym(); 
  946. X *                       getfnctname();
  947. X *                       getarguname();
  948. X *
  949. X *                       nameargu();
  950. X *                       initargu();
  951. X *                       namefnct();
  952. X *                       namecnst();
  953. X *                       clrfnct();
  954. X *                       clrfnctall();
  955. X *                       clrcnst();
  956. X *                       clrcnstall();
  957. X *
  958. X *                       matha2z();  
  959. X *
  960. X *    private variable : sym_table;
  961. X *                       tablen;
  962. X *
  963. X *    private function : top();
  964. X *                       isname();
  965. X *                       clrname();
  966. X *                       clrall();
  967. X *
  968. X *********************************************************/
  969. X
  970. X#include <stdio.h>
  971. X#include <malloc.h>
  972. X#include <string.h>
  973. X#include <math.h>
  974. X#include <ctype.h>
  975. X#include "dfcsymtable.h"
  976. X
  977. X#ifdef __cplusplus
  978. X  extern "C" {   /* for c++ */
  979. X#endif
  980. X
  981. Xstatic Symbol_record* sym_table = 0; 
  982. X/* The defunc system global name-object association table */
  983. X
  984. X#if NeedFunctionPrototypes
  985. X  Symbol_record* getsym(char *name) 
  986. X#else
  987. X  Symbol_record* getsym(name)   
  988. X  char *name;
  989. X#endif
  990. X/* search for record with the given name from sym_table */
  991. X{
  992. X    Symbol_record* ptr;
  993. X
  994. X    if(name==0) return 0;
  995. X
  996. X    for(ptr=sym_table; ptr!=0; ptr=(Symbol_record*)ptr->next)
  997. X    {
  998. X       if(strcmp(ptr->name, name)==0) break;
  999. X    }
  1000. X
  1001. X    return ptr;  
  1002. X       /* if symbol with name not in sym_table, return will be NULL */
  1003. X};
  1004. X
  1005. X#if NeedFunctionPrototypes
  1006. X  char* getfnctname(double (*fnct)())
  1007. X#else
  1008. X  char* getfnctname(fnct)
  1009. X  double (*fnct)();
  1010. X#endif
  1011. X{
  1012. X    Symbol_record* ptr;
  1013. X
  1014. X    for(ptr=sym_table;ptr!=0;ptr=ptr->next)
  1015. X    {
  1016. X        if(ptr->type==fnct_symbol)
  1017. X        {
  1018. X            if(fnct==ptr->content.fnctptr) return ptr->name;
  1019. X        }
  1020. X    }
  1021. X            
  1022. X    return 0;
  1023. X};
  1024. X
  1025. X#if NeedFunctionPrototypes
  1026. X  char* getarguname(int argidx)
  1027. X#else
  1028. X  char* getarguname(argidx)
  1029. X  int argidx;
  1030. X#endif
  1031. X/* return the name of the argument with specific index */ 
  1032. X{
  1033. X    Symbol_record* ptr;
  1034. X
  1035. X    for(ptr=sym_table; ptr!=0; ptr=ptr->next)
  1036. X    {
  1037. X        if(ptr->type==arg_symbol)
  1038. X        {
  1039. X            if(argidx==ptr->content.argidx) return ptr->name;
  1040. X        }    
  1041. X    }
  1042. X
  1043. X    return 0;
  1044. X};
  1045. X
  1046. X#if NeedFunctionPrototypes
  1047. X  static Symbol_record* top(void)
  1048. X#else
  1049. X  static Symbol_record* top() 
  1050. X#endif
  1051. X/* return the top symbol record in the sym_table heap */
  1052. X{
  1053. X    Symbol_record* ptr;
  1054. X
  1055. X    for(ptr=sym_table; ptr->next!=0; ptr=(Symbol_record*)ptr->next) 
  1056. X    { 
  1057. X    /* skim over the table, do nothing on it */ 
  1058. X    };
  1059. X
  1060. X    return ptr;
  1061. X};
  1062. X
  1063. X#if NeedFunctionPrototypes
  1064. X  static int isname(char* str)
  1065. X#else
  1066. X  static int isname(str)
  1067. X  char *str;
  1068. X#endif
  1069. X/* Is the string str a legal name? (start with alphabetic character
  1070. X * followed by alphabetics or numberical characters) */
  1071. X{
  1072. X    int i;
  1073. X
  1074. X    if(str==0||strlen(str)==0) return 0;
  1075. X
  1076. X    if(!isalpha(str[0])) return 0; /* fail */
  1077. X
  1078. X    for(i=1;i<strlen(str);i++)
  1079. X    {
  1080. X    if(!isalnum(str[i])) return 0;  /* fail */
  1081. X    }
  1082. X
  1083. X    return 1; /* true */
  1084. X};
  1085. X
  1086. X#if NeedFunctionPrototypes
  1087. X  static int clrname(char* name, Symbol_type type)
  1088. X#else
  1089. X  static int clrname(name, type)
  1090. X  char* name;
  1091. X  Symbol_type type;
  1092. X#endif
  1093. X/* delete a specific name with given type from symbol table */
  1094. X{
  1095. X    Symbol_record *ptr=sym_table, *newnext;
  1096. X
  1097. X    if(sym_table==0) return 0;    /* empty table, no delete action */
  1098. X
  1099. X    if(strcmp(sym_table->name, name)==0&&ptr->type==type)
  1100. X    /* name and type matched with the 1st record */
  1101. X    {
  1102. X    ptr = sym_table;
  1103. X    sym_table = sym_table->next;     /* cut out the 1st record */
  1104. X    free(ptr->name);           
  1105. X    free(ptr);
  1106. X    return 1;
  1107. X    }
  1108. X
  1109. X    for(ptr=sym_table; ptr->next!=0; ptr=(Symbol_record*)ptr->next)
  1110. X    /* skim through the table */
  1111. X    {
  1112. X    if(strcmp(ptr->next->name, name)==0&&ptr->next->type==type)
  1113. X    /* name and type matched with the next record */
  1114. X    {
  1115. X        newnext=ptr->next->next; /* cut out the next record */
  1116. X        free(ptr->next->name);
  1117. X        free(ptr->next);
  1118. X        ptr->next=newnext;
  1119. X        return 1;
  1120. X        }
  1121. X    }
  1122. X
  1123. X    return 0;  /* no matching */
  1124. X};
  1125. X
  1126. X#if NeedFunctionPrototypes
  1127. X  int clrfnct(char* name)
  1128. X      { return clrname(name, fnct_symbol);};
  1129. X  int clrcnst(char* name)
  1130. X      { return clrname(name, const_symbol);};
  1131. X#else
  1132. X  int clrfnct(name) char* name;
  1133. X      { return clrname(name, fnct_symbol);};
  1134. X  int clrcnst(name) char* name; 
  1135. X      { return clrname(name, const_symbol);};
  1136. X#endif
  1137. X
  1138. X#if NeedFunctionPrototypes
  1139. X  static int clrall(Symbol_type type)
  1140. X#else
  1141. X  static int clrall(type)
  1142. X  Symbol_type type;
  1143. X#endif
  1144. X/* delete all symbols with specific type from symbol table */
  1145. X{
  1146. X    Symbol_record* ptr, *newnext;
  1147. X    int i=0; 
  1148. X
  1149. X    if(sym_table==0) return 0;   /* empty, no delete action */
  1150. X
  1151. X    for(ptr=sym_table;sym_table!=0;ptr=sym_table)
  1152. X                /* always point to 1st record */
  1153. X    {
  1154. X        if(sym_table->type==type)    /* type matched with the 1st record */
  1155. X        {
  1156. X        sym_table=sym_table->next; /* cut out the 1st record */
  1157. X        free(ptr->name);
  1158. X        free(ptr);
  1159. X        i++;
  1160. X        }
  1161. X    else break;   /* if new 1st record not match, jump out */
  1162. X    }
  1163. X
  1164. X    if(sym_table == 0) return i;  /* still have record ? */
  1165. X
  1166. X    for(ptr=sym_table; ptr->next!=0; ptr=(Symbol_record*)ptr->next)
  1167. X    /* skim through the table */
  1168. X    {
  1169. X    if(ptr->next->type==type) /* type matched with next record */
  1170. X    {
  1171. X        newnext = ptr->next->next;  /* cut out the next record */
  1172. X        free(ptr->next->name);
  1173. X        free(ptr->next);
  1174. X        ptr->next = newnext;
  1175. X        i++;
  1176. X        }
  1177. X    }
  1178. X
  1179. X    return i;
  1180. X};
  1181. X
  1182. X#if NeedFunctionPrototypes
  1183. X  int clrfnctall(void)
  1184. X      { return clrall(fnct_symbol);};
  1185. X  int clrcnstall(void)
  1186. X      { return clrall(const_symbol);};
  1187. X#else
  1188. X  int clrfnctall()
  1189. X      { return clrall(fnct_symbol);};
  1190. X  int clrcnstall()
  1191. X      { return clrall(const_symbol);};
  1192. X#endif
  1193. X
  1194. X#if NeedFunctionPrototypes
  1195. X  int namefnct(char* str, double (*fnctptr)())
  1196. X#else
  1197. X  int namefnct(str, fnctptr)   
  1198. X  char *str;
  1199. X  double (*fnctptr)();
  1200. X#endif
  1201. X/* add a function symbol to the end of sym_table */
  1202. X{
  1203. X    Symbol_record* ptr=0; 
  1204. X
  1205. X    if(!isname(str)||fnctptr==0) return 0;
  1206. X
  1207. X    if(sym_table==0) /* table is empty */
  1208. X    {
  1209. X    if(nameargu("x", "y")<=0) return -1;
  1210. X    }
  1211. X
  1212. X    ptr = getsym(str);
  1213. X
  1214. X    if(ptr==0) /* symbol not exist in sym_table */
  1215. X    {
  1216. X        ptr = top()->next  
  1217. X            = (Symbol_record*)malloc(sizeof(Symbol_record));
  1218. X        if(ptr==0)
  1219. X    {
  1220. X        perror("malloc for new function token item");
  1221. X        exit(1);
  1222. X        }
  1223. X    }
  1224. X    else if(ptr->type==arg_symbol)
  1225. X    {
  1226. X        return -1;  /* forbiding override argument symbols */
  1227. X    }
  1228. X
  1229. X    ptr->name = (char*)malloc((strlen(str)+1)*sizeof(char));
  1230. X    if(ptr->name==0)
  1231. X    {
  1232. X    perror("malloc for new function token name");
  1233. X    exit(1);
  1234. X    }
  1235. X    
  1236. X    strncpy(ptr->name, str, strlen(str)+1);
  1237. X    ptr->type = fnct_symbol;
  1238. X    ptr->content.fnctptr = fnctptr;
  1239. X
  1240. X    return 1;
  1241. X};
  1242. X
  1243. X#if NeedFunctionPrototypes
  1244. X  int namecnst(char* str, double number)
  1245. X#else
  1246. X  int namecnst(str, number)  
  1247. X  char *str;
  1248. X  double number;
  1249. X#endif
  1250. X/* add a constnat symbol to the end of sym_table */
  1251. X{
  1252. X    Symbol_record* ptr=0;
  1253. X
  1254. X    if(!isname(str)) return 0;
  1255. X
  1256. X    if(sym_table==0) /* table is empty */
  1257. X    {
  1258. X    if(nameargu("x", "y")<=0) return -1;
  1259. X    } 
  1260. X
  1261. X    ptr = getsym(str);
  1262. X
  1263. X    if(ptr==0) /* symbol not exist in sym_table */
  1264. X    {
  1265. X        ptr = top()->next 
  1266. X            = (Symbol_record*)malloc(sizeof(Symbol_record));
  1267. X        if(ptr==0)
  1268. X    {
  1269. X        perror("malloc for constant token item");
  1270. X        exit(1);
  1271. X        }
  1272. X    }
  1273. X    else if(ptr->type==arg_symbol)
  1274. X    {
  1275. X        return -1;    /* forbiding override argument symbols */
  1276. X    }
  1277. X
  1278. X    ptr->name = (char*)malloc((strlen(str)+1)*sizeof(char));
  1279. X    if(ptr->name==0) 
  1280. X    {
  1281. X    perror("malloc for constant token name");
  1282. X    exit(1);
  1283. X    }
  1284. X
  1285. X    strncpy(ptr->name, str, strlen(str)+1);
  1286. X    ptr->type = const_symbol;
  1287. X    ptr->content.value = number;
  1288. X
  1289. X    return 1;
  1290. X};
  1291. X
  1292. X#if NeedFunctionPrototypes
  1293. X  int nameargu(char* arg1, char* arg2)
  1294. X#else
  1295. X  int nameargu(arg1, arg2)
  1296. X  char *arg1, *arg2;
  1297. X#endif
  1298. X/* on error return -1, on success return 1, if no change return 0 */
  1299. X{
  1300. X    if(!isname(arg1)) return 0;
  1301. X
  1302. X    if(!isname(arg2)) arg2 = "_";
  1303. X
  1304. X    if(strcmp(arg1, arg2)==0) return -1;  /* stupid */
  1305. X
  1306. X    clrname(arg1, fnct_symbol);
  1307. X    clrname(arg1, const_symbol);
  1308. X    clrname(arg2, fnct_symbol);
  1309. X    clrname(arg2, const_symbol);
  1310. X
  1311. X    if(sym_table==0)
  1312. X    {
  1313. X    sym_table = (Symbol_record*)malloc(sizeof(Symbol_record));
  1314. X    if(sym_table==0)
  1315. X    {
  1316. X        perror("malloc for the 1st argument token item");
  1317. X        exit(1);
  1318. X        }
  1319. X    }
  1320. X
  1321. X    sym_table->name = (char*)malloc((strlen(arg1)+1)*sizeof(char));
  1322. X    if(sym_table->name==0)
  1323. X    {
  1324. X    perror("malloc for the 1st argument token name");
  1325. X    exit(1);
  1326. X    }
  1327. X
  1328. X    strncpy(sym_table->name, arg1, strlen(arg1)+1);
  1329. X    sym_table->type = arg_symbol;
  1330. X    sym_table->content.argidx = 1;
  1331. X
  1332. X    if(sym_table->next==0)
  1333. X    {
  1334. X    sym_table->next = (Symbol_record*)malloc(sizeof(Symbol_record));
  1335. X    if(sym_table->next==0)
  1336. X    {
  1337. X        perror("malloc for the 2nd token token item");
  1338. X        exit(1);
  1339. X        }
  1340. X    }
  1341. X
  1342. X    sym_table->next->name = (char*)malloc((strlen(arg2)+1)*sizeof(char));
  1343. X    if(sym_table->next->name==0)
  1344. X    {
  1345. X    perror("alloc memory for 2nd arguement name");
  1346. X        exit(1);
  1347. X    }
  1348. X
  1349. X    strncpy(sym_table->next->name, arg2, strlen(arg2)+1);
  1350. X
  1351. X    sym_table->next->type = arg_symbol;
  1352. X    sym_table->next->content.argidx = 2;
  1353. X
  1354. X    return 1;
  1355. X};
  1356. X
  1357. X#if NeedFunctionPrototypes
  1358. X  int initargu(void)
  1359. X#else
  1360. X  int initargu()
  1361. X#endif
  1362. X/* if no argument names then use default */
  1363. X{
  1364. X    if(sym_table==0) return nameargu("x", "y");
  1365. X    return 0;
  1366. X};
  1367. X
  1368. Xstatic struct {
  1369. X    char* name;
  1370. X    double (*fnctptr)();
  1371. X} initfnct[] = {
  1372. X    "sin"  , sin  ,
  1373. X    "cos"  , cos  ,
  1374. X    "tan"  , tan  , 
  1375. X    "tg"   , tan  ,
  1376. X    "asin" , asin ,
  1377. X    "acos" , acos ,
  1378. X    "atan" , atan ,
  1379. X    "atan2", atan2,
  1380. X    "exp"  , exp  ,
  1381. X    "sinh" , sinh ,
  1382. X    "sh"   , sinh ,
  1383. X    "cosh" , cosh ,
  1384. X    "ch"   , cosh ,
  1385. X    "tanh" , tanh ,
  1386. X    "th"   , tanh ,
  1387. X    "asinh", asinh,
  1388. X    "ash"  , asinh,
  1389. X    "acosh", acosh,
  1390. X    "ach"  , acosh,
  1391. X    "atanh", atanh,
  1392. X    "ath"  , atanh,
  1393. X    "pow"  , pow  ,
  1394. X    "log"  , log  ,
  1395. X    "ln"   , log  ,
  1396. X    "log10", log10,
  1397. X    "log2" , log2 ,
  1398. X    "sqrt" , sqrt ,
  1399. X    "abs"  , fabs ,
  1400. X    0      , 0      };
  1401. X
  1402. X#if NeedFunctionPrototypes
  1403. X  int matha2z(void)
  1404. X#else
  1405. X  int matha2z()
  1406. X#endif
  1407. X/* set up a symbol table include math "a to z" functions */
  1408. X{
  1409. X    int i=0, j=0;
  1410. X
  1411. X    initargu();
  1412. X
  1413. X    for(i=0;initfnct[i].name!=0;i++)
  1414. X    {
  1415. X        j = j + namefnct(initfnct[i].name, initfnct[i].fnctptr); 
  1416. X    }
  1417. X
  1418. X    j = j + namecnst("pi", 2*asin(1.0));
  1419. X
  1420. X    return j;
  1421. X};
  1422. X
  1423. X/* codes for debug  -------------------------------------------  */
  1424. X
  1425. X#if NeedFunctionPrototypes
  1426. X  int printrec(Symbol_record* ptr, double x)
  1427. X#else
  1428. X  int printrec(ptr, x)
  1429. X  Symbol_record* ptr;
  1430. X  double x;
  1431. X#endif
  1432. X{
  1433. X    printf("type = %d\n", ptr->type);
  1434. X    if(ptr->name!=0) printf("name = %s\n", ptr->name);
  1435. X   
  1436. X    switch(ptr->type)
  1437. X    {
  1438. X    case const_symbol:
  1439. X         printf("value= %f\n", ptr->content.value);
  1440. X         break;
  1441. X
  1442. X        case arg_symbol:
  1443. X         printf("arg[%d] = %s\n", ptr->content.argidx,
  1444. X                      getfnctname(ptr->content.fnctptr));
  1445. X         break;
  1446. X
  1447. X        case fnct_symbol:
  1448. X         printf("%s(%f) = %f\n", ptr->name, x, (ptr->content.fnctptr)(x));
  1449. X             break;
  1450. X
  1451. X        default: break;
  1452. X    }
  1453. X
  1454. X    return 0;
  1455. X};
  1456. X
  1457. X#if NeedFunctionPrototypes
  1458. X  int printab(double x)
  1459. X#else
  1460. X  int printab(x)
  1461. X  double x;
  1462. X#endif
  1463. X{
  1464. X    Symbol_record* ptr;
  1465. X    int len = 0;
  1466. X
  1467. X    for(ptr=sym_table; ptr!=0; ptr=ptr->next)
  1468. X    {
  1469. X       len ++;
  1470. X       printrec(ptr, x);
  1471. X    }
  1472. X
  1473. X    return len;
  1474. X};
  1475. X
  1476. X#ifdef __cplusplus
  1477. X  }    /* end for c++ */
  1478. X#endif
  1479. END_OF_FILE
  1480.   if test 11913 -ne `wc -c <'dfcsymtable.c'`; then
  1481.     echo shar: \"'dfcsymtable.c'\" unpacked with wrong size!
  1482.   fi
  1483.   # end of 'dfcsymtable.c'
  1484. fi
  1485. if test -f 'dfctoken.3.UU' -a "${1}" != "-c" ; then 
  1486.   echo shar: Will not clobber existing file \"'dfctoken.3.UU'\"
  1487. else
  1488.   echo shar: Extracting \"'dfctoken.3.UU'\" \(7385 characters\)
  1489.   sed "s/^X//" >'dfctoken.3.UU' <<'END_OF_FILE'
  1490. Xbegin 644 dfctoken.3
  1491. XM"@H*1$5&54Y#*#,I(" @(" @(" @(" @("!#($Q)0E)!4ED@1E5.0U1)3TY3
  1492. XM(" @(" @(" @(" @("!$14953D,H,RD*"@H*3D%-10H@(" @(&=E=&%R9W5N
  1493. XM86UE+" @(&YA;65A<F=U+" @(&YA;65F;F-T+" @(&YA;65C;G-T+" @("!C
  1494. XM;')F;F-T+ H@(" @(&-L<F9N8W1A;&PL("!C;')C;G-T+" @8VQR8VYS=&%L
  1495. XM;" @+2T@(%\(9%\(95\(9E\(=5\(;E\(8R!E>'1E<FYA;"!T;VME;@H@(" @
  1496. XM(&UA;F%G96UE;G0@9G5N8W1I;VYS+@H*4UE.3U!325,*(" @(" C:6YC;'5D
  1497. XM92 \9&5F=6YC+F@^"@H@(" @(&-H87(J("!G971A<F=U;F%M92AI;G0@87)G
  1498. XM=6ED>"D["B @(" @:6YT(" @(&YA;65A<F=U*&-H87(J(&%R9W4Q+"!C:&%R
  1499. XM*B!A<F=U,BD["@H@(" @(&EN=" @("!N86UE9FYC="AC:&%R*B!N86UE+"!D
  1500. XM;W5B;&4@*"IF;BDH*2D["B @(" @:6YT(" @(&YA;65C;G-T*&-H87(J(&YA
  1501. XM;64L(&1O=6)L92D["@H@(" @(&EN=" @("!C;')F;F-T*&-H87(J(&YA;64I
  1502. XM.PH@(" @(&EN=" @("!C;')F;F-T86QL*'9O:60I.PH@(" @(&EN=" @("!C
  1503. XM;')C;G-T*&-H87(J(&YA;64I.PH@(" @(&EN=" @("!C;')C;G-T86QL*'9O
  1504. XM:60I.PH*"D1%4T-225!424]."B @(" @5&AE<V4@9G5N8W1I;VYS(&%R92!U
  1505. XM<V5D(&EN("!M86YA9VEN9R @=&AE("!G;&]B86P@(&5X=&5R;F%L"B @(" @
  1506. XM=&]K96X@('1A8FQE+B @7PAD7PAE7PAF7PAU7PAN7PAC("!F=6YC=&EO;B!?
  1507. XM"&1?"&9?"&]?"'!?"&5?"&XH*2!P87)S92!A;B!E>'!R97-S:6]N"B @(" @
  1508. XM8F%S960@;VX@=&AE('1O:V5N<R @:70@(')E8V]G;FEZ960N("!%>&-E<'0@
  1509. XM(&9O<B @;G5M97)I8V%L"B @(" @<W1R:6YG(&-O;G-T86YT('1O:V5N<RAI
  1510. XM+F4N(&%N;VYY;6]U<R!C;VYS=&%N="!T;VME;G,I(&%N9" X"B @(" @8G5I
  1511. XM;&0@:6X@=&]K96YS("(K(BP@(BTB+" B*B(L("(O(BP@(EXB+" B*"(L("(I
  1512. XM(BP@(BPB("!P;'5S"B @(" @82 @<'-E=61O("!T;VME;B @(CTB+"!A;&P@
  1513. XM;W1H97(@=&]K96YS(&%R92!E>'1E<FYA;"!T;VME;G,N"B @(" @17AT97)N
  1514. XM86P@=&]K96YS(&%R92!U<W5A;&QY(&)E('!U='1E9"!I;G1O("!A("!G;&]B
  1515. XM86P@('1O:V5N"B @(" @=&%B;&4@(&)Y("!U<V5R+B @5&AE>2 @:6YC;'5D
  1516. XM92 @,B!A<F=U;65N="!T;VME;G,L(&9U;F-T:6]N"B @(" @=&]K96YS(&%N
  1517. XM9"!N86UE9"!C;VYS=&%N="!T;VME;G,N(%1H97D@8V%N(&)E("!S970O<F5S
  1518. XM970@(&]R"B @(" @861D960O9&5L971E9"!S=&%T:6-A;&QY(&%S('=E;&P@
  1519. XM87,@9'EN86UI8V%L;'DN"@H@(" @(%\(9U\(95\(=%\(85\(<E\(9U\(=5\(
  1520. XM;E\(85\(;5\(92@I(')E='5R;B!T:&4@;F%M92!O9B @<W!E8VEF:6,@(&%R
  1521. XM9W5M96YT("!T;VME;BX*(" @("!4:&ES("!W:6QL("!B92 @=7-E9G5L("!A
  1522. XM9G1E<B @=&AE(&%R9W5M96YT('1O:V5N<R!H879E(&)E96X*(" @("!R97-E
  1523. XM="X@3VX@<W5C8V5S<RP@7PAG7PAE7PAT7PAA7PAR7PAG7PAU7PAN7PAA7PAM
  1524. XM7PAE*"D@<F5T=7)N(&$@(&-H87(@('!O:6YT97(@('1O"B @(" @=&AE("!N
  1525. XM86UE('-T<FEN9RX@3VX@97)R;W(L(&4N9RX@82!I;&QE9V%L(&%R9W5M96YT
  1526. XM(&EN9&5X(&)E"B @(" @<&%S<V5D+"!I="!W:6QL(')E='5R;B P('!O:6YT
  1527. XM97(N"@H@(" @(%\(;E\(85\(;5\(95\(85\(<E\(9U\(=2@I(')E<V5T<R R
  1528. XM(&%R9W5M96YT<R!T;VME;G,N($EN(%\(9%\(95\(9E\(=5\(;E\(8RP@=&AE
  1529. XM(&1E9F%U;'0*(" @("!T;VME;B @;F%M97,@(&9O<B!T:&4@,7-T(&%N9" R
  1530. XM;F0@87)G=6UE;G1S(&%R92 B>"(@86YD(")Y(BX*(" @("!!(&QE9V%L('1O
  1531. XM:V5N(&YA;64@<VAO=6QD('-T87)T("!W:71H("!A;'!H86)E=&EC("!C:&%R
  1532. XM86-T97(*(" @("!A;F0@(&9O;&QO=V5D("!B>2 @86QP:&%B971I8R @;W(@
  1533. XM(&YU;6)E<FEC("!C:&%R86-T97)S+B!4:&4*(" @("!L96YG=&@@;V8@82!N
  1534. XM86UE(&ES(&QI;6ET960@=&\@,S$@8GD@7PAD7PAE7PAF7PAU7PAN7PAC+B!?
  1535. XM"&Y?"&%?"&U?"&5?"&%?"')?"&=?"'4H*2!W:6QL"B @(" @<F5T=7)N(" H
  1536. XM:6YT*3$@(&]N('-U8V-E<W,L("AI;G0I*"TQ*2!O;B!E<G)O<B!A;F0@*&EN
  1537. XM="DP(&]N"B @(" @;F\@8VAA;F=E+B @7PAD7PAE7PAF7PAU7PAN7PAC(&%L
  1538. XM<V\@<')O=FED97,@86X@96%S>2!W87D@=&\@(&-H86YG92 @=&AE"B @(" @
  1539. XM87)G=6UE;G0@(&YA;65S("!D:7)E8W1L>2 @9G)O;2 @=&AE("!E>'!R97-S
  1540. XM:6]N("!P87-S960@('1O"B @(" @7PAD7PAF7PAO7PAP7PAE7PAN*"D@*'-E
  1541. XM92!?"&1?"&5?"&9?"'5?"&Y?"&,H7P@S*2 I+@H*(" @("!?"&Y?"&%?"&U?
  1542. XM"&5?"&9?"&Y?"&-?"'0H*2!!9&0@82!F=6YC=&EO;B!T;VME;B!T;R!T:&4@
  1543. XM97AT97)N86P@=&]K96X@=&%B;&4N"B @(" @268@(&%N;W1H97(@('1O:V5N
  1544. XM("!W:71H("!T:&4@('-A;64@(&YA;64@:&%S(&%L<F5A9'D@:6X@=&AE"B @
  1545. XM(" @=&%B;&4L('1H96X@=&AE(&]L9"!O;F4@=VEL;"!B92!O=F5R;&%P<&5D
  1546. XM(&5X8V5P="!F;W(@:70@(&ES"B @(" @82 @(&%R9W5M96YT(" @=&]K96XN
  1547. XM("!!<F=U;65N=" @=&]K96X@(&ET96US("!C86X@(&]N;'D@(&)E"@H*"F1E
  1548. XM9G5N8R Q+C(@(" @(" @(" @(" @($QA<W0@8VAA;F=E.B Q.3DS(" @(" @
  1549. XM(" @(" @(" @(" @(" @(" Q"@H*"@H*"D1%1E5.0R@S*2 @(" @(" @(" @
  1550. XM(" @0R!,24)205)9($953D-424].4R @(" @(" @(" @(" @1$5&54Y#*#,I
  1551. XM"@H*"B @(" @;W9E<FQA<'!E9"!B>2!N97<@87)G=6UE;G1S('-E='1I;F<N
  1552. XM("!!9G1E<B!A(&9U;F-T:6]N('1O:V5N"B @(" @8F5I;F<@('-U8V-E<W-F
  1553. XM=6QL>2!P=70@:6YT;R!T:&4@=&]K96X@=&%B;&4L(&ET(&-A;B!B92!U<V5D
  1554. XM"B @(" @:6X@86YY(%\(9%\(95\(9E\(=5\(;E\(8R!E>'!R97-S:6]N<RX@
  1555. XM7PAN7PAA7PAM7PAE7PAF7PAN7PAC7PAT*"D@=VEL;"!R971U<FX@*&EN="DQ
  1556. XM("!O;@H@(" @('-U8V-E<W,L(" H:6YT*2@M,2D@(&]N("!E<G)O<B @86YD
  1557. XM(" H:6YT*3 @(&]N(&YO(&-H86YG:6YG+@H@(" @(%\(9%\(95\(9E\(=5\(
  1558. XM;E\(8R!A;'-O('!R;W9I9&5S(&$@9'EN86UI8V%L;'D@=V%Y('1O(&5X<&%N
  1559. XM9" @=&AE("!T;VME;@H@(" @('1A8FQE("!W:71H("!D>6YA;6EC86QL>2!C
  1560. XM;VYS=')U8W1E9"!F=6YC=&EO;G,@9&ER96-T;'D@9G)O;0H@(" @(&5X<')E
  1561. XM<W-I;VX@<&%S<V5D('1O(%\(9%\(9E\(;U\(<%\(95\(;B@I+B H<V5E(%\(
  1562. XM9%\(9E\(;U\(<%\(95\(;BA?"#,I("DN"@H@(" @(%\(;E\(85\(;5\(95\(
  1563. XM8U\(;E\(<U\(="@I('!U=',@82!C;VYS=&%N="!T;VME;B!I;G1O('1H92!T
  1564. XM;VME;B!T86)L92X@(%1H:7,*(" @("!W:6QL('-H;W)T('1H92!L;VYG(&5X
  1565. XM<')E<W-I;VX@:6X@=W)I='1I;F<@<V]M92!S<&5C:6%L(&-O;BT*(" @("!S
  1566. XM=&%N="X@92YG+@H*(" @(" @(" @(&YA;65C;G-T*")P:2(L(#(N,"IA<VEN
  1567. XM*#$N,"DI.PH*(" @("!W:6QL(&%D9"!T:&4@;F%M92UC;VYS=&%N="!A<W-O
  1568. XM8VEA=&EO;B H(G!I(BP@(#,N,30Q-3DR-BXN+BD*(" @("!I;G1O("!T:&4@
  1569. XM('1A8FQE+B!!9G1E<B!T:&%T+"!T:&4@<WEM8F]L(")P:2(@8V%N(&)E('5S
  1570. XM960@:6X*(" @("!?"&1?"&5?"&9?"'5?"&Y?"&,@(&5X<')E<W-I;VX@(&%S
  1571. XM("!A("!C;VYS=&%N="X@("!386UE("!A<R @7PAN7PAA7PAM7PAE7PAF7PAN
  1572. XM7PAC7PAT*"DL"B @(" @7PAN7PAA7PAM7PAE7PAC7PAN7PAS7PAT*"D@(&-A
  1573. XM;B @;W9E<FQA<" @86X@(&]L9"!F=6YC=&EO;B!O<B!C;VYS=&%N="!T;VME
  1574. XM;@H@(" @(&ET96T@8G5T(&-A;B=T(&]V97)L87 @86X@87)G=6UE;G0@=&]K
  1575. XM96X@(&ET96TN(" @7PAN7PAA7PAM7PAE7PAC7PAN7PAS7PAT*"D*(" @("!W
  1576. XM:6QL(')E='5R;B H:6YT*3$@;VX@<W5C8V5S<RP@*&EN="DH+3$I(&]N(&5R
  1577. XM<F]R(&%N9" H:6YT*3 *(" @("!O;B!N;R!C:&%N9VEN9RX@3&EK92!A<F=U
  1578. XM;65N="!A;F0@9G5N8W1I;VXL(%\(9%\(95\(9E\(=5\(;E\(8R!A;'-O('!R
  1579. XM;RT*(" @("!V:61E<R @86X@('=A>2!T;R!E>'!A;F0@=&]K96X@=&%B;&4@
  1580. XM=VET:"!N97<@8V]N<W1A;G0@=&]K96X*(" @("!I=&5M(&%T(')U;G1I;64@
  1581. XM*'-E92!?"&1?"&9?"&]?"'!?"&5?"&XH*2 I+@H*(" @("!?"&-?"&Q?"')?
  1582. XM"&9?"&Y?"&-?"'0H*2P@7PAC7PAL7PAR7PAF7PAN7PAC7PAT7PAA7PAL7PAL
  1583. XM*"DL(%\(8U\(;%\(<E\(8U\(;E\(<U\(="@I+"!?"&-?"&Q?"')?"&-?"&Y?
  1584. XM"'-?"'1?"&%?"&Q?"&PH*2!C86X@8F4@=7-E9 H@(" @('1O("!D96QE=&4@
  1585. XM(&$@<W!E8VEF:6,@=&]K96X@;W(@<W!E8VEF:6,@=&]K96X@='EP92!F<F]M
  1586. XM('1H90H@(" @('1O:V5N('1A8FQE+@H*4T5%($%,4T\*(" @("!?"&1?"&5?
  1587. XM"&9?"'5?"&Y?"&,L(&1F;W!E;B@S*0H*15A!35!,15,*(" @(" @(" @(&YA
  1588. XM;65A<F=U*")R92(L(")I;2(I.PH*(" @("!4:&ES('=I;&P@<F5S970@(&%R
  1589. XM9W5M96YT("!T;VME;G,@('1O(" B<F4B("!A;F0@(")I;2(@("AT:&4*(" @
  1590. XM("!D969A=6QT(&ES(")X(B!A;F0@(GDB*2X*"B @(" @(" @("!N86UE9FYC
  1591. XM="@B;&]G(BP@;&]G*3L*(" @(" @(" @(&YA;65F;F-T*")L;B(@+"!L;V<I
  1592. XM.PH*(" @("!4:&ES('=I;&P@861D(&5X=&5R;F%L(&9U;F-T:6]N(&QO9R@I
  1593. XM(&EN=&\@=&]K96X@;&ES="!W:71H(#(*(" @("!A;&EA<R!N86UE<R B;&]G
  1594. XM(B!A;F0@(FQN(BX*"B @(" @(" @("!N86UE8VYS="@B<&DB+" R+C J87-I
  1595. XM;B@Q+C I*3L*(" @(" @(" @(&YA;65C;G-T*")022(L(#(N,"IA<VEN*#$N
  1596. XM,"DI.PH*(" @("!4:&ES('=I;&P@<'5T(&$@;F%M960@8V]N<W1A;G0@=&]K
  1597. XM96X@:6YT;R @=&AE("!T;VME;B @=&%B;&4*(" @("!W:71H('9A;'5E(#,N
  1598. XM,30Q-3DN+BX@86YD(#(@86QI87,@;F%M97,@(G!I(B!A;F0@(E!)(BX*"D%5
  1599. XM5$A/4@H@(" @($ME($II;@H@(" @(%!H>7-I8W,@1&5P87)T;65N= H*"@ID
  1600. XM969U;F,@,2XR(" @(" @(" @(" @("!,87-T(&-H86YG93H@,3DY,R @(" @
  1601. XM(" @(" @(" @(" @(" @(" @,@H*"@H*"@I$14953D,H,RD@(" @(" @(" @
  1602. XM(" @($,@3$E"4D%262!&54Y#5$E/3E,@(" @(" @(" @(" @($1%1E5.0R@S
  1603. XM*0H*"@H@(" @(%%U965N)W,@56YI=F5R<VET>0H@(" @($MI;F=S=&]N+"!/
  1604. XM;G1A<FEO"B @(" @0V%N861A($LW3" S3C8*(" @("!J:6YK94!S<&%R:WDN
  1605. XM<&AY+G%U965N<W4N8V$*"D)51U,*(" @("!297!O<G0@8G5G<R!O9B!?"&1?
  1606. XM"&5?"&9?"'5?"&Y?"&,@;&EB<F%R>2!T;R!T:&4@875T:&]R(&)Y(&5M86EL
  1607. XM+@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*"@H*
  1608. XM"@H*"@ID969U;F,@,2XR(" @(" @(" @(" @("!,87-T(&-H86YG93H@,3DY
  1609. X=,R @(" @(" @(" @(" @(" @(" @(" @,PH*"@I,
  1610. Xend
  1611. END_OF_FILE
  1612.   if test 7385 -ne `wc -c <'dfctoken.3.UU'`; then
  1613.     echo shar: \"'dfctoken.3.UU'\" unpacked with wrong size!
  1614.   else
  1615.     echo shar: Uudecoding \"'dfctoken.3'\" \(5339 characters\)
  1616.     cat dfctoken.3.UU | uudecode
  1617.     if test 5339 -ne `wc -c <'dfctoken.3'`; then
  1618.       echo shar: \"'dfctoken.3'\" uudecoded with wrong size!
  1619.     else
  1620.       rm dfctoken.3.UU
  1621.     fi
  1622.   fi
  1623.   # end of 'dfctoken.3.UU'
  1624. fi
  1625. if test -f 'dfctree.c' -a "${1}" != "-c" ; then 
  1626.   echo shar: Will not clobber existing file \"'dfctree.c'\"
  1627. else
  1628.   echo shar: Extracting \"'dfctree.c'\" \(4419 characters\)
  1629.   sed "s/^X//" >'dfctree.c' <<'END_OF_FILE'
  1630. X/*********************************************************
  1631. X *
  1632. X *    Copyright (c) 1993  Ke Jin
  1633. X *
  1634. X *    Permission to use, copy, modify, and distribute
  1635. X *    this software and its documentation without fee
  1636. X *    is granted, provided that the author's name and
  1637. X *    this copyright notice are retained.
  1638. X *
  1639. X * -----------------------------------------------------
  1640. X *
  1641. X *    dfctree.c -- defunc low level module
  1642. X *
  1643. X *    public  function : exparse();
  1644. X *                       evaluate();
  1645. X *                       reduce();
  1646. X *     
  1647. X *********************************************************/
  1648. X
  1649. X#include <stdio.h>
  1650. X#include "dfctree.h"
  1651. X
  1652. X#ifdef __cplusplus
  1653. X  extern "C" {    /* for c++ */
  1654. X#endif
  1655. X
  1656. X#if NeedFunctionPrototypes
  1657. X  extern yyinit(char* expression);
  1658. X  extern int yyparse(void);
  1659. X#else
  1660. X  extern yyinit();
  1661. X  extern int yyparse();
  1662. X#endif
  1663. X
  1664. X#define ltree  reduce(tree+tree->left-i,  tree->left)
  1665. X#define rtree  reduce(tree+tree->right-i, tree->right)
  1666. X#define trval  tree->content.value
  1667. X#define ltrval ltree->content.value
  1668. X#define rtrval rtree->content.value
  1669. X
  1670. X#if NeedFunctionPrototypes
  1671. X  Node* reduce(Node* tree, int i)
  1672. X#else
  1673. X  Node* reduce(tree, i)
  1674. X  Node* tree;
  1675. X  int   i;
  1676. X#endif
  1677. X/* constant folding. i is the shift relative to root 
  1678. X * reduce tree still in original memory address. the
  1679. X * root of the tree be returned */
  1680. X{ 
  1681. X    if(tree == 0) return 0;
  1682. X
  1683. X    switch(tree->type)
  1684. X    {
  1685. X    case const_node:
  1686. X    case arg_node:
  1687. X             break;
  1688. X
  1689. X    case simplex_fnct_node:
  1690. X         if(rtree->type==const_node)
  1691. X         {
  1692. X         tree->type = const_node;
  1693. X         trval = (tree->content.fnctptr)(rtrval);
  1694. X             }
  1695. X         break;
  1696. X
  1697. X        case duplex_fnct_node:
  1698. X         if((ltree->type==const_node)&&(rtree->type==const_node))
  1699. X         {
  1700. X         tree->type = const_node;
  1701. X         trval = (tree->content.fnctptr)(ltrval, rtrval);
  1702. X             }
  1703. X         break;
  1704. X
  1705. X        case unary_op_node:
  1706. X          if(rtree->type==const_node)
  1707. X         {
  1708. X          tree->type = const_node;
  1709. X         switch(tree->content.op)
  1710. X         {
  1711. X             case op_neg: trval = -rtrval; break;
  1712. X
  1713. X             default: break;
  1714. X                 }
  1715. X             }
  1716. X         break;
  1717. X
  1718. X        case binary_op_node:
  1719. X         if(rtree->type==const_node&&tree->content.op==op_div)
  1720. X         {
  1721. X         tree->content.op=op_mul;
  1722. X         rtree->content.value = 1.0/(rtree->content.value);
  1723. X             }
  1724. X
  1725. X         if(ltree->type==const_node&&rtree->type==const_node)
  1726. X         {
  1727. X         tree->type = const_node;
  1728. X         switch(tree->content.op)
  1729. X         {
  1730. X             case op_sum: trval = ltrval+rtrval; break;
  1731. X             case op_sub: trval = ltrval-rtrval; break;
  1732. X             case op_mul: trval = ltrval*rtrval; break;
  1733. X             case op_div: trval = ltrval/rtrval; break; 
  1734. X
  1735. X             default: break;
  1736. X                 }
  1737. X             }
  1738. X             break;
  1739. X
  1740. X        default: break;
  1741. X    }  
  1742. X
  1743. X    return tree;
  1744. X}; 
  1745. X
  1746. X#if NeedFunctionPrototype
  1747. X  int exparse(char* expression)
  1748. X#else
  1749. X  int exparse(expression)
  1750. X  char* expression;
  1751. X#endif
  1752. X{
  1753. X    yyinit(expression);  /* initial the parser */
  1754. X
  1755. X    return yyparse();    /* on success, return 0 or tree size
  1756. X              * on error  , return -1 */
  1757. X};
  1758. X
  1759. X#define lval evaluate(tree+tree->left-i,  tree->left , x, y)
  1760. X#define rval evaluate(tree+tree->right-i, tree->right, x, y) 
  1761. X
  1762. X#if NeedFunctionPrototype
  1763. X  double evaluate(Node* tree, int i, double x, double y)
  1764. X#else
  1765. X  double evaluate(tree, i, x, y) 
  1766. X  Node*  tree;
  1767. X  int    i;
  1768. X  double x, y;
  1769. X#endif
  1770. X/* evaluate a parse tree. i is the shift relative to root */
  1771. X{
  1772. X    if(tree == 0) 
  1773. X    {
  1774. X    fprintf(stderr, "Null parse tree\n");
  1775. X        exit(1) ;
  1776. X    }
  1777. X
  1778. X    switch (tree->type) 
  1779. X    {
  1780. X    case const_node:
  1781. X         return tree->content.value;
  1782. X
  1783. X        case arg_node:
  1784. X         switch(tree->content.argidx)
  1785. X         {
  1786. X         case 1: return x;
  1787. X         case 2: return y;
  1788. X         default: exit(1);
  1789. X             }
  1790. X         break;
  1791. X
  1792. X    case simplex_fnct_node:
  1793. X         return (tree->content.fnctptr)(rval);
  1794. X
  1795. X        case duplex_fnct_node:
  1796. X         return (tree->content.fnctptr)(lval, rval);
  1797. X
  1798. X        case unary_op_node:
  1799. X         switch(tree->content.op)
  1800. X         {
  1801. X        case op_neg: return -rval;
  1802. X
  1803. X        default: break;
  1804. X             }
  1805. X         break;
  1806. X
  1807. X        case binary_op_node:
  1808. X             switch(tree->content.op)
  1809. X         {
  1810. X        case op_sum: return lval + rval; 
  1811. X                case op_sub: return lval - rval;
  1812. X        case op_mul: return lval * rval;
  1813. X        case op_div: return lval / rval;
  1814. X
  1815. X        default: break;
  1816. X             }
  1817. X         break;
  1818. X
  1819. X        default:
  1820. X         exit(1) ;   /* something wrong */
  1821. X    }
  1822. X
  1823. X    return 0; /* turn off the warning of lint */
  1824. X};
  1825. X
  1826. X#ifdef __cplusplus
  1827. X  }    /* end for c++ */
  1828. X#endif
  1829. END_OF_FILE
  1830.   if test 4419 -ne `wc -c <'dfctree.c'`; then
  1831.     echo shar: \"'dfctree.c'\" unpacked with wrong size!
  1832.   fi
  1833.   # end of 'dfctree.c'
  1834. fi
  1835. echo shar: End of archive 1 \(of 2\).
  1836. cp /dev/null ark1isdone
  1837. MISSING=""
  1838. for I in 1 2 ; do
  1839.     if test ! -f ark${I}isdone ; then
  1840.     MISSING="${MISSING} ${I}"
  1841.     fi
  1842. done
  1843. if test "${MISSING}" = "" ; then
  1844.     echo You have unpacked both archives.
  1845.     rm -f ark[1-9]isdone
  1846. else
  1847.     echo You still must unpack the following archives:
  1848.     echo "        " ${MISSING}
  1849. fi
  1850. exit 0
  1851. exit 0 # Just in case...
  1852.