mirror of
https://github.com/cc65/cc65.git
synced 2025-08-13 08:25:28 +00:00
Fixed parsing numeric constants.
This commit is contained in:
@@ -248,6 +248,45 @@ int IsSym (char* S)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int IsPPNumber (int Cur, int Next)
|
||||||
|
/* Return 1 if the two successive characters indicate a pp-number, otherwise
|
||||||
|
** return 0.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
return Cur != '.' ? IsDigit (Cur) : IsDigit (Next);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void CopyPPNumber (StrBuf* Target)
|
||||||
|
/* Copy a pp-number from the input to Target */
|
||||||
|
{
|
||||||
|
int Std;
|
||||||
|
|
||||||
|
if (!IsPPNumber (CurC, NextC)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* P-exp is only valid in C99 and later */
|
||||||
|
Std = IS_Get (&Standard);
|
||||||
|
while (IsIdent (CurC) || IsDigit (CurC) || CurC == '.') {
|
||||||
|
SB_AppendChar (Target, CurC);
|
||||||
|
if (NextC == '+' || NextC == '-') {
|
||||||
|
if (CurC == 'e' || CurC == 'E' ||
|
||||||
|
(Std >= STD_C99 && (CurC == 'p' || CurC == 'P'))) {
|
||||||
|
SB_AppendChar (Target, NextC);
|
||||||
|
NextChar ();
|
||||||
|
} else {
|
||||||
|
NextChar ();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NextChar ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void UnknownChar (char C)
|
static void UnknownChar (char C)
|
||||||
/* Error message for unknown character */
|
/* Error message for unknown character */
|
||||||
{
|
{
|
||||||
@@ -460,76 +499,77 @@ static void StringConst (void)
|
|||||||
static void NumericConst (void)
|
static void NumericConst (void)
|
||||||
/* Parse a numeric constant */
|
/* Parse a numeric constant */
|
||||||
{
|
{
|
||||||
unsigned Base; /* Temporary number base */
|
unsigned Base; /* Temporary number base according to prefix */
|
||||||
unsigned Prefix; /* Base according to prefix */
|
unsigned Index;
|
||||||
StrBuf S = STATIC_STRBUF_INITIALIZER;
|
StrBuf Src = AUTO_STRBUF_INITIALIZER;
|
||||||
int IsFloat;
|
int IsFloat;
|
||||||
char C;
|
char C;
|
||||||
unsigned DigitVal;
|
unsigned DigitVal;
|
||||||
unsigned long IVal; /* Value */
|
unsigned long IVal; /* Value */
|
||||||
|
|
||||||
|
/* Get the pp-number first, then parse on it */
|
||||||
|
CopyPPNumber (&Src);
|
||||||
|
SB_Terminate (&Src);
|
||||||
|
SB_Reset (&Src);
|
||||||
|
|
||||||
/* Check for a leading hex, octal or binary prefix and determine the
|
/* Check for a leading hex, octal or binary prefix and determine the
|
||||||
** possible integer types.
|
** possible integer types.
|
||||||
*/
|
*/
|
||||||
if (CurC == '0') {
|
if (SB_Peek (&Src) == '0') {
|
||||||
/* Gobble 0 and examine next char */
|
/* Gobble 0 and examine next char */
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
if (toupper (CurC) == 'X') {
|
if (toupper (SB_Peek (&Src)) == 'X' &&
|
||||||
Base = Prefix = 16;
|
IsXDigit (SB_LookAt (&Src, SB_GetIndex (&Src) + 1))) {
|
||||||
NextChar (); /* gobble "x" */
|
Base = 16;
|
||||||
} else if (toupper (CurC) == 'B' && IS_Get (&Standard) >= STD_CC65) {
|
SB_Skip (&Src); /* gobble "x" */
|
||||||
Base = Prefix = 2;
|
} else if (toupper (SB_Peek (&Src)) == 'B' &&
|
||||||
NextChar (); /* gobble 'b' */
|
IS_Get (&Standard) >= STD_CC65 &&
|
||||||
|
IsDigit (SB_LookAt (&Src, SB_GetIndex (&Src) + 1))) {
|
||||||
|
Base = 2;
|
||||||
|
SB_Skip (&Src); /* gobble 'b' */
|
||||||
} else {
|
} else {
|
||||||
Base = 10; /* Assume 10 for now - see below */
|
Base = 10; /* Assume 10 for now - see below */
|
||||||
Prefix = 8; /* Actual prefix says octal */
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Base = Prefix = 10;
|
Base = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Because floating point numbers don't have octal prefixes (a number
|
/* Because floating point numbers don't have octal prefixes (a number with
|
||||||
** with a leading zero is decimal), we first have to read the number
|
** a leading zero is decimal), we first have to read the number before
|
||||||
** before converting it, so we can determine if it's a float or an
|
** converting it, so we can determine if it's a float or an integer.
|
||||||
** integer.
|
|
||||||
*/
|
*/
|
||||||
while (IsXDigit (CurC) && HexVal (CurC) < Base) {
|
Index = SB_GetIndex (&Src);
|
||||||
SB_AppendChar (&S, CurC);
|
while ((C = SB_Peek (&Src)) != '\0' && (Base <= 10 ? IsDigit (C) : IsXDigit (C))) {
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
}
|
}
|
||||||
SB_Terminate (&S);
|
|
||||||
|
|
||||||
/* The following character tells us if we have an integer or floating
|
/* The following character tells us if we have an integer or floating
|
||||||
** point constant. Note: Hexadecimal floating point constants aren't
|
** point constant. Note: Hexadecimal floating point constants aren't
|
||||||
** supported in C89.
|
** supported in C89.
|
||||||
*/
|
*/
|
||||||
IsFloat = (CurC == '.' ||
|
IsFloat = (C == '.' ||
|
||||||
(Base == 10 && toupper (CurC) == 'E') ||
|
(Base == 10 && toupper (C) == 'E') ||
|
||||||
(Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
|
(Base == 16 && toupper (C) == 'P' && IS_Get (&Standard) >= STD_C99));
|
||||||
|
|
||||||
/* If we don't have a floating point type, an octal prefix results in an
|
/* An octal prefix for an integer type results in an octal base */
|
||||||
** octal base.
|
if (!IsFloat && Base == 10 && SB_LookAt (&Src, 0) == '0') {
|
||||||
*/
|
|
||||||
if (!IsFloat && Prefix == 8) {
|
|
||||||
Base = 8;
|
Base = 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Since we do now know the correct base, convert the remembered input
|
/* Since we now know the correct base, convert the input into a number */
|
||||||
** into a number.
|
SB_SetIndex (&Src, Index);
|
||||||
*/
|
|
||||||
SB_Reset (&S);
|
|
||||||
IVal = 0;
|
IVal = 0;
|
||||||
while ((C = SB_Get (&S)) != '\0') {
|
while ((C = SB_Peek (&Src)) != '\0' && (Base <= 10 ? IsDigit (C) : IsXDigit (C))) {
|
||||||
DigitVal = HexVal (C);
|
DigitVal = HexVal (C);
|
||||||
if (DigitVal >= Base) {
|
if (DigitVal >= Base) {
|
||||||
Error ("Numeric constant contains digits beyond the radix");
|
Error ("Invalid digit \"%c\" beyond radix %u constant", C, Base);
|
||||||
|
SB_Clear (&Src);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
IVal = (IVal * Base) + DigitVal;
|
IVal = (IVal * Base) + DigitVal;
|
||||||
|
SB_Skip (&Src);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We don't need the string buffer any longer */
|
|
||||||
SB_Done (&S);
|
|
||||||
|
|
||||||
/* Distinguish between integer and floating point constants */
|
/* Distinguish between integer and floating point constants */
|
||||||
if (!IsFloat) {
|
if (!IsFloat) {
|
||||||
|
|
||||||
@@ -540,27 +580,32 @@ static void NumericConst (void)
|
|||||||
** possible to convert the data to unsigned long even if the IT_ULONG
|
** possible to convert the data to unsigned long even if the IT_ULONG
|
||||||
** flag were not set, but we are not doing that.
|
** flag were not set, but we are not doing that.
|
||||||
*/
|
*/
|
||||||
if (toupper (CurC) == 'U') {
|
if (toupper (SB_Peek (&Src)) == 'U') {
|
||||||
/* Unsigned type */
|
/* Unsigned type */
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
if (toupper (CurC) != 'L') {
|
if (toupper (SB_Peek (&Src)) != 'L') {
|
||||||
Types = IT_UINT | IT_ULONG;
|
Types = IT_UINT | IT_ULONG;
|
||||||
} else {
|
} else {
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
Types = IT_ULONG;
|
Types = IT_ULONG;
|
||||||
}
|
}
|
||||||
} else if (toupper (CurC) == 'L') {
|
} else if (toupper (SB_Peek (&Src)) == 'L') {
|
||||||
/* Long type */
|
/* Long type */
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
if (toupper (CurC) != 'U') {
|
if (toupper (SB_Peek (&Src)) != 'U') {
|
||||||
Types = IT_LONG | IT_ULONG;
|
Types = IT_LONG | IT_ULONG;
|
||||||
WarnTypes = IT_ULONG;
|
WarnTypes = IT_ULONG;
|
||||||
} else {
|
} else {
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
Types = IT_ULONG;
|
Types = IT_ULONG;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Prefix == 10) {
|
if (SB_Peek (&Src) != '\0') {
|
||||||
|
Error ("Invalid suffix \"%s\" on integer constant",
|
||||||
|
SB_GetConstBuf (&Src) + SB_GetIndex (&Src));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Base == 10) {
|
||||||
/* Decimal constants are of any type but uint */
|
/* Decimal constants are of any type but uint */
|
||||||
Types = IT_INT | IT_LONG | IT_ULONG;
|
Types = IT_INT | IT_LONG | IT_ULONG;
|
||||||
WarnTypes = IT_LONG | IT_ULONG;
|
WarnTypes = IT_LONG | IT_ULONG;
|
||||||
@@ -624,16 +669,16 @@ static void NumericConst (void)
|
|||||||
Double FVal = FP_D_FromInt (IVal); /* Convert to double */
|
Double FVal = FP_D_FromInt (IVal); /* Convert to double */
|
||||||
|
|
||||||
/* Check for a fractional part and read it */
|
/* Check for a fractional part and read it */
|
||||||
if (CurC == '.') {
|
if (SB_Peek (&Src) == '.') {
|
||||||
|
|
||||||
Double Scale;
|
Double Scale;
|
||||||
|
|
||||||
/* Skip the dot */
|
/* Skip the dot */
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
|
|
||||||
/* Read fractional digits */
|
/* Read fractional digits */
|
||||||
Scale = FP_D_Make (1.0);
|
Scale = FP_D_Make (1.0);
|
||||||
while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
|
while (IsXDigit (SB_Peek (&Src)) && (DigitVal = HexVal (SB_Peek (&Src))) < Base) {
|
||||||
/* Get the value of this digit */
|
/* Get the value of this digit */
|
||||||
Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
|
Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
|
||||||
/* Add it to the float value */
|
/* Add it to the float value */
|
||||||
@@ -641,25 +686,25 @@ static void NumericConst (void)
|
|||||||
/* Scale base */
|
/* Scale base */
|
||||||
Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
|
Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
|
||||||
/* Skip the digit */
|
/* Skip the digit */
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for an exponent and read it */
|
/* Check for an exponent and read it */
|
||||||
if ((Base == 16 && toupper (CurC) == 'F') ||
|
if ((Base == 16 && toupper (SB_Peek (&Src)) == 'P') ||
|
||||||
(Base == 10 && toupper (CurC) == 'E')) {
|
(Base == 10 && toupper (SB_Peek (&Src)) == 'E')) {
|
||||||
|
|
||||||
unsigned Digits;
|
unsigned Digits;
|
||||||
unsigned Exp;
|
unsigned Exp;
|
||||||
|
|
||||||
/* Skip the exponent notifier */
|
/* Skip the exponent notifier */
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
|
|
||||||
/* Read an optional sign */
|
/* Read an optional sign */
|
||||||
if (CurC == '-') {
|
if (SB_Peek (&Src) == '-') {
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
} else if (CurC == '+') {
|
} else if (SB_Peek (&Src) == '+') {
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read exponent digits. Since we support only 32 bit floats
|
/* Read exponent digits. Since we support only 32 bit floats
|
||||||
@@ -670,11 +715,11 @@ static void NumericConst (void)
|
|||||||
*/
|
*/
|
||||||
Digits = 0;
|
Digits = 0;
|
||||||
Exp = 0;
|
Exp = 0;
|
||||||
while (IsDigit (CurC)) {
|
while (IsDigit (SB_Peek (&Src))) {
|
||||||
if (++Digits <= 3) {
|
if (++Digits <= 3) {
|
||||||
Exp = Exp * 10 + HexVal (CurC);
|
Exp = Exp * 10 + HexVal (SB_Peek (&Src));
|
||||||
}
|
}
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for errors: We must have exponent digits, and not more
|
/* Check for errors: We must have exponent digits, and not more
|
||||||
@@ -693,10 +738,14 @@ static void NumericConst (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check for a suffix and determine the type of the constant */
|
/* Check for a suffix and determine the type of the constant */
|
||||||
if (toupper (CurC) == 'F') {
|
if (toupper (SB_Peek (&Src)) == 'F') {
|
||||||
NextChar ();
|
SB_Skip (&Src);
|
||||||
NextTok.Type = type_float;
|
NextTok.Type = type_float;
|
||||||
} else {
|
} else {
|
||||||
|
if (SB_Peek (&Src) != '\0') {
|
||||||
|
Error ("Invalid suffix \"%s\" on floating constant",
|
||||||
|
SB_GetConstBuf (&Src) + SB_GetIndex (&Src));
|
||||||
|
}
|
||||||
NextTok.Type = type_double;
|
NextTok.Type = type_double;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -705,6 +754,9 @@ static void NumericConst (void)
|
|||||||
NextTok.Tok = TOK_FCONST;
|
NextTok.Tok = TOK_FCONST;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* We don't need the string buffer any longer */
|
||||||
|
SB_Done (&Src);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -746,7 +798,7 @@ void NextToken (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Determine the next token from the lookahead */
|
/* Determine the next token from the lookahead */
|
||||||
if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
|
if (IsPPNumber (CurC, NextC)) {
|
||||||
/* A number */
|
/* A number */
|
||||||
NumericConst ();
|
NumericConst ();
|
||||||
return;
|
return;
|
||||||
|
@@ -284,6 +284,14 @@ void SymName (char* S);
|
|||||||
int IsSym (char* S);
|
int IsSym (char* S);
|
||||||
/* If a symbol follows, read it and return 1, otherwise return 0 */
|
/* If a symbol follows, read it and return 1, otherwise return 0 */
|
||||||
|
|
||||||
|
int IsPPNumber (int Cur, int Next);
|
||||||
|
/* Return 1 if the two successive characters indicate a pp-number, otherwise
|
||||||
|
** return 0.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void CopyPPNumber (StrBuf* Target);
|
||||||
|
/* Copy a pp-number from the input to Target */
|
||||||
|
|
||||||
void NextToken (void);
|
void NextToken (void);
|
||||||
/* Get next token from input stream */
|
/* Get next token from input stream */
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user