eval.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include "cal_tab.h"
  8. #include "compiler.h"
  9. #include "eval.h"
  10. #define INTCONST 1
  11. #define DBLCONST 2
  12. #define HEXCONST 3
  13. #define VARIABLE 4
  14. #define OTHER 5
  15. int yyparse(char *exp);
  16. void llinit(void);
  17. int gettoken(char *lltb, int lltbsiz);
  18. char yytext[256]="";
  19. char lastVar[256]="";
  20. int *errPtr;
  21. int result;
  22. int colCount=0;
  23. varType *varTable;
  24. char last_error_string[1024];
  25. double globalregs[100];
  26. //------------------------------------------------------------------------------
  27. void *compileExpression(char *exp)
  28. {
  29. int errv=0;
  30. errPtr=&errv;
  31. llinit();
  32. if (!yyparse(exp) && !*errPtr)
  33. {
  34. return (void*)result;
  35. }
  36. return 0;
  37. }
  38. //------------------------------------------------------------------------------
  39. void setLastVar(void)
  40. {
  41. gettoken(lastVar, sizeof lastVar);
  42. }
  43. //------------------------------------------------------------------------------
  44. int setVar(int varNum, double value)
  45. {
  46. int i=varNum;
  47. if (varNum < 0)
  48. {
  49. char *var=lastVar;
  50. if (!_strnicmp(var,"reg",3) && strlen(var) == 5 && isdigit(var[3]) && isdigit(var[4]))
  51. {
  52. int x=atoi(var+3);
  53. if (x < 0 || x > 99) x=0;
  54. i=EVAL_MAX_VARS+x;
  55. }
  56. else
  57. {
  58. for (i=0;i<EVAL_MAX_VARS;i++)
  59. {
  60. if (!varTable[i].name[0] || !_strnicmp(varTable[i].name,lastVar,sizeof(varTable[i].name)))
  61. break;
  62. }
  63. if (i==EVAL_MAX_VARS) return -1;
  64. }
  65. }
  66. if (i < 0 || i >= EVAL_MAX_VARS+100)
  67. {
  68. return -1;
  69. }
  70. if (i >= EVAL_MAX_VARS && i < EVAL_MAX_VARS+100)
  71. {
  72. globalregs[i - EVAL_MAX_VARS]=value;
  73. }
  74. else if (!varTable[i].name[0])
  75. {
  76. strncpy(varTable[i].name,lastVar,sizeof(varTable[i].name));
  77. varTable[i].value = value;
  78. }
  79. return i;
  80. }
  81. //------------------------------------------------------------------------------
  82. int getVar(int varNum)
  83. {
  84. if (varNum >= EVAL_MAX_VARS && varNum < EVAL_MAX_VARS+100)
  85. return createCompiledValue(0, globalregs + (varNum - EVAL_MAX_VARS));
  86. if (varNum >= 0 && varNum < EVAL_MAX_VARS)
  87. return createCompiledValue(0, &(varTable[varNum].value));
  88. return createCompiledValue(0, NULL);
  89. }
  90. #if 0
  91. //------------------------------------------------------------------------------
  92. double *getVarPtr(char *var)
  93. {
  94. int i;
  95. if (!_strnicmp(var,"reg",3) && strlen(var) == 5 && isdigit(var[3]) && isdigit(var[4]))
  96. {
  97. int x=atoi(var+3);
  98. if (x < 0 || x > 99) x=0;
  99. return globalregs + x;
  100. }
  101. for (i=0;i<EVAL_MAX_VARS;i++)
  102. if (!_strnicmp(varTable[i].name, yytext,sizeof(varTable[i].name)))
  103. return &(varTable[i].value);
  104. return NULL;
  105. }
  106. #endif
  107. //------------------------------------------------------------------------------
  108. double *registerVar(char *var)
  109. {
  110. int i;
  111. if (!_strnicmp(var,"reg",3) && strlen(var) == 5 && isdigit(var[3]) && isdigit(var[4]))
  112. {
  113. int x=atoi(var+3);
  114. if (x < 0 || x > 99) x=0;
  115. return globalregs + x;
  116. }
  117. for (i=0;i<EVAL_MAX_VARS;i++)
  118. if (!varTable[i].name[0] ||
  119. !_strnicmp(varTable[i].name,var,sizeof(varTable[i].name)))
  120. break;
  121. if (i==EVAL_MAX_VARS) return NULL;
  122. if (!varTable[i].name[0])
  123. {
  124. strncpy(varTable[i].name,var,sizeof(varTable[i].name));
  125. varTable[i].value = 0.0;
  126. }
  127. return &(varTable[i].value);
  128. }
  129. //------------------------------------------------------------------------------
  130. int translate(int type)
  131. {
  132. int v;
  133. int n;
  134. *yytext = 0;
  135. gettoken(yytext, sizeof yytext);
  136. switch (type)
  137. {
  138. case INTCONST: return createCompiledValue((double)atoi(yytext), NULL);
  139. case DBLCONST: return createCompiledValue((double)atof(yytext), NULL);
  140. case HEXCONST:
  141. v=0;
  142. n=0;
  143. while (1)
  144. {
  145. int a=yytext[n++];
  146. if (a >= '0' && a <= '9') v+=a-'0';
  147. else if (a >= 'A' && a <= 'F') v+=10+a-'A';
  148. else if (a >= 'a' && a <= 'f') v+=10+a-'a';
  149. else break;
  150. v<<=4;
  151. }
  152. return createCompiledValue((double)v, NULL);
  153. }
  154. return 0;
  155. }
  156. //------------------------------------------------------------------------------
  157. int objectId(int nParams)
  158. {
  159. switch (nParams)
  160. {
  161. case 1: return FUNCTION1;
  162. case 2: return FUNCTION2;
  163. case 3: return FUNCTION3;
  164. }
  165. return IDENTIFIER;
  166. }
  167. //------------------------------------------------------------------------------
  168. int lookup(int *typeOfObject)
  169. {
  170. int i;
  171. gettoken(yytext, sizeof yytext);
  172. if (!_strnicmp(yytext,"reg",3) && strlen(yytext) == 5 && isdigit(yytext[3]) && isdigit(yytext[4]) && (i=atoi(yytext+3))>=0 && i<100)
  173. {
  174. *typeOfObject=IDENTIFIER;
  175. return i+EVAL_MAX_VARS;
  176. }
  177. for (i=0;i<EVAL_MAX_VARS;i++)
  178. if (!_strnicmp(varTable[i].name, yytext,sizeof(varTable[i].name)))
  179. {
  180. *typeOfObject = IDENTIFIER;
  181. return i;
  182. }
  183. for (i=0;getFunctionFromTable(i);i++)
  184. {
  185. functionType *f=getFunctionFromTable(i);
  186. if (!strcmpi(f->name, yytext))
  187. {
  188. *typeOfObject = objectId(f->nParams);
  189. return i;
  190. }
  191. }
  192. *typeOfObject = IDENTIFIER;
  193. setLastVar();
  194. i = setVar(-1, 0);
  195. return i;
  196. }
  197. //---------------------------------------------------------------------------
  198. void count(void)
  199. {
  200. gettoken(yytext, sizeof yytext);
  201. colCount+=strlen(yytext);
  202. }
  203. //---------------------------------------------------------------------------
  204. int yyerror(char *txt)
  205. {
  206. *errPtr = colCount;
  207. return 0;
  208. }