home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / misc / math / MCalc17.lha / MCalc / Source / Scanner.l < prev    next >
Text File  |  1995-11-29  |  11KB  |  468 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    IMem,
  57.         JMem,
  58.         KMem,
  59.         LMem,
  60.         MMem,
  61.         NMem,
  62.         OMem,
  63.         PMem,
  64.         QMem,
  65.         RMem,
  66.         SMem,
  67.         TMem,
  68.         UMem,
  69.         VMem,
  70.         WMem,
  71.         XMem,
  72.         YMem,
  73.         ZMem;
  74.  
  75. extern    struct    List    StandardList,
  76.             LinearList;
  77.  
  78. UWORD    PCharRead;
  79. UWORD    PColumn = 0;
  80. UWORD    NonDouble;
  81. %}
  82.  
  83.  
  84. %%
  85. "Abs"                        { count(); return(MY_ABS); }
  86. "Cos"                        { count(); return(COS); }
  87. "Sin"                        { count(); return(SIN); }
  88. "Tan"                        { count(); return(TAN); }
  89. "ACos"                        { count(); return(ACOS); }
  90. "ASin"                        { count(); return(ASIN); }
  91. "ATan"                        { count(); return(ATAN); }
  92. "Sinh"                        { count(); return(SINH); }
  93. "Cosh"                        { count(); return(COSH); }
  94. "Tanh"                        { count(); return(TANH); }
  95. "Cot"                        { count(); return(COT); }
  96. "Exp"                        { count(); return(EXP); }
  97. "^"                        { count(); return(POW); }
  98. "Log"                        { count(); return(LOG); }
  99. "Log10"                        { count(); return(LOG10); }
  100. "Sqrt"                        { count(); return(SQRT); }
  101. "Asl"                        { count(); return(ASL); }
  102. "Asr"                        { count(); return(ASR); }
  103. "Lsl"                        { count(); return(LSL); }
  104. "Lsr"                        { count(); return(LSR); }
  105. "Rol"                        { count(); return(ROL); }
  106. "Ror"                        { count(); return(ROR); }
  107. "And"                        { count(); return(AND_OP); }
  108. "Or"                        { count(); return(OR_OP); }
  109. "XOr"                        { count(); return(XOR_OP); }
  110. "Not"                        { count(); return(NOT_OP); }
  111. "!"                        { count(); return(FAK); }
  112. "%"                        { count(); return(PERCENT); }
  113. "%ch"                        { count(); return(CHPERCENT); }
  114. "%t"                        { count(); return(TPERCENT); }
  115. "(n k)"                        { count(); return(N_OVER_K); }
  116. "YRoot"                        { count(); return(YROOT); }
  117. "REZ"                        { count(); return(REZ); }
  118. "SD"                        { count(); FreeList(&StandardList); return(SD_INIT); }
  119. "SD_AVE"                    { count(); return(SD_AVERAGE); }
  120. "SD_PDEV"                    { count(); return(SD_DEVIATION1); }
  121. "SD_SDEV"                    { count(); return(SD_DEVIATION2); }
  122. "SD_NUM"                    { count(); return(SD_NUM); }
  123. "SD_QSUM"                    { count(); return(SD_QSUM); }
  124. "SD_SUM"                    { count(); return(SD_SUM); }
  125. "LR"                        { count(); FreeList(&LinearList); return(LR_INIT); }
  126. "LR_XAVE"                    { count(); return(LR_XAVERAGE); }
  127. "LR_XPDEV"                    { count(); return(LR_XDEVIATION1); }
  128. "LR_XSDEV"                    { count(); return(LR_XDEVIATION2); }
  129. "LR_XNUM"                    { count(); return(LR_XNUM); }
  130. "LR_XQSUM"                    { count(); return(LR_XQSUM); }
  131. "LR_XSUM"                    { count(); return(LR_XSUM); }
  132. "LR_YAVE"                    { count(); return(LR_YAVERAGE); }
  133. "LR_YPDEV"                    { count(); return(LR_YDEVIATION1); }
  134. "LR_YSDEV"                    { count(); return(LR_YDEVIATION2); }
  135. "LR_YNUM"                    { count(); return(LR_YNUM); }
  136. "LR_YQSUM"                    { count(); return(LR_YQSUM); }
  137. "LR_YSUM"                    { count(); return(LR_YSUM); }
  138. "LR_XYSUM"                    { count(); return(LR_XYSUM); }
  139. "LR_ALPHA"                    { count(); return(LR_ALPHA); }
  140. "LR_BETA"                    { count(); return(LR_BETA); }
  141. "LR_ASSX"                    { count(); return(LR_ASSESSX); }
  142. "LR_ASSY"                    { count(); return(LR_ASSESSY); }
  143. "LR_CORR"                    { count(); return(LR_CORR); }
  144. "LR_CCORR"                    { count(); return(LR_CRITCORR); }
  145. "LR_COVAR"                    { count(); return(LR_COVAR); }
  146. "EXG"                        { count(); return(EXG_OP); }
  147.  
  148.  
  149.  
  150.  
  151. [0-9]+                        { count(); calc_int_value(&yytext[0]); return(INT_CONSTANT); }
  152. "$"[0-9a-fA-F]+                    {    if(yyleng > 9)
  153.                                 return(-1);
  154.                             else
  155.                             {
  156.                                 count();
  157.                                 calc_hex_value(&yytext[1], yyleng - 1);
  158.                                 return(INT_CONSTANT);
  159.                             }
  160.                         }
  161. "0x"[0-9a-fA-F]+                {    if(yyleng > 10)
  162.                                 return(-1);
  163.                             else
  164.                             {
  165.                                 count();
  166.                                 calc_hex_value(&yytext[2], yyleng - 2);
  167.                                 return(INT_CONSTANT);
  168.                             }
  169.                         }
  170. "\\"[0-7]+                    {    if(yyleng > 13)
  171.                                 return(-1);
  172.                             else
  173.                             {
  174.                                 count();
  175.                                 calc_oct_value(&yytext[1]);
  176.                                 return(INT_CONSTANT);
  177.                             }
  178.                         }
  179. "&"[0-1]+                    {    if(yyleng > 33)
  180.                                 return(-1);
  181.                             else
  182.                             {
  183.                                 count();
  184.                                 calc_bin_value(&yytext[1]);
  185.                                 return(INT_CONSTANT);
  186.                             }
  187.                         }
  188. [0-9]*"."[0-9]+                    { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  189. [0-9]*"."[0-9]*[eE][+-]?[0-9]+            { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  190. [0-9]*[eE][+-]?[0-9]+                { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
  191.  
  192. "Pi"                        { count(); yylval.Real = PI; return(INT_CONSTANT); }
  193. "E"                        { count(); yylval.Real = 2.718281828; return(INT_CONSTANT); }
  194.  
  195. "I"                        { count(); return(I_MEM); }
  196. "J"                        { count(); return(J_MEM); }
  197. "K"                        { count(); return(K_MEM); }
  198. "L"                        { count(); return(L_MEM); }
  199. "M"                        { count(); return(M_MEM); }
  200. "N"                        { count(); return(N_MEM); }
  201. "O"                        { count(); return(O_MEM); }
  202. "P"                        { count(); return(P_MEM); }
  203. "Q"                        { count(); return(Q_MEM); }
  204. "R"                        { count(); return(R_MEM); }
  205. "S"                        { count(); return(S_MEM); }
  206. "T"                        { count(); return(T_MEM); }
  207. "U"                        { count(); return(U_MEM); }
  208. "V"                        { count(); return(V_MEM); }
  209. "W"                        { count(); return(W_MEM); }
  210. "X"                        { count(); return(X_MEM); }
  211. "Y"                        { count(); return(Y_MEM); }
  212. "Z"                        { count(); return(Z_MEM); }
  213.  
  214. "("                        { count(); return(OPEN_OP); }
  215. ")"                        { count(); return(CLOSE_OP); }
  216. "-"                        { count(); return(SUB_OP); }
  217. "+"                        { count(); return(ADD_OP); }
  218. "*"                        { count(); return(MUL_OP); }
  219. "/"                        { count(); return(DIV_OP); }
  220. "Mod"                        { count(); return(MOD_OP); }
  221. "="                        { count(); return(EQU_OP); }
  222.  
  223. ","                        { count(); return(COMMA); }
  224. ";"                        { count(); return(SEMICOLON); }
  225.  
  226. [ \t]                        { count(); }
  227. \n                        { return(0); }
  228. .                        { count(); PError = ERR_UNKNOWN_CHR; return(-1); }
  229.  
  230. %%
  231.  
  232.  
  233. /**********************************************************************/
  234. /*                     My special yywrap function                     */
  235. /**********************************************************************/
  236. int yywrap(void)
  237. {
  238.     return(1);
  239. }
  240.  
  241.  
  242. /**********************************************************************/
  243. /*                            Count column                            */
  244. /**********************************************************************/
  245. void count(void)
  246. {
  247.     PError        = 0;
  248.     PColumn        += yyleng;
  249. }
  250.  
  251.  
  252.  
  253.  
  254. /**********************************************************************/
  255. /*                            Calc integer                            */
  256. /**********************************************************************/
  257. int calc_int_value(char *s)
  258. {
  259.     char    *p;
  260.  
  261.     yylval.Real = strtod(s, &p);
  262.     return(0);
  263. }
  264.  
  265.  
  266.  
  267. /**********************************************************************/
  268. /*                           Calc Hex-Value                           */
  269. /**********************************************************************/
  270. int calc_hex_value(char *s, int leng)
  271. {
  272.     int    i, NumParse;
  273.     double    MaxVal;
  274.  
  275.         // Set flag for non-decimal input
  276.  
  277.     ContainsUnDec    = TRUE;
  278.  
  279.     switch(IntBase)
  280.     {
  281.         case ID_8BIT :
  282.         {
  283.             NumParse    = 2;
  284.             MaxVal        = 255.0;
  285.             break;
  286.         }
  287.         case ID_16BIT :
  288.         {
  289.             NumParse    = 4;
  290.             MaxVal        = 65535.0;
  291.             break;
  292.         }
  293.         case ID_32BIT :
  294.         {
  295.             NumParse    = 8;
  296.             MaxVal        = 4294967295.0;
  297.             break;
  298.         }
  299.     }
  300.  
  301.     yylval.Real    = 0;
  302.  
  303.     for(i = 0; ((i < leng) && (i < NumParse)); i++)
  304.     {
  305.         yylval.Real        *= 16.0;
  306.  
  307.         if((*s >= 'a') && (*s <= 'f'))
  308.             yylval.Real    += (double)(*s - 'a' + 10);
  309.         else if((*s >= 'A') && (*s <= 'F'))
  310.             yylval.Real    += (double)(*s - 'A' + 10);
  311.         else
  312.             yylval.Real    += (double)(*s - '0');
  313.  
  314.         s++;
  315.     }
  316.  
  317.     if(IntType != ID_DECIMAL)
  318.     {
  319.         if(yylval.Real > MaxVal)
  320.             yylval.Real = MaxVal;
  321.     }
  322.     else if(IntSign == ID_SIGNED)
  323.     {
  324.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  325.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  326.     }
  327.  
  328.     return(0);
  329. }
  330.  
  331.  
  332. /**********************************************************************/
  333. /*                         Calc Binary value                          */
  334. /**********************************************************************/
  335. int calc_bin_value(char *s)
  336. {
  337.     int    i, NumParse;
  338.     double    MaxVal;
  339.  
  340.         // Set flag for non-decimal input
  341.  
  342.     ContainsUnDec    = TRUE;
  343.  
  344.         // Check for how many bits to check for ;)
  345.  
  346.     switch(IntBase)
  347.     {
  348.         case ID_8BIT :
  349.         {
  350.             NumParse    = 8;
  351.             MaxVal        = 255.0;
  352.             break;
  353.         }
  354.         case ID_16BIT :
  355.         {
  356.             NumParse    = 16;
  357.             MaxVal        = 65535.0;
  358.             break;
  359.         }
  360.         case ID_32BIT :
  361.         {
  362.             NumParse    = 32;
  363.             MaxVal        = 4294967295.0;
  364.             break;
  365.         }
  366.     }
  367.  
  368.         // get to end of input
  369.  
  370.     yylval.Real    = 0;
  371.  
  372.         // Convert to double
  373.  
  374.     for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
  375.     {
  376.         yylval.Real    *= 2.0;
  377.         if(*s++ == '1')
  378.             yylval.Real    += 1.0;
  379.     }
  380.  
  381.     if(IntType != ID_DECIMAL)
  382.     {
  383.         if(yylval.Real > MaxVal)
  384.             yylval.Real = MaxVal;
  385.     }
  386.     else if(IntSign == ID_SIGNED)
  387.     {
  388.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  389.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  390.     }
  391.  
  392.     return(0);
  393. }
  394.  
  395.  
  396.  
  397.  
  398. /**********************************************************************/
  399. /*                          Calc octal value                          */
  400. /**********************************************************************/
  401. int calc_oct_value(char *s)
  402. {
  403.     int    i, NumParse;
  404.     double    MaxVal;
  405.  
  406.         // Set flag for non-decimal input
  407.  
  408.     ContainsUnDec    = TRUE;
  409.  
  410.     switch(IntBase)
  411.     {
  412.         case ID_8BIT :
  413.         {
  414.             NumParse    = 3;
  415.             MaxVal        = 255.0;
  416.             break;
  417.         }
  418.         case ID_16BIT :
  419.         {
  420.             NumParse    = 6;
  421.             MaxVal        = 65535.0;
  422.             break;
  423.         }
  424.         case ID_32BIT :
  425.         {
  426.             NumParse    = 11;
  427.             MaxVal        = 4294967295.0;
  428.             break;
  429.         }
  430.     }
  431.  
  432.     yylval.Real    = 0;
  433.     for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
  434.     {
  435.         yylval.Real    *= 8.0;
  436.         yylval.Real    += (double)(*s - '0');
  437.  
  438.         s++;
  439.     }
  440.  
  441.     if(IntType != ID_DECIMAL)
  442.     {
  443.         if(yylval.Real > MaxVal)
  444.             yylval.Real = MaxVal;
  445.     }
  446.     else if(IntSign == ID_SIGNED)
  447.     {
  448.         if(yylval.Real >= ((MaxVal + 1.0) / 2))
  449.             yylval.Real = -(MaxVal - yylval.Real + 1.0);
  450.     }
  451.  
  452.     return(0);
  453. }
  454.  
  455.  
  456.  
  457.  
  458. /**********************************************************************/
  459. /*                            Calc double                             */
  460. /**********************************************************************/
  461. int calc_dbl_value(char *s)
  462. {
  463.     char    *p;
  464.  
  465.     yylval.Real = strtod(s, &p);
  466.     return(0);
  467. }
  468.