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