home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / misc / mcalc / source / scanner.l < prev    next >
Encoding:
Lex Description  |  1994-08-18  |  8.2 KB  |  394 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. "%"                        { count(); return(PERCENT); }
  92. "%ch"                        { count(); return(CHPERCENT); }
  93. "%t"                        { count(); return(TPERCENT); }
  94.  
  95.  
  96. [0-9]+                        { count(); calc_int_value(&yytext[0]); return(INT_CONSTANT); }
  97. "$"[0-9a-fA-F]+                    {    if(yyleng > 9)
  98.                                 return(-1);
  99.                             else
  100.                             {
  101.                                 count();
  102.                                 calc_hex_value(&yytext[1], yyleng - 1);
  103.                                 return(INT_CONSTANT);
  104.                             }
  105.                         }
  106. "0x"[0-9a-fA-F]+                {    if(yyleng > 10)
  107.                                 return(-1);
  108.                             else
  109.                             {
  110.                                 count();
  111.                                 calc_hex_value(&yytext[2], yyleng - 2);
  112.                                 return(INT_CONSTANT);
  113.                             }
  114.                         }
  115. "\\"[0-7]+                    {    if(yyleng > 13)
  116.                                 return(-1);
  117.                             else
  118.                             {
  119.                                 count();
  120.                                 calc_oct_value(&yytext[1]);
  121.                                 return(INT_CONSTANT);
  122.                             }
  123.                         }
  124. "&"[0-1]+                    {    if(yyleng > 33)
  125.                                 return(-1);
  126.                             else
  127.                             {
  128.                                 count();
  129.                                 calc_bin_value(&yytext[1]);
  130.                                 return(INT_CONSTANT);
  131.                             }
  132.                         }
  133. [0-9]*"."[0-9]+                    { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  134. [0-9]*"."[0-9]*[eE][+-]?[0-9]+            { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  135. [0-9]*[eE][+-]?[0-9]+                { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  136.  
  137. "Pi"                        { count(); yylval.Real = PI; return(INT_CONSTANT); }
  138. "E"                        { count(); yylval.Real = 2.718281828; return(INT_CONSTANT); }
  139. "X"                        { count(); yylval.Real = XMem; return(X_MEM); }
  140. "Y"                        { count(); yylval.Real = YMem; return(Y_MEM); }
  141. "Z"                        { count(); yylval.Real = ZMem; return(Z_MEM); }
  142.  
  143. "("                        { count(); return(OPEN_OP); }
  144. ")"                        { count(); return(CLOSE_OP); }
  145. "-"                        { count(); return(SUB_OP); }
  146. "+"                        { count(); return(ADD_OP); }
  147. "*"                        { count(); return(MUL_OP); }
  148. "/"                        { count(); return(DIV_OP); }
  149. "Mod"                        { count(); return(MOD_OP); }
  150. "="                        { count(); return(EQU_OP); }
  151.  
  152. [ \t]                        { count(); }
  153. \n                        { return(0); }
  154. .                        { count(); PError = ERR_UNKNOWN_CHR; return(-1); }
  155.  
  156. %%
  157.  
  158.  
  159. /**********************************************************************/
  160. /*                     My special yywrap function                     */
  161. /**********************************************************************/
  162. int yywrap(void)
  163. {
  164.     return(1);
  165. }
  166.  
  167.  
  168. /**********************************************************************/
  169. /*                            Count column                            */
  170. /**********************************************************************/
  171. void count(void)
  172. {
  173.     PError        = 0;
  174.     PColumn        += yyleng;
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /**********************************************************************/
  181. /*                            Calc integer                            */
  182. /**********************************************************************/
  183. int calc_int_value(char *s)
  184. {
  185.     char    *p;
  186.  
  187.     yylval.Real = strtod(s, &p);
  188.     return(0);
  189. }
  190.  
  191.  
  192.  
  193. /**********************************************************************/
  194. /*                           Calc Hex-Value                           */
  195. /**********************************************************************/
  196. int calc_hex_value(char *s, int leng)
  197. {
  198.     int    i, NumParse;
  199.     double    MaxVal;
  200.  
  201.         // Set flag for non-decimal input
  202.  
  203.     ContainsUnDec    = TRUE;
  204.  
  205.     switch(IntBase)
  206.     {
  207.         case ID_8BIT :
  208.         {
  209.             NumParse    = 2;
  210.             MaxVal        = 255.0;
  211.             break;
  212.         }
  213.         case ID_16BIT :
  214.         {
  215.             NumParse    = 4;
  216.             MaxVal        = 65535.0;
  217.             break;
  218.         }
  219.         case ID_32BIT :
  220.         {
  221.             NumParse    = 8;
  222.             MaxVal        = 4294967295.0;
  223.             break;
  224.         }
  225.     }
  226.  
  227.     yylval.Real    = 0;
  228.  
  229.     for(i = 0; ((i < leng) && (i < NumParse)); i++)
  230.     {
  231.         yylval.Real        *= 16.0;
  232.  
  233.         if((*s >= 'a') && (*s <= 'f'))
  234.             yylval.Real    += (double)(*s - 'a' + 10);
  235.         else if((*s >= 'A') && (*s <= 'F'))
  236.             yylval.Real    += (double)(*s - 'A' + 10);
  237.         else
  238.             yylval.Real    += (double)(*s - '0');
  239.  
  240.         s++;
  241.     }
  242.  
  243.     if(IntType != ID_DECIMAL)
  244.     {
  245.         if(yylval.Real > MaxVal)
  246.             yylval.Real = MaxVal;
  247.     }
  248.     else if(IntSign == ID_SIGNED)
  249.     {
  250.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  251.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  252.     }
  253.  
  254.     return(0);
  255. }
  256.  
  257.  
  258. /**********************************************************************/
  259. /*                         Calc Binary value                          */
  260. /**********************************************************************/
  261. int calc_bin_value(char *s)
  262. {
  263.     int    i, NumParse;
  264.     double    MaxVal;
  265.  
  266.         // Set flag for non-decimal input
  267.  
  268.     ContainsUnDec    = TRUE;
  269.  
  270.         // Check for how many bits to check for ;)
  271.  
  272.     switch(IntBase)
  273.     {
  274.         case ID_8BIT :
  275.         {
  276.             NumParse    = 8;
  277.             MaxVal        = 255.0;
  278.             break;
  279.         }
  280.         case ID_16BIT :
  281.         {
  282.             NumParse    = 16;
  283.             MaxVal        = 65535.0;
  284.             break;
  285.         }
  286.         case ID_32BIT :
  287.         {
  288.             NumParse    = 32;
  289.             MaxVal        = 4294967295.0;
  290.             break;
  291.         }
  292.     }
  293.  
  294.         // get to end of input
  295.  
  296.     yylval.Real    = 0;
  297.  
  298.         // Convert to double
  299.  
  300.     for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
  301.     {
  302.         yylval.Real    *= 2.0;
  303.         if(*s++ == '1')
  304.             yylval.Real    += 1.0;
  305.     }
  306.  
  307.     if(IntType != ID_DECIMAL)
  308.     {
  309.         if(yylval.Real > MaxVal)
  310.             yylval.Real = MaxVal;
  311.     }
  312.     else if(IntSign == ID_SIGNED)
  313.     {
  314.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  315.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  316.     }
  317.  
  318.     return(0);
  319. }
  320.  
  321.  
  322.  
  323.  
  324. /**********************************************************************/
  325. /*                          Calc octal value                          */
  326. /**********************************************************************/
  327. int calc_oct_value(char *s)
  328. {
  329.     int    i, NumParse;
  330.     double    MaxVal;
  331.  
  332.         // Set flag for non-decimal input
  333.  
  334.     ContainsUnDec    = TRUE;
  335.  
  336.     switch(IntBase)
  337.     {
  338.         case ID_8BIT :
  339.         {
  340.             NumParse    = 3;
  341.             MaxVal        = 255.0;
  342.             break;
  343.         }
  344.         case ID_16BIT :
  345.         {
  346.             NumParse    = 6;
  347.             MaxVal        = 65535.0;
  348.             break;
  349.         }
  350.         case ID_32BIT :
  351.         {
  352.             NumParse    = 11;
  353.             MaxVal        = 4294967295.0;
  354.             break;
  355.         }
  356.     }
  357.  
  358.     yylval.Real    = 0;
  359.     for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
  360.     {
  361.         yylval.Real    *= 8.0;
  362.         yylval.Real    += (double)(*s - '0');
  363.  
  364.         s++;
  365.     }
  366.  
  367.     if(IntType != ID_DECIMAL)
  368.     {
  369.         if(yylval.Real > MaxVal)
  370.             yylval.Real = MaxVal;
  371.     }
  372.     else if(IntSign == ID_SIGNED)
  373.     {
  374.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  375.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  376.     }
  377.  
  378.     return(0);
  379. }
  380.  
  381.  
  382.  
  383.  
  384. /**********************************************************************/
  385. /*                            Calc double                             */
  386. /**********************************************************************/
  387. int calc_dbl_value(char *s)
  388. {
  389.     char    *p;
  390.  
  391.     yylval.Real = strtod(s, &p);
  392.     return(0);
  393. }
  394.