home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / misc / mcalc / source / parser.y < prev    next >
Encoding:
Lex Description  |  1994-08-18  |  7.3 KB  |  230 lines

  1. %{
  2. /*
  3. Auto:        smake MCalc
  4. */
  5.  
  6. static void    __yy_bcopy(char *from, char *to, int count);
  7.  
  8. #undef        alloca
  9. #define        alloca(x) AllocVecPool(ParsePool, x)
  10. #undef        malloc
  11. #define        malloc(x) AllocVecPool(ParsePool, x)
  12. #undef        free
  13. #define        free(x) FreeVecPool(ParsePool, x)
  14.  
  15. #undef        YYINITDEPTH
  16. #define        YYINITDEPTH 512
  17.  
  18. #undef        _STDC_
  19. #define        _STDC_
  20.  
  21. #undef        error
  22. #define        error
  23.  
  24. extern        APTR        ParsePool;
  25. extern        double        Value;
  26. extern        double        XMem, YMem, ZMem;
  27.  
  28. #define        outputr(x)    {Value = x;}
  29. #define        outputx(x)    {Value = XMem = x;}
  30. #define        outputy(x)    {Value = YMem = x;}
  31. #define        outputz(x)    {Value = ZMem = x;}
  32. #define        errout(x)    ;
  33. #define        erroutm(x, y)    ;
  34.  
  35.  
  36. /**********************************************************************/
  37. /*                    Chars read and read-position                    */
  38. /**********************************************************************/
  39. extern    UWORD    PColumn;
  40. extern    UWORD    PCharRead;
  41.  
  42.  
  43.     // This is for the wanted angle
  44.  
  45. extern    UWORD        IntAngle;
  46.  
  47.  
  48. /**********************************************************************/
  49. /*                            Error-Value                             */
  50. /**********************************************************************/
  51. UWORD        PError;
  52. %}
  53.  
  54.  
  55.  
  56. /**********************************************************************/
  57. /*                       Possible Return-Value                        */
  58. /**********************************************************************/
  59. %union
  60. {
  61.     double    Real;
  62. }
  63.  
  64.  
  65. /**********************************************************************/
  66. /*                      Token which returns Real                      */
  67. /**********************************************************************/
  68. %token <Real>   INT_CONSTANT
  69. %token <Real>   X_MEM Y_MEM Z_MEM
  70.  
  71.  
  72.  
  73. /**********************************************************************/
  74. /*                       Non-Priority functions                       */
  75. /**********************************************************************/
  76. %token EQU_OP OPEN_OP CLOSE_OP
  77.  
  78.  
  79.  
  80. /**********************************************************************/
  81. /*                           Set priorities                           */
  82. /**********************************************************************/
  83. %left AND_OP OR_OP XOR_OP ASL ASR LSL LSR ROL ROR
  84. %left ADD_OP SUB_OP
  85. %left MUL_OP DIV_OP MOD_OP
  86. %left NEG_OP NOT_OP PERCENT CHPERCENT TPERCENT
  87. %left POW
  88. %nonassoc MY_ABS COS SIN TAN ACOS ASIN ATAN EXP LOG LOG10 SQRT SINH COSH TANH FAK COT
  89.  
  90.  
  91.  
  92. /**********************************************************************/
  93. /*                    Return-Value of my terminal                     */
  94. /**********************************************************************/
  95. %type <Real>   int_expr
  96.  
  97. %%
  98.  
  99.  
  100.  
  101. /**********************************************************************/
  102. /*                          Main "sentence"                           */
  103. /**********************************************************************/
  104. eingabe
  105.     : int_expr                    {outputr($1);}
  106.     | X_MEM EQU_OP int_expr                {outputx($3);}
  107.     | Y_MEM EQU_OP int_expr                {outputy($3);}
  108.     | Z_MEM EQU_OP int_expr                {outputz($3);}
  109.     | error
  110.     ;
  111.  
  112.  
  113.  
  114.  
  115. /**********************************************************************/
  116. /*                           Do the grammar                           */
  117. /**********************************************************************/
  118. int_expr
  119.     : OPEN_OP int_expr CLOSE_OP            {$$ = $2;}
  120.     | int_expr AND_OP int_expr            {$$ = (make_ulong($1)) & (make_ulong($3));}
  121.     | int_expr OR_OP int_expr            {$$ = (make_ulong($1)) | (make_ulong($3));}
  122.     | int_expr XOR_OP int_expr            {$$ = MyXOR(make_ulong($1),make_ulong($3));}
  123.     | int_expr NOT_OP AND_OP int_expr        {$$ = MyNAND(make_ulong($1), make_ulong($4));}
  124.     | int_expr NOT_OP OR_OP int_expr        {$$ = MyNOR(make_ulong($1), make_ulong($4));}
  125.     | int_expr NOT_OP XOR_OP int_expr        {$$ = MyNXOR(make_ulong($1), make_ulong($4));}
  126.     | int_expr ADD_OP int_expr PERCENT        {$$ = ($1 + (($1 * $3) / 100));}
  127.     | int_expr SUB_OP int_expr PERCENT        {$$ = ($1 - (($1 * $3) / 100));}
  128.     | int_expr MUL_OP int_expr PERCENT        {$$ = (($1 * $3) / 100);}
  129.     | int_expr CHPERCENT int_expr            {$$ = ((($3 - $1) * 100) / $1);}
  130.     | int_expr TPERCENT int_expr            {$$ = (($3 * 100) / $1);}
  131.     | int_expr ADD_OP int_expr            {$$ = $1 + $3;}
  132.     | int_expr SUB_OP int_expr            {$$ = $1 - $3;}
  133.     | int_expr MUL_OP int_expr            {$$ = $1 * $3;}
  134.     | int_expr DIV_OP int_expr            {if($3 == 0.0) {PError = ERR_DIVBY0; yyerror(NULL); } else $$ = $1 / $3;}
  135.     | int_expr MOD_OP int_expr            {if($3 == 0.0) {PError = ERR_DIVBY0; yyerror(NULL); } else $$ = fmod($1,$3);}
  136.     | SUB_OP int_expr %prec NEG_OP            {$$ = -$2; }
  137.     | NOT_OP int_expr                {$$ = (~make_ulong($2)); }
  138.     | int_expr ASL int_expr                {$$ = (double)MyASL(make_ulong($1),make_ulong($3)); }
  139.     | int_expr ASR int_expr                {$$ = (double)MyASR(make_ulong($1),make_ulong($3)); }
  140.     | int_expr LSL int_expr                {$$ = (double)MyLSL(make_ulong($1),make_ulong($3)); }
  141.     | int_expr LSR int_expr                {$$ = (double)MyLSR(make_ulong($1),make_ulong($3)); }
  142.     | int_expr ROL int_expr                {$$ = (double)MyROL(make_ulong($1),make_ulong($3)); }
  143.     | int_expr ROR int_expr                {$$ = (double)MyROR(make_ulong($1),make_ulong($3)); }
  144.     | int_expr POW int_expr                {$$ = pow($1,$3); }
  145.     | SIN int_expr                    {$$ = sin(calc_angle($2)); }
  146.     | COS int_expr                    {$$ = cos(calc_angle($2)); }
  147.     | TAN int_expr                    {$$ = tan(calc_angle($2)); }
  148.     | ASIN int_expr                    {$$ = asin(calc_angle($2)); }
  149.     | ACOS int_expr                    {$$ = acos(calc_angle($2)); }
  150.     | ATAN int_expr                    {$$ = atan(calc_angle($2)); }
  151.     | COT int_expr                    {$$ = cot(calc_angle($2)); }
  152.     | SINH int_expr                    {$$ = sinh(calc_angle($2)); }
  153.     | COSH int_expr                    {$$ = cosh(calc_angle($2)); }
  154.     | TANH int_expr                    {$$ = tanh(calc_angle($2)); }
  155.     | EXP int_expr                    {$$ = exp($2); }
  156.     | LOG int_expr                    {$$ = log($2); }
  157.     | LOG10 int_expr                {$$ = log10($2); }
  158.     | MY_ABS int_expr                {$$ = fabs($2); }
  159.     | SQRT int_expr                    {if($2 < 0.0) {PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = sqrt($2);}
  160.     | int_expr FAK                    {if($1 > 170.0) { PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = calc_fak($1);}
  161.     | FAK int_expr                    {if($2 > 170.0) { PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = calc_fak($2);}
  162.     | INT_CONSTANT
  163.     | X_MEM
  164.     | Y_MEM
  165.     | Z_MEM
  166.     ;
  167.  
  168. %%
  169.  
  170.  
  171.  
  172.  
  173. /**********************************************************************/
  174. /*             Convert a double to a long (the hard way)              */
  175. /**********************************************************************/
  176. ULONG make_ulong(double OldVal)
  177. {
  178.     char    Buffer[26];
  179.     ULONG    Dummy;
  180.  
  181.     sprintf(Buffer, "%f", OldVal);
  182.     stcd_l(Buffer, (LONG *)&Dummy);
  183.     return(Dummy);
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190. /**********************************************************************/
  191. /*                           Calculate Fak                            */
  192. /**********************************************************************/
  193. double calc_fak(double Fak)
  194. {
  195.     double    RetVal = 1.0, i = 1.0;
  196.  
  197.     while(i <= Fak)
  198.     {
  199.         RetVal *= i;
  200.         i++;
  201.     }
  202.  
  203.     return(RetVal);
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /**********************************************************************/
  211. /*              Calculate correct angle value (rad, deg)              */
  212. /**********************************************************************/
  213. double calc_angle(double Value)
  214. {
  215.     if(IntAngle == ID_DEG)
  216.         Value = (Value / 180.0) * Pi;
  217.  
  218.     return(Value);
  219. }
  220.  
  221.  
  222.  
  223. /**********************************************************************/
  224. /*                          Error-Routine ;)                          */
  225. /**********************************************************************/
  226. int yyerror(char *s)
  227. {
  228.     return(0);
  229. }
  230.