home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 10
/
aminetcdnumber101996.iso
/
Aminet
/
misc
/
math
/
MCalc17.lha
/
MCalc
/
Source
/
Scanner.l
< prev
next >
Wrap
Text File
|
1995-11-29
|
11KB
|
468 lines
%{
/*
Auto: smake MCalc
*/
#include "y.tab.h"
#undef malloc
#define malloc(x) AllocVecPool(ParsePool, x)
#undef free
#define free(x) FreeVecPool(ParsePool, x)
#undef YYLMAX
#define YYLMAX 1000
#undef ECHO
#define ECHO
#undef YY_FATAL_ERROR
#define YY_FATAL_ERROR(x)\
do\
{\
PError = ERR_PARSE;\
}\
while(0)
#undef YY_USER_INIT
#define YY_USER_INIT \
{\
PCharRead = 0; \
PColumn = 0; \
PError = 0; \
NonDouble = FALSE; \
}
/**********************************************************************/
/* This is our YYINPUT for scanning the inputbuffer */
/**********************************************************************/
#undef YY_INPUT
#define YY_INPUT(buf, result, max_size)\
{\
char c = ParseInput[PCharRead++];\
result = (c == '\0') ? YY_NULL : (buf[0] = c, 1);\
}
extern APTR ParsePool;
extern UWORD PError;
extern UWORD IntType;
extern UWORD IntBase;
extern UWORD IntSign;
extern UWORD ContainsUnDec;
extern char *ParseInput;
extern double IMem,
JMem,
KMem,
LMem,
MMem,
NMem,
OMem,
PMem,
QMem,
RMem,
SMem,
TMem,
UMem,
VMem,
WMem,
XMem,
YMem,
ZMem;
extern struct List StandardList,
LinearList;
UWORD PCharRead;
UWORD PColumn = 0;
UWORD NonDouble;
%}
%%
"Abs" { count(); return(MY_ABS); }
"Cos" { count(); return(COS); }
"Sin" { count(); return(SIN); }
"Tan" { count(); return(TAN); }
"ACos" { count(); return(ACOS); }
"ASin" { count(); return(ASIN); }
"ATan" { count(); return(ATAN); }
"Sinh" { count(); return(SINH); }
"Cosh" { count(); return(COSH); }
"Tanh" { count(); return(TANH); }
"Cot" { count(); return(COT); }
"Exp" { count(); return(EXP); }
"^" { count(); return(POW); }
"Log" { count(); return(LOG); }
"Log10" { count(); return(LOG10); }
"Sqrt" { count(); return(SQRT); }
"Asl" { count(); return(ASL); }
"Asr" { count(); return(ASR); }
"Lsl" { count(); return(LSL); }
"Lsr" { count(); return(LSR); }
"Rol" { count(); return(ROL); }
"Ror" { count(); return(ROR); }
"And" { count(); return(AND_OP); }
"Or" { count(); return(OR_OP); }
"XOr" { count(); return(XOR_OP); }
"Not" { count(); return(NOT_OP); }
"!" { count(); return(FAK); }
"%" { count(); return(PERCENT); }
"%ch" { count(); return(CHPERCENT); }
"%t" { count(); return(TPERCENT); }
"(n k)" { count(); return(N_OVER_K); }
"YRoot" { count(); return(YROOT); }
"REZ" { count(); return(REZ); }
"SD" { count(); FreeList(&StandardList); return(SD_INIT); }
"SD_AVE" { count(); return(SD_AVERAGE); }
"SD_PDEV" { count(); return(SD_DEVIATION1); }
"SD_SDEV" { count(); return(SD_DEVIATION2); }
"SD_NUM" { count(); return(SD_NUM); }
"SD_QSUM" { count(); return(SD_QSUM); }
"SD_SUM" { count(); return(SD_SUM); }
"LR" { count(); FreeList(&LinearList); return(LR_INIT); }
"LR_XAVE" { count(); return(LR_XAVERAGE); }
"LR_XPDEV" { count(); return(LR_XDEVIATION1); }
"LR_XSDEV" { count(); return(LR_XDEVIATION2); }
"LR_XNUM" { count(); return(LR_XNUM); }
"LR_XQSUM" { count(); return(LR_XQSUM); }
"LR_XSUM" { count(); return(LR_XSUM); }
"LR_YAVE" { count(); return(LR_YAVERAGE); }
"LR_YPDEV" { count(); return(LR_YDEVIATION1); }
"LR_YSDEV" { count(); return(LR_YDEVIATION2); }
"LR_YNUM" { count(); return(LR_YNUM); }
"LR_YQSUM" { count(); return(LR_YQSUM); }
"LR_YSUM" { count(); return(LR_YSUM); }
"LR_XYSUM" { count(); return(LR_XYSUM); }
"LR_ALPHA" { count(); return(LR_ALPHA); }
"LR_BETA" { count(); return(LR_BETA); }
"LR_ASSX" { count(); return(LR_ASSESSX); }
"LR_ASSY" { count(); return(LR_ASSESSY); }
"LR_CORR" { count(); return(LR_CORR); }
"LR_CCORR" { count(); return(LR_CRITCORR); }
"LR_COVAR" { count(); return(LR_COVAR); }
"EXG" { count(); return(EXG_OP); }
[0-9]+ { count(); calc_int_value(&yytext[0]); return(INT_CONSTANT); }
"$"[0-9a-fA-F]+ { if(yyleng > 9)
return(-1);
else
{
count();
calc_hex_value(&yytext[1], yyleng - 1);
return(INT_CONSTANT);
}
}
"0x"[0-9a-fA-F]+ { if(yyleng > 10)
return(-1);
else
{
count();
calc_hex_value(&yytext[2], yyleng - 2);
return(INT_CONSTANT);
}
}
"\\"[0-7]+ { if(yyleng > 13)
return(-1);
else
{
count();
calc_oct_value(&yytext[1]);
return(INT_CONSTANT);
}
}
"&"[0-1]+ { if(yyleng > 33)
return(-1);
else
{
count();
calc_bin_value(&yytext[1]);
return(INT_CONSTANT);
}
}
[0-9]*"."[0-9]+ { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
[0-9]*"."[0-9]*[eE][+-]?[0-9]+ { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
[0-9]*[eE][+-]?[0-9]+ { count(); calc_dbl_value(&yytext[0]); return(INT_CONSTANT); }
"Pi" { count(); yylval.Real = PI; return(INT_CONSTANT); }
"E" { count(); yylval.Real = 2.718281828; return(INT_CONSTANT); }
"I" { count(); return(I_MEM); }
"J" { count(); return(J_MEM); }
"K" { count(); return(K_MEM); }
"L" { count(); return(L_MEM); }
"M" { count(); return(M_MEM); }
"N" { count(); return(N_MEM); }
"O" { count(); return(O_MEM); }
"P" { count(); return(P_MEM); }
"Q" { count(); return(Q_MEM); }
"R" { count(); return(R_MEM); }
"S" { count(); return(S_MEM); }
"T" { count(); return(T_MEM); }
"U" { count(); return(U_MEM); }
"V" { count(); return(V_MEM); }
"W" { count(); return(W_MEM); }
"X" { count(); return(X_MEM); }
"Y" { count(); return(Y_MEM); }
"Z" { count(); return(Z_MEM); }
"(" { count(); return(OPEN_OP); }
")" { count(); return(CLOSE_OP); }
"-" { count(); return(SUB_OP); }
"+" { count(); return(ADD_OP); }
"*" { count(); return(MUL_OP); }
"/" { count(); return(DIV_OP); }
"Mod" { count(); return(MOD_OP); }
"=" { count(); return(EQU_OP); }
"," { count(); return(COMMA); }
";" { count(); return(SEMICOLON); }
[ \t] { count(); }
\n { return(0); }
. { count(); PError = ERR_UNKNOWN_CHR; return(-1); }
%%
/**********************************************************************/
/* My special yywrap function */
/**********************************************************************/
int yywrap(void)
{
return(1);
}
/**********************************************************************/
/* Count column */
/**********************************************************************/
void count(void)
{
PError = 0;
PColumn += yyleng;
}
/**********************************************************************/
/* Calc integer */
/**********************************************************************/
int calc_int_value(char *s)
{
char *p;
yylval.Real = strtod(s, &p);
return(0);
}
/**********************************************************************/
/* Calc Hex-Value */
/**********************************************************************/
int calc_hex_value(char *s, int leng)
{
int i, NumParse;
double MaxVal;
// Set flag for non-decimal input
ContainsUnDec = TRUE;
switch(IntBase)
{
case ID_8BIT :
{
NumParse = 2;
MaxVal = 255.0;
break;
}
case ID_16BIT :
{
NumParse = 4;
MaxVal = 65535.0;
break;
}
case ID_32BIT :
{
NumParse = 8;
MaxVal = 4294967295.0;
break;
}
}
yylval.Real = 0;
for(i = 0; ((i < leng) && (i < NumParse)); i++)
{
yylval.Real *= 16.0;
if((*s >= 'a') && (*s <= 'f'))
yylval.Real += (double)(*s - 'a' + 10);
else if((*s >= 'A') && (*s <= 'F'))
yylval.Real += (double)(*s - 'A' + 10);
else
yylval.Real += (double)(*s - '0');
s++;
}
if(IntType != ID_DECIMAL)
{
if(yylval.Real > MaxVal)
yylval.Real = MaxVal;
}
else if(IntSign == ID_SIGNED)
{
if(yylval.Real >= ((MaxVal + 1.0) / 2))
yylval.Real = -(MaxVal - yylval.Real + 1.0);
}
return(0);
}
/**********************************************************************/
/* Calc Binary value */
/**********************************************************************/
int calc_bin_value(char *s)
{
int i, NumParse;
double MaxVal;
// Set flag for non-decimal input
ContainsUnDec = TRUE;
// Check for how many bits to check for ;)
switch(IntBase)
{
case ID_8BIT :
{
NumParse = 8;
MaxVal = 255.0;
break;
}
case ID_16BIT :
{
NumParse = 16;
MaxVal = 65535.0;
break;
}
case ID_32BIT :
{
NumParse = 32;
MaxVal = 4294967295.0;
break;
}
}
// get to end of input
yylval.Real = 0;
// Convert to double
for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
{
yylval.Real *= 2.0;
if(*s++ == '1')
yylval.Real += 1.0;
}
if(IntType != ID_DECIMAL)
{
if(yylval.Real > MaxVal)
yylval.Real = MaxVal;
}
else if(IntSign == ID_SIGNED)
{
if(yylval.Real >= ((MaxVal + 1.0) / 2))
yylval.Real = -(MaxVal - yylval.Real + 1.0);
}
return(0);
}
/**********************************************************************/
/* Calc octal value */
/**********************************************************************/
int calc_oct_value(char *s)
{
int i, NumParse;
double MaxVal;
// Set flag for non-decimal input
ContainsUnDec = TRUE;
switch(IntBase)
{
case ID_8BIT :
{
NumParse = 3;
MaxVal = 255.0;
break;
}
case ID_16BIT :
{
NumParse = 6;
MaxVal = 65535.0;
break;
}
case ID_32BIT :
{
NumParse = 11;
MaxVal = 4294967295.0;
break;
}
}
yylval.Real = 0;
for(i = 0; ((i < (yyleng - 1)) && (i < NumParse)); i++)
{
yylval.Real *= 8.0;
yylval.Real += (double)(*s - '0');
s++;
}
if(IntType != ID_DECIMAL)
{
if(yylval.Real > MaxVal)
yylval.Real = MaxVal;
}
else if(IntSign == ID_SIGNED)
{
if(yylval.Real >= ((MaxVal + 1.0) / 2))
yylval.Real = -(MaxVal - yylval.Real + 1.0);
}
return(0);
}
/**********************************************************************/
/* Calc double */
/**********************************************************************/
int calc_dbl_value(char *s)
{
char *p;
yylval.Real = strtod(s, &p);
return(0);
}