1
0

cal.y 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. %{
  2. #define YYSTYPE double
  3. #include <malloc.h>
  4. #include <memory.h>
  5. #include "Compiler.h"
  6. #include "eval.h"
  7. yyerror(char *);
  8. yylex();
  9. extern int yyStackSize;
  10. extern double result;
  11. int regs[26];
  12. int base;
  13. %}
  14. %token VALUE IDENTIFIER FUNCTION1 FUNCTION2 FUNCTION3
  15. %left '|'
  16. %left '&'
  17. %left '+' '-'
  18. %left '*' '/' '%'
  19. %left UMINUS /*supplies precedence for unary minus */
  20. %left UPLUS /*supplies precedence for unary plus */
  21. %% /*beginning of rules section */
  22. stat : math_expr
  23. { $$ = $1; result = $1; }
  24. | IDENTIFIER '=' math_expr
  25. { if (parseType == PARSE_EVAL)
  26. {
  27. setVar((int)$1, $3);
  28. $$ = $3;
  29. result = $3;
  30. }
  31. else
  32. {
  33. double i = setVar((int)$1, 0);
  34. double v = createCompiledValue(0, &(varTable[(int)i].value));
  35. $$ = createCompiledFunction2(MATH_SIMPLE, FN_ASSIGN, v, $3);
  36. result = $$;
  37. }
  38. }
  39. ;
  40. value : VALUE { $$ = $1 }
  41. primary_expr
  42. : IDENTIFIER
  43. { $$ = getVar((int)$1);}
  44. | value
  45. { $$ = $1;}
  46. | '(' math_expr ')'
  47. { $$ = $2;}
  48. ;
  49. math_expr
  50. : primary_expr
  51. { $$ = $1; }
  52. | math_expr '*' math_expr
  53. { if (parseType == PARSE_EVAL)
  54. $$ = $1 * $3;
  55. else
  56. $$ = createCompiledFunction2(MATH_SIMPLE, FN_MULTIPLY, $1, $3);
  57. }
  58. | math_expr '/' math_expr
  59. { if (parseType == PARSE_EVAL)
  60. $$ = $1 / $3;
  61. else
  62. $$ = createCompiledFunction2(MATH_SIMPLE, FN_DIVIDE, $1, $3);
  63. }
  64. | math_expr '%' math_expr
  65. { if (parseType == PARSE_EVAL)
  66. $$ = (double)((int)$1 % (int)$3);
  67. else
  68. $$ = createCompiledFunction2(MATH_SIMPLE, FN_MODULO, $1, $3);
  69. }
  70. | math_expr '+' math_expr
  71. { if (parseType == PARSE_EVAL)
  72. $$ = $1 + $3;
  73. else
  74. $$ = createCompiledFunction2(MATH_SIMPLE, FN_ADD, $1, $3);
  75. }
  76. | math_expr '-' math_expr
  77. { if (parseType == PARSE_EVAL)
  78. $$ = $1 - $3;
  79. else
  80. $$ = createCompiledFunction2(MATH_SIMPLE, FN_SUB, $1, $3);
  81. }
  82. | math_expr '&' math_expr
  83. { if (parseType == PARSE_EVAL)
  84. $$ = (double)((int)$1 & (int)$3);
  85. else
  86. $$ = createCompiledFunction2(MATH_SIMPLE, FN_AND, $1, $3);
  87. }
  88. | math_expr '|' math_expr
  89. { if (parseType == PARSE_EVAL)
  90. $$ = (double)((int)$1 | (int)$3);
  91. else
  92. $$ = createCompiledFunction2(MATH_SIMPLE, FN_OR, $1, $3);
  93. }
  94. | '-' math_expr %prec UMINUS
  95. { if (parseType == PARSE_EVAL)
  96. $$ = -$2;
  97. else
  98. $$ = createCompiledFunction1(MATH_SIMPLE, FN_UMINUS, $2);
  99. }
  100. | '+' math_expr %prec UPLUS
  101. { if (parseType == PARSE_EVAL)
  102. $$ = +$2;
  103. else
  104. $$ = createCompiledFunction1(MATH_SIMPLE, FN_UPLUS, $2);
  105. }
  106. | fonction
  107. { $$ = $1; }
  108. ;
  109. fonction
  110. : FUNCTION1 '(' math_expr ')'
  111. { if (parseType == PARSE_EVAL)
  112. $$ = calcFunction1((int)$1, $3);
  113. else
  114. $$ = createCompiledFunction1(MATH_FN, (int)$1, $3);
  115. }
  116. | FUNCTION2 '(' math_expr ',' math_expr ')'
  117. { if (parseType == PARSE_EVAL)
  118. $$ = calcFunction2((int)$1, $3, $5);
  119. else
  120. $$ = createCompiledFunction2(MATH_FN, (int)$1, $3, $5);
  121. }
  122. | FUNCTION3 '(' math_expr ',' math_expr ',' math_expr ')'
  123. { if (parseType == PARSE_EVAL)
  124. $$ = calcFunction3((int)$1, $3, $5, $7);
  125. else
  126. $$ = createCompiledFunction3(MATH_FN, (int)$1, $3, $5, $7);
  127. }
  128. ;
  129. %%
  130. main()
  131. {
  132. return(yyparse());
  133. }
  134. yywrap()
  135. {
  136. return(1);
  137. }