Subversion Repositories filter_foundry

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.         This file is part of exprparser, a lex/yacc based expression parser
  3.     Copyright (C) 2002-3 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by  
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License  
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <math.h>
  22.  
  23. #include "parser.h"
  24. #include "node.h"
  25. #include "y.tab.h"
  26.  
  27. value_type t=.123,u=.456,v=.789,s[8],pi = M_PI;
  28.  
  29. value_type fxy(value_type x,value_type y){ return x*y; }
  30. value_type fxyz(value_type x,value_type y,value_type z){ return x*y*z; }
  31.  
  32. /* predefined symbols */
  33. extern struct sym_rec predefs[];
  34. value_type cell[0x100],map[4][0x100],slider[8];
  35. #if 0
  36. struct sym_rec predefs[]={
  37.         /* variables */
  38.         {0,TOK_VAR,"t", 0, &t},
  39.         {0,TOK_VAR,"u", 0, &u},
  40.         {0,TOK_VAR,"v", 0, &v},
  41.         {0,TOK_VAR,"s0", 0, &s[0]},
  42.         {0,TOK_VAR,"s1", 0, &s[1]},
  43.         {0,TOK_VAR,"s2", 0, &s[2]},
  44.         {0,TOK_VAR,"s3", 0, &s[3]},
  45.         {0,TOK_VAR,"s4", 0, &s[4]},
  46.         {0,TOK_VAR,"s5", 0, &s[5]},
  47.         {0,TOK_VAR,"s6", 0, &s[6]},
  48.         {0,TOK_VAR,"s7", 0, &s[7]},
  49.         /* constants */
  50.         {0,TOK_VAR,"PI", 0, &pi},
  51.         /* functions */
  52.         {0,TOK_FN1,"sin", &sin, 0},
  53.         {0,TOK_FN1,"cos", &cos, 0},
  54.         {0,TOK_FN1,"tan", &tan, 0},
  55.         {0,TOK_FN1,"exp", &exp, 0},
  56.         {0,TOK_FN1,"log", &log, 0},
  57.         {0,TOK_FN1,"log10", &log10, 0},
  58.         {0,TOK_FN1,"sqrt", &sqrt, 0},
  59.         {0,TOK_FN2,"fxy", &fxy, 0}, /* example 2-arg function */
  60.         {0,TOK_FN3,"fxyz", &fxyz, 0}, /* example 3-arg function */
  61.         {0,0,0}
  62. };
  63. #endif
  64.  
  65.  
  66. int test1(int x,int y){
  67.     int d = abs(x) + abs(y);
  68.     int h;
  69.     if (d == 0) return 0;
  70.     h = x*x + y*y;      //easily overflows!
  71.     d = (h/d + d) >> 1; //1st N-R iteration
  72.     d = (h/d + d) >> 1; //2nd N-R iteration
  73.     return d;
  74. }
  75. int test2(int x,int y){
  76.     return isqrt(x*x + y*y);
  77. }
  78. double test3(int x,int y){
  79.     return sqrt(x*x + y*y);
  80. }
  81.  
  82. int main(int argc,char *argv[]){
  83.         int i,n;
  84.         struct node *tree;
  85.         char expr[0x100];
  86.         extern int tokpos,varused[],allocs;
  87.         int srcradused;
  88.         extern char *errstr;
  89.  
  90.         init_symtab(predefs);
  91.  
  92.         while( fgets(expr,0x100,stdin) ){
  93.                 tokpos = 0;
  94.  
  95.                 if(tree = parseformula(expr)){
  96.                         dumptree(tree,0);
  97. #ifdef FP_VALUE
  98.                         printf("= %g\n",eval(tree));
  99. #else
  100.                         printf("= %ld\n",eval(tree));
  101. #endif
  102.                         /* initialise flags for tracking special variable usage */
  103.                         for( i=0 ; i<0x100 ; i++ )
  104.                                 varused[i] = 0;
  105.                         checkvars(tree,varused,&srcradused);
  106.                         for(i=n=0;i<0x100;i++)
  107.                                 if(varused[i]){
  108.                                         if(n) putchar(',');
  109.                                         else printf("special variables used: ");
  110.                                         putchar(i);
  111.                                         ++n;
  112.                                 }
  113.                         if(n) putchar('\n');
  114.                 }else
  115.                         printf("%*s\n%s\n",tokpos,"^",errstr);
  116.  
  117.                 freeallnodes(); // we're done with the parse tree
  118.                 printf("### leaked allocs = %d\n", allocs);
  119.         }
  120.         /*
  121.         for(i=0;i<40;++i){
  122.                 int x = (i*123)%1000, y = (i*234)%1000;
  123.                 printf("c2m(%4d,%4d) FM= %6d  FF= %6d  FP= %12.3f\n",x,y,test1(x,y),test2(x,y),test3(x,y));
  124.         }
  125.         */
  126.         return 0;
  127. }
  128.