logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://hacktivis.me/git/libbulletml.git

calc.yy (3268B)


  1. /* Ãæ´Öµ­Ë¡ÅÅÂî -- calc */
  2. %{
  3. typedef double NumType;
  4. #define YYSTYPE double
  5. #define YYERROR_VERBOSE
  6. #include <cmath>
  7. #include <cctype>
  8. #include <cstring>
  9. #include <vector>
  10. #include <sstream>
  11. #include <string.h>
  12. #include "calc.h"
  13. #include "formula.h"
  14. #include "formula-variables.h"
  15. extern void yyerror(const char* s);
  16. extern int yylex();
  17. const char* yyinStr;
  18. typedef Formula<NumType> CalcFormula;
  19. typedef Number<NumType> CalcNumber;
  20. typedef Random<NumType> CalcRandom;
  21. typedef Rank<NumType> CalcRank;
  22. typedef Param<NumType> CalcParam;
  23. typedef Operator CalcOperator;
  24. namespace {
  25. CalcFormula* formula;
  26. std::vector<CalcFormula*> formulas;
  27. CalcFormula* f(double d) { return formulas[(int)d]; }
  28. int paramId;
  29. }
  30. %}
  31. /* BISONÀë¸À */
  32. %token NUM RAND RANK PARAM
  33. %left '-' '+'
  34. %left '*' '/'
  35. %left NEG /* negation--ñ¹à¥Þ¥¤¥Ê¥¹ */
  36. %right '^' /* ¤Ù¤­¾è´Ø¿ô */
  37. /* ʸˡµ¬Â§¤¬Â³¤¯ */
  38. %%
  39. input: /* ¶õʸ»úÎó */
  40. | input line
  41. ;
  42. line: '\n'
  43. | exp '\n' { formula = f($1); return 0; }
  44. ;
  45. exp: NUM {
  46. $$ = formulas.size();
  47. formulas.push_back(new CalcFormula(new CalcNumber($1)));
  48. }
  49. | RAND {
  50. $$ = formulas.size();
  51. formulas.push_back(new CalcFormula(new CalcRandom()));
  52. }
  53. | RANK {
  54. $$ = formulas.size();
  55. formulas.push_back(new CalcFormula(new CalcRank()));
  56. }
  57. | PARAM {
  58. $$ = formulas.size();
  59. formulas.push_back(new CalcFormula(new CalcParam(paramId)));
  60. }
  61. | exp '+' exp {
  62. $$ = formulas.size();
  63. formulas.push_back(new CalcFormula(f($1), op_add, f($3)));
  64. }
  65. | exp '-' exp {
  66. $$ = formulas.size();
  67. formulas.push_back(new CalcFormula(f($1), op_sub, f($3)));
  68. }
  69. | exp '*' exp {
  70. $$ = formulas.size();
  71. formulas.push_back(new CalcFormula(f($1), op_mul, f($3)));
  72. }
  73. | exp '/' exp {
  74. $$ = formulas.size();
  75. formulas.push_back(new CalcFormula(f($1), op_div, f($3)));
  76. }
  77. | '-' exp %prec NEG {
  78. $$ = $2;
  79. f($2)->setHeadSub();
  80. }
  81. | '(' exp ')' {
  82. $$ = $2;
  83. }
  84. ;
  85. %%
  86. /**
  87. * »ú¶ç²òÀÏ´ï¤Ï¡¢¿ôÃͤòÆɤá¤Ð¡¢double·¿¤ÎÃͤò¥¹¥¿¥Ã¥¯¤ËÀѤó¤Ç
  88. * ¥È¡¼¥¯¥ó¡ÖNUM¡×¤òÊÖ¤·¡¢¿ôÃͰʳ°¤òÆɤá¤Ð¡¢¤½¤Îʸ»ú¤Î¥¢¥¹¥­¡¼Éä¹æ¤òÊÖ¤¹¡£
  89. * ¶õÇò¤È¥¿¥Ö¤ÏÆɤßÈô¤Ð¤µ¤ì¤ë¡£¥Õ¥¡¥¤¥ë¤¬½ª¤ï¤ë¤È0¤òÊÖ¤¹¡£
  90. */
  91. #include <ctype.h>
  92. #include <stdio.h>
  93. int yylex ()
  94. {
  95. int c;
  96. /* ¶õÇòÎà¤òÆɤßÈô¤Ð¤¹ */
  97. while ((c = *(yyinStr++)) == ' ' || c == '\t')
  98. ;
  99. /* ¿ôÃͤò½èÍý¤¹¤ë */
  100. if (c == '.' || isdigit (c))
  101. {
  102. yyinStr--;
  103. sscanf (yyinStr, "%lf", &yylval);
  104. while ((c = *(++yyinStr)) == '.' || isdigit(c)) {}
  105. return NUM;
  106. }
  107. // ÊÑ¿ô¤ò½èÍý¤¹¤ë */
  108. if (c == '$') {
  109. if (strncmp(yyinStr, "rand", 4) == 0) {
  110. yyinStr += 4;
  111. return RAND;
  112. }
  113. else if (strncmp(yyinStr, "rank", 4) == 0) {
  114. yyinStr += 4;
  115. return RANK;
  116. }
  117. else {
  118. std::istringstream iss(std::string(yyinStr).substr(0, 1));
  119. iss >> paramId;
  120. yyinStr++;
  121. return PARAM;
  122. }
  123. }
  124. /* ¥Õ¥¡¥¤¥ë¤Î½ª¤ï¤ê¤ò½èÍý¤¹¤ë */
  125. if (c == '\0')
  126. return 0;
  127. /* 1ʸ»ú¤òÊÖ¤¹ */
  128. return c;
  129. }
  130. void yyerror(const char* s) {
  131. printf("yyerror: %s\n", s);
  132. }
  133. std::auto_ptr<CalcFormula> calc(const std::string& str) {
  134. std::string fml = str + '\n';
  135. yyinStr = fml.c_str();
  136. yyparse();
  137. return std::auto_ptr<CalcFormula>(formula);
  138. }