nseel-eval.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Nullsoft Expression Evaluator Library (NS-EEL)
  3. Copyright (C) 1999-2003 Nullsoft, Inc.
  4. nseel-eval.c
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <windows.h>
  20. #include "ns-eel-int.h"
  21. #define NSEEL_VARS_PER_BLOCK 64
  22. #define NSEEL_VARS_MALLOC_CHUNKSIZE 8
  23. #define NSEEL_GLOBALVAR_BASE (1<<24)
  24. #ifndef NSEEL_MAX_VARIABLE_NAMELEN
  25. #define NSEEL_MAX_VARIABLE_NAMELEN 8
  26. #endif
  27. #define INTCONST 1
  28. #define DBLCONST 2
  29. #define HEXCONST 3
  30. #define VARIABLE 4
  31. #define OTHER 5
  32. double nseel_globalregs[100];
  33. //------------------------------------------------------------------------------
  34. void *nseel_compileExpression(compileContext *ctx, char *exp)
  35. {
  36. ctx->errVar=0;
  37. nseel_llinit(ctx);
  38. if (!nseel_yyparse(ctx,exp) && !ctx->errVar)
  39. {
  40. return (void*)ctx->result;
  41. }
  42. return 0;
  43. }
  44. //------------------------------------------------------------------------------
  45. void nseel_setLastVar(compileContext *ctx)
  46. {
  47. nseel_gettoken(ctx,ctx->lastVar, sizeof(ctx->lastVar));
  48. }
  49. static int register_var(compileContext *ctx, char *name, double **ptr)
  50. {
  51. int wb;
  52. int ti=0;
  53. int i=0;
  54. char *nameptr;
  55. for (wb = 0; wb < ctx->varTable_numBlocks; wb ++)
  56. {
  57. int namepos=0;
  58. for (ti = 0; ti < NSEEL_VARS_PER_BLOCK; ti ++)
  59. {
  60. if (!ctx->varTable_Names[wb][namepos] || !_strnicmp(ctx->varTable_Names[wb]+namepos,name,NSEEL_MAX_VARIABLE_NAMELEN))
  61. {
  62. break;
  63. }
  64. namepos += NSEEL_MAX_VARIABLE_NAMELEN;
  65. i++;
  66. }
  67. if (ti < NSEEL_VARS_PER_BLOCK)
  68. break;
  69. }
  70. if (wb == ctx->varTable_numBlocks)
  71. {
  72. ti=0;
  73. // add new block
  74. if (!(ctx->varTable_numBlocks&(NSEEL_VARS_MALLOC_CHUNKSIZE-1)) || !ctx->varTable_Values || !ctx->varTable_Names )
  75. {
  76. ctx->varTable_Values = (double **)realloc(ctx->varTable_Values,(ctx->varTable_numBlocks+NSEEL_VARS_MALLOC_CHUNKSIZE) * sizeof(double *));
  77. ctx->varTable_Names = (char **)realloc(ctx->varTable_Names,(ctx->varTable_numBlocks+NSEEL_VARS_MALLOC_CHUNKSIZE) * sizeof(char *));
  78. }
  79. ctx->varTable_numBlocks++;
  80. ctx->varTable_Values[wb] = (double *)calloc(sizeof(double),NSEEL_VARS_PER_BLOCK);
  81. ctx->varTable_Names[wb] = (char *)calloc(NSEEL_MAX_VARIABLE_NAMELEN,NSEEL_VARS_PER_BLOCK);
  82. }
  83. nameptr=ctx->varTable_Names[wb]+ti*NSEEL_MAX_VARIABLE_NAMELEN;
  84. if (!nameptr[0])
  85. {
  86. strncpy(nameptr,name,NSEEL_MAX_VARIABLE_NAMELEN);
  87. }
  88. if (ptr) *ptr = ctx->varTable_Values[wb] + ti;
  89. return i;
  90. }
  91. //------------------------------------------------------------------------------
  92. int nseel_setVar(compileContext *ctx, int varNum)
  93. {
  94. if (varNum < 0) // adding new var
  95. {
  96. char *var=ctx->lastVar;
  97. if (!_strnicmp(var,"reg",3) && strlen(var) == 5 && isdigit(var[3]) && isdigit(var[4]))
  98. {
  99. int i,x=atoi(var+3);
  100. if (x < 0 || x > 99) x=0;
  101. i=NSEEL_GLOBALVAR_BASE+x;
  102. return i;
  103. }
  104. return register_var(ctx,ctx->lastVar,NULL);
  105. }
  106. // setting/getting oldvar
  107. if (varNum >= NSEEL_GLOBALVAR_BASE && varNum < NSEEL_GLOBALVAR_BASE+100)
  108. {
  109. return varNum;
  110. }
  111. {
  112. int wb,ti;
  113. char *nameptr;
  114. if (varNum < 0 || varNum >= ctx->varTable_numBlocks*NSEEL_VARS_PER_BLOCK) return -1;
  115. wb=varNum/NSEEL_VARS_PER_BLOCK;
  116. ti=(varNum%NSEEL_VARS_PER_BLOCK);
  117. nameptr=ctx->varTable_Names[wb]+ti*NSEEL_MAX_VARIABLE_NAMELEN;
  118. if (!nameptr[0])
  119. {
  120. strncpy(nameptr,ctx->lastVar,NSEEL_MAX_VARIABLE_NAMELEN);
  121. }
  122. return varNum;
  123. }
  124. }
  125. //------------------------------------------------------------------------------
  126. int nseel_getVar(compileContext *ctx, int i)
  127. {
  128. if (i >= 0 && i < (NSEEL_VARS_PER_BLOCK*ctx->varTable_numBlocks))
  129. return nseel_createCompiledValue(ctx,0, ctx->varTable_Values[i/NSEEL_VARS_PER_BLOCK] + i%NSEEL_VARS_PER_BLOCK);
  130. if (i >= NSEEL_GLOBALVAR_BASE && i < NSEEL_GLOBALVAR_BASE+100)
  131. return nseel_createCompiledValue(ctx,0, nseel_globalregs+i-NSEEL_GLOBALVAR_BASE);
  132. return nseel_createCompiledValue(ctx,0, NULL);
  133. }
  134. //------------------------------------------------------------------------------
  135. double *NSEEL_VM_regvar(NSEEL_VMCTX _ctx, char *var)
  136. {
  137. compileContext *ctx = (compileContext *)_ctx;
  138. double *r;
  139. if (!ctx) return 0;
  140. if (!_strnicmp(var,"reg",3) && strlen(var) == 5 && isdigit(var[3]) && isdigit(var[4]))
  141. {
  142. int x=atoi(var+3);
  143. if (x < 0 || x > 99) x=0;
  144. return nseel_globalregs + x;
  145. }
  146. register_var(ctx,var,&r);
  147. return r;
  148. }
  149. //------------------------------------------------------------------------------
  150. int nseel_translate(compileContext *ctx, int type)
  151. {
  152. int v;
  153. int n;
  154. *ctx->yytext = 0;
  155. nseel_gettoken(ctx,ctx->yytext, sizeof(ctx->yytext));
  156. switch (type)
  157. {
  158. case INTCONST: return nseel_createCompiledValue(ctx,(double)atoi(ctx->yytext), NULL);
  159. case DBLCONST: return nseel_createCompiledValue(ctx,(double)atof(ctx->yytext), NULL);
  160. case HEXCONST:
  161. v=0;
  162. n=0;
  163. while (1)
  164. {
  165. int a=ctx->yytext[n++];
  166. if (a >= '0' && a <= '9') v+=a-'0';
  167. else if (a >= 'A' && a <= 'F') v+=10+a-'A';
  168. else if (a >= 'a' && a <= 'f') v+=10+a-'a';
  169. else break;
  170. v<<=4;
  171. }
  172. return nseel_createCompiledValue(ctx,(double)v, NULL);
  173. }
  174. return 0;
  175. }
  176. //------------------------------------------------------------------------------
  177. int nseel_lookup(compileContext *ctx, int *typeOfObject)
  178. {
  179. int i;
  180. nseel_gettoken(ctx,ctx->yytext, sizeof(ctx->yytext));
  181. if (!_strnicmp(ctx->yytext,"reg",3) && strlen(ctx->yytext) == 5 && isdigit(ctx->yytext[3]) && isdigit(ctx->yytext[4]) && (i=atoi(ctx->yytext+3))>=0 && i<100)
  182. {
  183. *typeOfObject=IDENTIFIER;
  184. return i+NSEEL_GLOBALVAR_BASE;
  185. }
  186. {
  187. int wb;
  188. int ti=0;
  189. i=0;
  190. for (wb = 0; wb < ctx->varTable_numBlocks; wb ++)
  191. {
  192. int namepos=0;
  193. for (ti = 0; ti < NSEEL_VARS_PER_BLOCK; ti ++)
  194. {
  195. if (!ctx->varTable_Names[wb][namepos]) break;
  196. if (!_strnicmp(ctx->varTable_Names[wb]+namepos,ctx->yytext,NSEEL_MAX_VARIABLE_NAMELEN))
  197. {
  198. *typeOfObject = IDENTIFIER;
  199. return i;
  200. }
  201. namepos += NSEEL_MAX_VARIABLE_NAMELEN;
  202. i++;
  203. }
  204. if (ti < NSEEL_VARS_PER_BLOCK) break;
  205. }
  206. }
  207. for (i=0;nseel_getFunctionFromTable(i);i++)
  208. {
  209. functionType *f=nseel_getFunctionFromTable(i);
  210. if (!strcmpi(f->name, ctx->yytext))
  211. {
  212. switch (f->nParams)
  213. {
  214. case 1: *typeOfObject = FUNCTION1; break;
  215. case 2: *typeOfObject = FUNCTION2; break;
  216. case 3: *typeOfObject = FUNCTION3; break;
  217. default: *typeOfObject = IDENTIFIER; break;
  218. }
  219. return i;
  220. }
  221. }
  222. *typeOfObject = IDENTIFIER;
  223. nseel_setLastVar(ctx);
  224. return nseel_setVar(ctx,-1);
  225. }
  226. //---------------------------------------------------------------------------
  227. void nseel_count(compileContext *ctx)
  228. {
  229. nseel_gettoken(ctx,ctx->yytext, sizeof(ctx->yytext));
  230. ctx->colCount+=strlen(ctx->yytext);
  231. }
  232. //---------------------------------------------------------------------------
  233. int nseel_yyerror(compileContext *ctx)
  234. {
  235. ctx->errVar = ctx->colCount;
  236. return 0;
  237. }