1
0

nseel-eval.c 8.6 KB

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