CAL_TAB.C 17 KB

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