nseel-caltab.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. Nullsoft Expression Evaluator Library (NS-EEL)
  3. Copyright (C) 1999-2003 Nullsoft, Inc.
  4. nseel-caltab.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 "ns-eel-int.h"
  20. #define VALUE 258
  21. #define IDENTIFIER 259
  22. #define FUNCTION1 260
  23. #define FUNCTION2 261
  24. #define FUNCTION3 262
  25. #define UMINUS 263
  26. #define UPLUS 264
  27. #define YYSTYPE int
  28. #define YYERROR(x) nseel_yyerror(ctx)
  29. #define YYFINAL 51
  30. #define YYFLAG -32768
  31. #define YYNTBASE 21
  32. #define YYTRANSLATE(x) ((unsigned)(x) <= 264 ? yytranslate[x] : 26)
  33. static const char yytranslate[] = { 0,
  34. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  35. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  36. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  37. 2, 2, 2, 2, 2, 2, 14, 9, 2, 18,
  38. 19, 12, 10, 20, 11, 2, 13, 2, 2, 2,
  39. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  40. 17, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  41. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  42. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  43. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  44. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  45. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  46. 2, 2, 2, 8, 2, 2, 2, 2, 2, 2,
  47. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  48. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  49. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  50. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  51. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  52. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  53. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  54. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  59. 2, 2, 2, 2, 2, 1, 2, 3, 4, 5,
  60. 6, 7, 15, 16
  61. };
  62. static const short yyr1[] = { 0,
  63. 21, 21, 22, 23, 23, 23, 24, 24, 24, 24,
  64. 24, 24, 24, 24, 24, 24, 24, 25, 25, 25
  65. };
  66. static const short yyr2[] = { 0,
  67. 1, 3, 1, 1, 1, 3, 1, 3, 3, 3,
  68. 3, 3, 3, 3, 2, 2, 1, 4, 6, 8
  69. };
  70. static const short yydefact[] = { 0,
  71. 3, 4, 0, 0, 0, 0, 0, 0, 5, 7,
  72. 1, 17, 0, 0, 0, 0, 4, 16, 15, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
  74. 0, 6, 14, 13, 11, 12, 8, 9, 10, 18,
  75. 0, 0, 0, 0, 19, 0, 0, 20, 0, 0,
  76. 0
  77. };
  78. static const short yydefgoto[] = { 49,
  79. 9, 10, 11, 12
  80. };
  81. static const short yypact[] = { 19,
  82. -32768, -11, -7, -5, -4, 38, 38, 38,-32768,-32768,
  83. 136,-32768, 38, 38, 38, 38,-32768,-32768,-32768, 88,
  84. 38, 38, 38, 38, 38, 38, 38, 136, 100, 49,
  85. 62,-32768, 41, 54, -9, -9,-32768,-32768,-32768,-32768,
  86. 38, 38, 112, 75,-32768, 38, 124,-32768, 12, 27,
  87. -32768
  88. };
  89. static const short yypgoto[] = {-32768,
  90. -32768,-32768, -6,-32768
  91. };
  92. #define YYLAST 150
  93. static const short yytable[] = { 18,
  94. 19, 20, 25, 26, 27, 13, 28, 29, 30, 31,
  95. 14, 50, 15, 16, 33, 34, 35, 36, 37, 38,
  96. 39, 1, 2, 3, 4, 5, 51, 0, 6, 7,
  97. 0, 0, 0, 0, 43, 44, 8, 0, 0, 47,
  98. 1, 17, 3, 4, 5, 0, 0, 6, 7, 22,
  99. 23, 24, 25, 26, 27, 8, 21, 22, 23, 24,
  100. 25, 26, 27, 23, 24, 25, 26, 27, 41, 21,
  101. 22, 23, 24, 25, 26, 27, 0, 0, 0, 0,
  102. 0, 42, 21, 22, 23, 24, 25, 26, 27, 0,
  103. 0, 0, 0, 0, 46, 21, 22, 23, 24, 25,
  104. 26, 27, 0, 0, 0, 0, 32, 21, 22, 23,
  105. 24, 25, 26, 27, 0, 0, 0, 0, 40, 21,
  106. 22, 23, 24, 25, 26, 27, 0, 0, 0, 0,
  107. 45, 21, 22, 23, 24, 25, 26, 27, 0, 0,
  108. 0, 0, 48, 21, 22, 23, 24, 25, 26, 27
  109. };
  110. static const short yycheck[] = { 6,
  111. 7, 8, 12, 13, 14, 17, 13, 14, 15, 16,
  112. 18, 0, 18, 18, 21, 22, 23, 24, 25, 26,
  113. 27, 3, 4, 5, 6, 7, 0, -1, 10, 11,
  114. -1, -1, -1, -1, 41, 42, 18, -1, -1, 46,
  115. 3, 4, 5, 6, 7, -1, -1, 10, 11, 9,
  116. 10, 11, 12, 13, 14, 18, 8, 9, 10, 11,
  117. 12, 13, 14, 10, 11, 12, 13, 14, 20, 8,
  118. 9, 10, 11, 12, 13, 14, -1, -1, -1, -1,
  119. -1, 20, 8, 9, 10, 11, 12, 13, 14, -1,
  120. -1, -1, -1, -1, 20, 8, 9, 10, 11, 12,
  121. 13, 14, -1, -1, -1, -1, 19, 8, 9, 10,
  122. 11, 12, 13, 14, -1, -1, -1, -1, 19, 8,
  123. 9, 10, 11, 12, 13, 14, -1, -1, -1, -1,
  124. 19, 8, 9, 10, 11, 12, 13, 14, -1, -1,
  125. -1, -1, 19, 8, 9, 10, 11, 12, 13, 14
  126. };
  127. #define yyerrok (yyerrstatus = 0)
  128. #define yyclearin (ctx->yychar = YYEMPTY)
  129. #define YYEMPTY -2
  130. #define YYEOF 0
  131. #define YYACCEPT return(0)
  132. #define YYABORT return(1)
  133. #define YYTERROR 1
  134. #define YYERRCODE 256
  135. #define YYLEX nseel_yylex(ctx,&exp)
  136. /* If nonreentrant, generate the variables here */
  137. /* YYINITDEPTH indicates the initial size of the parser's stacks */
  138. #define YYINITDEPTH 5000
  139. #define YYMAXDEPTH 5000
  140. /* This is the most reliable way to avoid incompatibilities
  141. in available built-in functions on various systems. */
  142. #define __yy_bcopy(from,to,count) memcpy(to,from,(count)>0?(count):0)
  143. //#ln 131 "bison.simple"
  144. int nseel_yyparse(compileContext *ctx, char *exp)
  145. {
  146. int yystate;
  147. int yyn;
  148. short *yyssp;
  149. YYSTYPE *yyvsp;
  150. int yyerrstatus; /* number of tokens to shift before error messages enabled */
  151. int yychar1; /* lookahead token as an internal (translated) token number */
  152. short yyssa[YYINITDEPTH]; /* the state stack */
  153. YYSTYPE yyvsa[YYINITDEPTH]; /* the semantic value stack */
  154. short *yyss = yyssa; /* refer to the stacks thru separate pointers */
  155. YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */
  156. int yystacksize = YYINITDEPTH;
  157. YYSTYPE yyval; /* the variable used to return */
  158. /* semantic values from the action */
  159. /* routines */
  160. int yylen;
  161. ctx->yylval = 0;
  162. yystate = 0;
  163. yyerrstatus = 0;
  164. ctx->yynerrs = 0;
  165. ctx->yychar = YYEMPTY; /* Cause a token to be read. */
  166. /* Initialize stack pointers.
  167. Waste one element of value and location stack
  168. so that they stay on the same level as the state stack. */
  169. yyssp = yyss - 1;
  170. yyvsp = yyvs;
  171. /* Push a new state, which is found in yystate . */
  172. /* In all cases, when you get here, the value and location stacks
  173. have just been pushed. so pushing a state here evens the stacks. */
  174. yynewstate:
  175. *++yyssp = yystate;
  176. if (yyssp >= yyss + yystacksize - 1)
  177. {
  178. /* Give user a chance to reallocate the stack */
  179. /* Use copies of these so that the &'s don't force the real ones into memory. */
  180. YYSTYPE *yyvs1 = yyvs;
  181. short *yyss1 = yyss;
  182. /* Get the current used size of the three stacks, in elements. */
  183. int size = yyssp - yyss + 1;
  184. if (yystacksize >= YYMAXDEPTH)
  185. {
  186. YYERROR("internal error: parser stack overflow");
  187. return 2;
  188. }
  189. yyssp = yyss + size - 1;
  190. yyvsp = yyvs + size - 1;
  191. if (yyssp >= yyss + yystacksize - 1) YYABORT;
  192. }
  193. // yybackup:
  194. /* Do appropriate processing given the current state. */
  195. /* Read a lookahead token if we need one and don't already have one. */
  196. /* yyresume: */
  197. /* First try to decide what to do without reference to lookahead token. */
  198. yyn = yypact[yystate];
  199. if (yyn == YYFLAG)
  200. goto yydefault;
  201. /* Not known => get a lookahead token if don't already have one. */
  202. /* yychar is either YYEMPTY or YYEOF
  203. or a valid token in external form. */
  204. if (ctx->yychar == YYEMPTY)
  205. {
  206. // yyStackSize = yyssp - (yyss - 1);
  207. ctx->yychar = YYLEX;
  208. }
  209. /* Convert token to internal form (in yychar1) for indexing tables with */
  210. if (ctx->yychar <= 0) /* This means end of input. */
  211. {
  212. yychar1 = 0;
  213. ctx->yychar = YYEOF; /* Don't call YYLEX any more */
  214. }
  215. else
  216. {
  217. yychar1 = YYTRANSLATE(ctx->yychar);
  218. }
  219. yyn += yychar1;
  220. if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
  221. goto yydefault;
  222. yyn = yytable[yyn];
  223. /* yyn is what to do for this token type in this state.
  224. Negative => reduce, -yyn is rule number.
  225. Positive => shift, yyn is new state.
  226. New state is final state => don't bother to shift,
  227. just return success.
  228. 0, or most negative number => error. */
  229. if (yyn < 0)
  230. {
  231. if (yyn == YYFLAG)
  232. goto yyerrlab;
  233. yyn = -yyn;
  234. goto yyreduce;
  235. }
  236. else if (yyn == 0)
  237. goto yyerrlab;
  238. if (yyn == YYFINAL)
  239. YYACCEPT;
  240. /* Shift the lookahead token. */
  241. /* Discard the token being shifted unless it is eof. */
  242. if (ctx->yychar != YYEOF)
  243. ctx->yychar = YYEMPTY;
  244. *++yyvsp = ctx->yylval;
  245. /* count tokens shifted since error; after three, turn off error status. */
  246. if (yyerrstatus) yyerrstatus--;
  247. yystate = yyn;
  248. goto yynewstate;
  249. /* Do the default action for the current state. */
  250. yydefault:
  251. yyn = yydefact[yystate];
  252. if (yyn == 0)
  253. goto yyerrlab;
  254. /* Do a reduction. yyn is the number of a rule to reduce with. */
  255. yyreduce:
  256. yylen = yyr2[yyn];
  257. yyval = yyvsp[1-yylen]; /* implement default value of the action */
  258. switch (yyn) {
  259. case 1:
  260. //#ln 32 "cal.y"
  261. { yyval = yyvsp[0]; ctx->result = yyvsp[0]; ;
  262. break;}
  263. case 2:
  264. //#ln 34 "cal.y"
  265. { {
  266. int i = (int)nseel_setVar(ctx,(int)yyvsp[-2]);
  267. int v=nseel_getVar(ctx,i);
  268. yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_ASSIGN, v, (int)yyvsp[0]);
  269. ctx->result = yyval;
  270. }
  271. ;
  272. break;}
  273. case 3:
  274. //#ln 50 "cal.y"
  275. { yyval = yyvsp[0] ;
  276. break;}
  277. case 4:
  278. //#ln 55 "cal.y"
  279. { yyval = nseel_getVar(ctx,(int)yyvsp[0]);;
  280. break;}
  281. case 5:
  282. //#ln 57 "cal.y"
  283. { yyval = yyvsp[0];;
  284. break;}
  285. case 6:
  286. //#ln 59 "cal.y"
  287. { yyval = yyvsp[-1];;
  288. break;}
  289. case 7:
  290. //#ln 64 "cal.y"
  291. { yyval = yyvsp[0]; ;
  292. break;}
  293. case 8:
  294. //#ln 66 "cal.y"
  295. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_MULTIPLY, yyvsp[-2], yyvsp[0]);
  296. break;}
  297. case 9:
  298. //#ln 72 "cal.y"
  299. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_DIVIDE, yyvsp[-2], yyvsp[0]);
  300. break;}
  301. case 10:
  302. //#ln 78 "cal.y"
  303. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_MODULO, yyvsp[-2], yyvsp[0]);
  304. break;}
  305. case 11:
  306. //#ln 84 "cal.y"
  307. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_ADD, yyvsp[-2], yyvsp[0]);
  308. break;}
  309. case 12:
  310. //#ln 90 "cal.y"
  311. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_SUB, yyvsp[-2], yyvsp[0]);
  312. break;}
  313. case 13:
  314. //#ln 96 "cal.y"
  315. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_AND, yyvsp[-2], yyvsp[0]);
  316. break;}
  317. case 14:
  318. //#ln 102 "cal.y"
  319. { yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_OR, yyvsp[-2], yyvsp[0]);
  320. break;}
  321. case 15:
  322. //#ln 108 "cal.y"
  323. { yyval = nseel_createCompiledFunction1(ctx,MATH_SIMPLE, FN_UMINUS, yyvsp[0]);
  324. break;}
  325. case 16:
  326. //#ln 114 "cal.y"
  327. { yyval = nseel_createCompiledFunction1(ctx,MATH_SIMPLE, FN_UPLUS, yyvsp[0]);
  328. break;}
  329. case 17:
  330. //#ln 120 "cal.y"
  331. { yyval = yyvsp[0];
  332. break;}
  333. case 18:
  334. //#ln 125 "cal.y"
  335. { yyval = nseel_createCompiledFunction1(ctx,MATH_FN, (int)yyvsp[-3], yyvsp[-1]);
  336. break;}
  337. case 19:
  338. //#ln 131 "cal.y"
  339. { yyval = nseel_createCompiledFunction2(ctx,MATH_FN, (int)yyvsp[-5], yyvsp[-3], yyvsp[-1]);
  340. break;}
  341. case 20:
  342. //#ln 137 "cal.y"
  343. { yyval = nseel_createCompiledFunction3(ctx,MATH_FN, (int)yyvsp[-7], yyvsp[-5], yyvsp[-3], yyvsp[-1]);
  344. break;}
  345. }
  346. /* the action file gets copied in in place of this dollarsign */
  347. //#ln 362 "bison.simple"
  348. yyvsp -= yylen;
  349. yyssp -= yylen;
  350. *++yyvsp = yyval;
  351. /* Now "shift" the result of the reduction.
  352. Determine what state that goes to,
  353. based on the state we popped back to
  354. and the rule number reduced by. */
  355. yyn = yyr1[yyn];
  356. yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  357. if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
  358. yystate = yytable[yystate];
  359. else
  360. yystate = yydefgoto[yyn - YYNTBASE];
  361. goto yynewstate;
  362. yyerrlab: /* here on detecting error */
  363. if (! yyerrstatus)
  364. /* If not already recovering from an error, report this error. */
  365. {
  366. ++ctx->yynerrs;
  367. #ifdef YYERROR_VERBOSE
  368. yyn = yypact[yystate];
  369. if (yyn > YYFLAG && yyn < YYLAST)
  370. {
  371. int size = 0;
  372. char *msg;
  373. int x, count;
  374. count = 0;
  375. for (x = 0; x < (sizeof(yytname) / sizeof(char *)); x++)
  376. if (yycheck[x + yyn] == x)
  377. size += strlen(yytname[x]) + 15, count++;
  378. #error this should not compile
  379. msg = (char *) xmalloc(size + 15);
  380. strcpy(msg, "syntax error");
  381. if (count < 5)
  382. {
  383. count = 0;
  384. for (x = 0; x < (sizeof(yytname) / sizeof(char *)); x++)
  385. if (yycheck[x + yyn] == x)
  386. {
  387. strcat(msg, count == 0 ? ", expecting `" : " or `");
  388. strcat(msg, yytname[x]);
  389. strcat(msg, "'");
  390. count++;
  391. }
  392. }
  393. YYERROR(msg);
  394. free(msg);
  395. }
  396. else
  397. #endif /* YYERROR_VERBOSE */
  398. YYERROR("syntax error");
  399. }
  400. //yyerrlab1: /* here on error raised explicitly by an action */
  401. if (yyerrstatus == 3)
  402. {
  403. /* if just tried and failed to reuse lookahead token after an error, discard it. */
  404. /* return failure if at end of input */
  405. if (ctx->yychar == YYEOF) YYABORT;
  406. ctx->yychar = YYEMPTY;
  407. }
  408. /* Else will try to reuse lookahead token
  409. after shifting the error token. */
  410. yyerrstatus = 3; /* Each real token shifted decrements this */
  411. goto yyerrhandle;
  412. yyerrdefault: /* current state does not do anything special for the error token. */
  413. #if 0
  414. /* This is wrong; only states that explicitly want error tokens
  415. should shift them. */
  416. yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/
  417. if (yyn) goto yydefault;
  418. #endif
  419. yyerrpop: /* pop the current state because it cannot handle the error token */
  420. if (yyssp == yyss) YYABORT;
  421. yyvsp--;
  422. yystate = *--yyssp;
  423. yyerrhandle:
  424. yyn = yypact[yystate];
  425. if (yyn == YYFLAG)
  426. goto yyerrdefault;
  427. yyn += YYTERROR;
  428. if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
  429. goto yyerrdefault;
  430. yyn = yytable[yyn];
  431. if (yyn < 0)
  432. {
  433. if (yyn == YYFLAG)
  434. goto yyerrpop;
  435. yyn = -yyn;
  436. goto yyreduce;
  437. }
  438. else if (yyn == 0)
  439. goto yyerrpop;
  440. if (yyn == YYFINAL)
  441. YYACCEPT;
  442. *++yyvsp = ctx->yylval;
  443. yystate = yyn;
  444. goto yynewstate;
  445. }