home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / mar94 / os20 / util / mcalc.lha / MCalc / Source / Scanner.l < prev    next >
Text File  |  1993-12-31  |  8KB  |  382 lines

  1. %{
  2. /*
  3. Auto:        smake MCalc
  4. */
  5.  
  6. #include    "y.tab.h"
  7.  
  8. #undef    malloc
  9. #define    malloc(x)    AllocVecPool(ParsePool, x)
  10.  
  11. #undef    free
  12. #define    free(x)        FreeVecPool(ParsePool, x)
  13.  
  14. #undef    YYLMAX
  15. #define    YYLMAX 1000
  16.  
  17. #undef    ECHO
  18. #define    ECHO
  19.  
  20. #undef    YY_FATAL_ERROR
  21. #define    YY_FATAL_ERROR(x)\
  22.     do\
  23.     {\
  24.         PError = ERR_PARSE;\
  25.     }\
  26.     while(0)
  27.  
  28. #undef    YY_USER_INIT
  29. #define    YY_USER_INIT \
  30. {\
  31.     PCharRead    = 0; \
  32.     PColumn        = 0; \
  33.     PError        = 0; \
  34.     NonDouble    = FALSE; \
  35. }
  36.  
  37.  
  38. /**********************************************************************/
  39. /*          This is our YYINPUT for scanning the inputbuffer          */
  40. /**********************************************************************/
  41. #undef    YY_INPUT
  42. #define    YY_INPUT(buf, result, max_size)\
  43.         {\
  44.             char c = ParseInput[PCharRead++];\
  45.             result = (c == '\0') ? YY_NULL : (buf[0] = c, 1);\
  46.         }
  47.  
  48.  
  49. extern    APTR    ParsePool;
  50. extern    UWORD    PError;
  51. extern    UWORD    IntType;
  52. extern    UWORD    IntBase;
  53. extern    UWORD    IntSign;
  54. extern    UWORD    ContainsUnDec;
  55. extern    char    *ParseInput;
  56. extern    double    XMem, YMem, ZMem;
  57. UWORD    PCharRead;
  58. UWORD    PColumn = 0;
  59. UWORD    NonDouble;
  60. %}
  61.  
  62.  
  63. %%
  64. "Abs"                        { count(); return(MY_ABS); }
  65. "Cos"                        { count(); return(COS); }
  66. "Sin"                        { count(); return(SIN); }
  67. "Tan"                        { count(); return(TAN); }
  68. "ACos"                        { count(); return(ACOS); }
  69. "ASin"                        { count(); return(ASIN); }
  70. "ATan"                        { count(); return(ATAN); }
  71. "Sinh"                        { count(); return(SINH); }
  72. "Cosh"                        { count(); return(COSH); }
  73. "Tanh"                        { count(); return(TANH); }
  74. "Cot"                        { count(); return(COT); }
  75. "Exp"                        { count(); return(EXP); }
  76. "^"                        { count(); return(POW); }
  77. "Log"                        { count(); return(LOG); }
  78. "Log10"                        { count(); return(LOG10); }
  79. "Sqrt"                        { count(); return(SQRT); }
  80. "Asl"                        { count(); return(ASL); }
  81. "Asr"                        { count(); return(ASR); }
  82. "Lsl"                        { count(); return(LSL); }
  83. "Lsr"                        { count(); return(LSR); }
  84. "Rol"                        { count(); return(ROL); }
  85. "Ror"                        { count(); return(ROR); }
  86. "And"                        { count(); return(AND_OP); }
  87. "Or"                        { count(); return(OR_OP); }
  88. "XOr"                        { count(); return(XOR_OP); }
  89. "Not"                        { count(); return(NOT_OP); }
  90. "!"                        { count(); return(FAK); }
  91.  
  92.  
  93. [0-9]+                        { count(); calc_int_value(&yytext[0]); return(INT_CONSTANT); }
  94. "$"[0-9a-fA-F]+                    {    if(yyleng > 9)
  95.                                 return(-1);
  96.                             else
  97.                             {
  98.                                 count();
  99.                                 calc_hex_value(&yytext[1], yyleng - 1);
  100.                                 return(INT_CONSTANT);
  101.                             }
  102.                         }
  103. "0x"[0-9a-fA-F]+                {    if(yyleng > 10)
  104.                                 return(-1);
  105.                             else
  106.                             {
  107.                                 count();
  108.                                 calc_hex_value(&yytext[2], yyleng - 2);
  109.                                 return(INT_CONSTANT);
  110.                             }
  111.                         }
  112. "\\"[0-7]+                    {    if(yyleng > 13)
  113.                                 return(-1);
  114.                             else
  115.                             {
  116.                                 count();
  117.                                 calc_oct_value(&yytext[1]);
  118.                                 return(INT_CONSTANT);
  119.                             }
  120.                         }
  121. "%"[0-1]+                    {    if(yyleng > 33)
  122.                                 return(-1);
  123.                             else
  124.                             {
  125.                                 count();
  126.                                 calc_bin_value(&yytext[1]);
  127.                                 return(INT_CONSTANT);
  128.                             }
  129.                         }
  130. [0-9]*"."[0-9]+                    { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  131. [0-9]*"."[0-9]*[eE][+-]?[0-9]+            { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  132.  
  133. "Pi"                        { count(); yylval.Real = PI; return(INT_CONSTANT); }
  134. "E"                        { count(); yylval.Real = 2.718281828; return(INT_CONSTANT); }
  135. "X"                        { count(); yylval.Real = XMem; return(X_MEM); }
  136. "Y"                        { count(); yylval.Real = YMem; return(Y_MEM); }
  137. "Z"                        { count(); yylval.Real = ZMem; return(Z_MEM); }
  138.  
  139. "("                        { count(); return(OPEN_OP); }
  140. ")"                        { count(); return(CLOSE_OP); }
  141. "-"                        { count(); return(SUB_OP); }
  142. "+"                        { count(); return(ADD_OP); }
  143. "*"                        { count(); return(MUL_OP); }
  144. "/"                        { count(); return(DIV_OP); }
  145. "Mod"                        { count(); return(MOD_OP); }
  146. "="                        { count(); return(EQU_OP); }
  147.  
  148. [ \t]                        { count(); }
  149. \n                        { return(0); }
  150. .                        { count(); PError = ERR_UNKNOWN_CHR; return(-1); }
  151.  
  152. %%
  153.  
  154.  
  155.  
  156. /**********************************************************************/
  157. /*                            Count column                            */
  158. /**********************************************************************/
  159. void count(void)
  160. {
  161.     PError        = 0;
  162.     PColumn        += yyleng;
  163. }
  164.  
  165.  
  166.  
  167.  
  168. /**********************************************************************/
  169. /*                            Calc integer                            */
  170. /**********************************************************************/
  171. int calc_int_value(char *s)
  172. {
  173.     char    *p;
  174.  
  175.     yylval.Real = strtod(s, &p);
  176.     return(0);
  177. }
  178.  
  179.  
  180.  
  181. /**********************************************************************/
  182. /*                           Calc Hex-Value                           */
  183. /**********************************************************************/
  184. int calc_hex_value(char *s, int leng)
  185. {
  186.     int    i, NumParse;
  187.     double    MaxVal;
  188.  
  189.         // Set flag for non-decimal input
  190.  
  191.     ContainsUnDec    = TRUE;
  192.  
  193.     switch(IntBase)
  194.     {
  195.         case ID_8BIT :
  196.         {
  197.             NumParse    = 2;
  198.             MaxVal        = 255.0;
  199.             break;
  200.         }
  201.         case ID_16BIT :
  202.         {
  203.             NumParse    = 4;
  204.             MaxVal        = 65535.0;
  205.             break;
  206.         }
  207.         case ID_32BIT :
  208.         {
  209.             NumParse    = 8;
  210.             MaxVal        = 4294967295.0;
  211.             break;
  212.         }
  213.     }
  214.  
  215.     yylval.Real    = 0;
  216.  
  217.     for(i = 0; ((i < leng) && (i < NumParse)); i++)
  218.     {
  219.         yylval.Real        *= 16.0;
  220.  
  221.         if((*s >= 'a') && (*s <= 'f'))
  222.             yylval.Real    += (double)(*s - 'a' + 10);
  223.         else if((*s >= 'A') && (*s <= 'F'))
  224.             yylval.Real    += (double)(*s - 'A' + 10);
  225.         else
  226.             yylval.Real    += (double)(*s - '0');
  227.  
  228.         s++;
  229.     }
  230.  
  231.     if(IntType != ID_DECIMAL)
  232.     {
  233.         if(yylval.Real > MaxVal)
  234.             yylval.Real = MaxVal;
  235.     }
  236.     else if(IntSign == ID_SIGNED)
  237.     {
  238.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  239.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  240.     }
  241.  
  242.     return(0);
  243. }
  244.  
  245.  
  246. /**********************************************************************/
  247. /*                         Calc Binary value                          */
  248. /**********************************************************************/
  249. int calc_bin_value(char *s)
  250. {
  251.     int    i, NumParse;
  252.     double    MaxVal;
  253.  
  254.         // Set flag for non-decimal input
  255.  
  256.     ContainsUnDec    = TRUE;
  257.  
  258.         // Check for how many bits to check for ;)
  259.  
  260.     switch(IntBase)
  261.     {
  262.         case ID_8BIT :
  263.         {
  264.             NumParse    = 8;
  265.             MaxVal        = 255.0;
  266.             break;
  267.         }
  268.         case ID_16BIT :
  269.         {
  270.             NumParse    = 16;
  271.             MaxVal        = 65535.0;
  272.             break;
  273.         }
  274.         case ID_32BIT :
  275.         {
  276.             NumParse    = 32;
  277.             MaxVal        = 4294967295.0;
  278.             break;
  279.         }
  280.     }
  281.  
  282.         // get to end of input
  283.  
  284.     yylval.Real    = 0;
  285.  
  286.         // Convert to double
  287.  
  288.     for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
  289.     {
  290.         yylval.Real    *= 2.0;
  291.         if(*s++ == '1')
  292.             yylval.Real    += 1.0;
  293.     }
  294.  
  295.     if(IntType != ID_DECIMAL)
  296.     {
  297.         if(yylval.Real > MaxVal)
  298.             yylval.Real = MaxVal;
  299.     }
  300.     else if(IntSign == ID_SIGNED)
  301.     {
  302.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  303.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  304.     }
  305.  
  306.     return(0);
  307. }
  308.  
  309.  
  310.  
  311.  
  312. /**********************************************************************/
  313. /*                          Calc octal value                          */
  314. /**********************************************************************/
  315. int calc_oct_value(char *s)
  316. {
  317.     int    i, NumParse;
  318.     double    MaxVal;
  319.  
  320.         // Set flag for non-decimal input
  321.  
  322.     ContainsUnDec    = TRUE;
  323.  
  324.     switch(IntBase)
  325.     {
  326.         case ID_8BIT :
  327.         {
  328.             NumParse    = 3;
  329.             MaxVal        = 255.0;
  330.             break;
  331.         }
  332.         case ID_16BIT :
  333.         {
  334.             NumParse    = 6;
  335.             MaxVal        = 65535.0;
  336.             break;
  337.         }
  338.         case ID_32BIT :
  339.         {
  340.             NumParse    = 11;
  341.             MaxVal        = 4294967295.0;
  342.             break;
  343.         }
  344.     }
  345.  
  346.     yylval.Real    = 0;
  347.     for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
  348.     {
  349.         yylval.Real    *= 8.0;
  350.         yylval.Real    += (double)(*s - '0');
  351.  
  352.         s++;
  353.     }
  354.  
  355.     if(IntType != ID_DECIMAL)
  356.     {
  357.         if(yylval.Real > MaxVal)
  358.             yylval.Real = MaxVal;
  359.     }
  360.     else if(IntSign == ID_SIGNED)
  361.     {
  362.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  363.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  364.     }
  365.  
  366.     return(0);
  367. }
  368.  
  369.  
  370.  
  371.  
  372. /**********************************************************************/
  373. /*                            Calc double                             */
  374. /**********************************************************************/
  375. int calc_dbl_value(char *s)
  376. {
  377.     char    *p;
  378.  
  379.     yylval.Real = strtod(s, &p);
  380.     return(0);
  381. }
  382.