Add support for calculated defines to parser (#2463)
* Add support for calculated defines to parser * Regenerate parser output
This commit is contained in:
parent
bbc8d39185
commit
aa318674e8
5 changed files with 156 additions and 19 deletions
|
@ -26,13 +26,13 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "DEG2RAD",
|
"name": "DEG2RAD",
|
||||||
"type": "UNKNOWN",
|
"type": "FLOAT_MATH",
|
||||||
"value": "(PI/180.0f)",
|
"value": "(PI/180.0f)",
|
||||||
"description": ""
|
"description": ""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "RAD2DEG",
|
"name": "RAD2DEG",
|
||||||
"type": "UNKNOWN",
|
"type": "FLOAT_MATH",
|
||||||
"value": "(180.0f/PI)",
|
"value": "(180.0f/PI)",
|
||||||
"description": ""
|
"description": ""
|
||||||
},
|
},
|
||||||
|
|
|
@ -26,13 +26,13 @@ return {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name = "DEG2RAD",
|
name = "DEG2RAD",
|
||||||
type = "UNKNOWN",
|
type = "FLOAT_MATH",
|
||||||
value = "(PI/180.0f)",
|
value = "(PI/180.0f)",
|
||||||
description = ""
|
description = ""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name = "RAD2DEG",
|
name = "RAD2DEG",
|
||||||
type = "UNKNOWN",
|
type = "FLOAT_MATH",
|
||||||
value = "(180.0f/PI)",
|
value = "(180.0f/PI)",
|
||||||
description = ""
|
description = ""
|
||||||
},
|
},
|
||||||
|
|
|
@ -23,12 +23,12 @@ Define 004: PI
|
||||||
Description:
|
Description:
|
||||||
Define 005: DEG2RAD
|
Define 005: DEG2RAD
|
||||||
Name: DEG2RAD
|
Name: DEG2RAD
|
||||||
Type: UNKNOWN
|
Type: FLOAT_MATH
|
||||||
Value: (PI/180.0f)
|
Value: (PI/180.0f)
|
||||||
Description:
|
Description:
|
||||||
Define 006: RAD2DEG
|
Define 006: RAD2DEG
|
||||||
Name: RAD2DEG
|
Name: RAD2DEG
|
||||||
Type: UNKNOWN
|
Type: FLOAT_MATH
|
||||||
Value: (180.0f/PI)
|
Value: (180.0f/PI)
|
||||||
Description:
|
Description:
|
||||||
Define 007: RL_MALLOC(sz)
|
Define 007: RL_MALLOC(sz)
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
<Define name="RAYLIB_VERSION" type="STRING" value="4.1-dev" desc="" />
|
<Define name="RAYLIB_VERSION" type="STRING" value="4.1-dev" desc="" />
|
||||||
<Define name="RLAPI" type="UNKNOWN" value="__declspec(dllexport)" desc="We are building the library as a Win32 shared library (.dll)" />
|
<Define name="RLAPI" type="UNKNOWN" value="__declspec(dllexport)" desc="We are building the library as a Win32 shared library (.dll)" />
|
||||||
<Define name="PI" type="FLOAT" value="3.14159265358979323846" desc="" />
|
<Define name="PI" type="FLOAT" value="3.14159265358979323846" desc="" />
|
||||||
<Define name="DEG2RAD" type="UNKNOWN" value="(PI/180.0f)" desc="" />
|
<Define name="DEG2RAD" type="FLOAT_MATH" value="(PI/180.0f)" desc="" />
|
||||||
<Define name="RAD2DEG" type="UNKNOWN" value="(180.0f/PI)" desc="" />
|
<Define name="RAD2DEG" type="FLOAT_MATH" value="(180.0f/PI)" desc="" />
|
||||||
<Define name="RL_MALLOC(sz)" type="MACRO" value="malloc(sz)" desc="" />
|
<Define name="RL_MALLOC(sz)" type="MACRO" value="malloc(sz)" desc="" />
|
||||||
<Define name="RL_CALLOC(n,sz)" type="MACRO" value="calloc(n,sz)" desc="" />
|
<Define name="RL_CALLOC(n,sz)" type="MACRO" value="calloc(n,sz)" desc="" />
|
||||||
<Define name="RL_REALLOC(ptr,sz)" type="MACRO" value="realloc(ptr,sz)" desc="" />
|
<Define name="RL_REALLOC(ptr,sz)" type="MACRO" value="realloc(ptr,sz)" desc="" />
|
||||||
|
|
|
@ -84,7 +84,22 @@
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Type of parsed define
|
// Type of parsed define
|
||||||
typedef enum { UNKNOWN = 0, MACRO, GUARD, INT, LONG, FLOAT, DOUBLE, CHAR, STRING, COLOR } DefineType;
|
typedef enum {
|
||||||
|
UNKNOWN = 0,
|
||||||
|
MACRO,
|
||||||
|
GUARD,
|
||||||
|
INT,
|
||||||
|
INT_MATH,
|
||||||
|
LONG,
|
||||||
|
LONG_MATH,
|
||||||
|
FLOAT,
|
||||||
|
FLOAT_MATH,
|
||||||
|
DOUBLE,
|
||||||
|
DOUBLE_MATH,
|
||||||
|
CHAR,
|
||||||
|
STRING,
|
||||||
|
COLOR
|
||||||
|
} DefineType;
|
||||||
|
|
||||||
// Define info data
|
// Define info data
|
||||||
typedef struct DefineInfo {
|
typedef struct DefineInfo {
|
||||||
|
@ -459,6 +474,124 @@ int main(int argc, char* argv[])
|
||||||
MemoryCopy(defines[defineIndex].desc, &linePtr[commentStart], commentLen);
|
MemoryCopy(defines[defineIndex].desc, &linePtr[commentStart], commentLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse defines of type UNKNOWN to find calculated numbers
|
||||||
|
if (defines[defineIndex].type == UNKNOWN)
|
||||||
|
{
|
||||||
|
DefineType largestType = UNKNOWN;
|
||||||
|
bool isMath = true;
|
||||||
|
char *valuePtr = defines[defineIndex].value;
|
||||||
|
|
||||||
|
for (int c = 0; c < TextLength(valuePtr); c++)
|
||||||
|
{
|
||||||
|
char ch = valuePtr[c];
|
||||||
|
|
||||||
|
// Skip operators and whitespace
|
||||||
|
if ((ch == '(') ||
|
||||||
|
(ch == ')') ||
|
||||||
|
(ch == '+') ||
|
||||||
|
(ch == '-') ||
|
||||||
|
(ch == '*') ||
|
||||||
|
(ch == '/') ||
|
||||||
|
(ch == ' ') ||
|
||||||
|
(ch == '\t')) continue;
|
||||||
|
|
||||||
|
// Read number operand
|
||||||
|
else if (isdigit(ch))
|
||||||
|
{
|
||||||
|
bool isNumber = true, isFloat = false;
|
||||||
|
while (!((ch == '(') ||
|
||||||
|
(ch == ')') ||
|
||||||
|
(ch == '*') ||
|
||||||
|
(ch == '/') ||
|
||||||
|
(ch == ' ') ||
|
||||||
|
(ch == '\t') ||
|
||||||
|
(ch == '\0')))
|
||||||
|
{
|
||||||
|
if (ch == '.') isFloat = true;
|
||||||
|
if (!(isdigit(ch) ||
|
||||||
|
((ch >= 'a') && (ch <= 'f')) ||
|
||||||
|
((ch >= 'A') && (ch <= 'F')) ||
|
||||||
|
(ch == 'x') ||
|
||||||
|
(ch == 'L') ||
|
||||||
|
(ch == '.') ||
|
||||||
|
(ch == '+') ||
|
||||||
|
(ch == '-')))
|
||||||
|
{
|
||||||
|
isNumber = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
c++;
|
||||||
|
ch = valuePtr[c];
|
||||||
|
}
|
||||||
|
if (isNumber)
|
||||||
|
{
|
||||||
|
// Found a valid number -> update largestType
|
||||||
|
DefineType numberType;
|
||||||
|
if (isFloat) numberType = valuePtr[c - 1] == 'f' ? FLOAT_MATH : DOUBLE_MATH;
|
||||||
|
else numberType = valuePtr[c - 1] == 'L' ? LONG_MATH : INT_MATH;
|
||||||
|
|
||||||
|
if (numberType > largestType) largestType = numberType;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isMath = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read string operand
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int operandStart = c;
|
||||||
|
while (!((ch == '\0') ||
|
||||||
|
(ch == ' ') ||
|
||||||
|
(ch == '(') ||
|
||||||
|
(ch == ')') ||
|
||||||
|
(ch == '+') ||
|
||||||
|
(ch == '-') ||
|
||||||
|
(ch == '*') ||
|
||||||
|
(ch == '/')))
|
||||||
|
{
|
||||||
|
c++;
|
||||||
|
ch = valuePtr[c];
|
||||||
|
}
|
||||||
|
int operandEnd = c;
|
||||||
|
int operandLength = operandEnd - operandStart;
|
||||||
|
|
||||||
|
// Search previous defines for operand
|
||||||
|
bool foundOperand = false;
|
||||||
|
for (int previousDefineIndex = 0; previousDefineIndex < defineIndex; previousDefineIndex++)
|
||||||
|
{
|
||||||
|
if (IsTextEqual(defines[previousDefineIndex].name, &valuePtr[operandStart], operandLength))
|
||||||
|
{
|
||||||
|
if ((defines[previousDefineIndex].type >= INT) && (defines[previousDefineIndex].type <= DOUBLE_MATH))
|
||||||
|
{
|
||||||
|
// Found operand and it's a number -> update largestType
|
||||||
|
if (defines[previousDefineIndex].type > largestType) largestType = defines[previousDefineIndex].type;
|
||||||
|
foundOperand = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!foundOperand)
|
||||||
|
{
|
||||||
|
isMath = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isMath)
|
||||||
|
{
|
||||||
|
// Define is a calculated number -> update type
|
||||||
|
if (largestType == INT) largestType = INT_MATH;
|
||||||
|
else if (largestType == LONG) largestType = LONG_MATH;
|
||||||
|
else if (largestType == FLOAT) largestType = FLOAT_MATH;
|
||||||
|
else if (largestType == DOUBLE) largestType = DOUBLE_MATH;
|
||||||
|
defines[defineIndex].type = largestType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
defineIndex++;
|
defineIndex++;
|
||||||
}
|
}
|
||||||
defineCount = defineIndex;
|
defineCount = defineIndex;
|
||||||
|
@ -1255,9 +1388,13 @@ static const char *StrDefineType(DefineType type)
|
||||||
case GUARD: return "GUARD";
|
case GUARD: return "GUARD";
|
||||||
case MACRO: return "MACRO";
|
case MACRO: return "MACRO";
|
||||||
case INT: return "INT";
|
case INT: return "INT";
|
||||||
|
case INT_MATH: return "INT_MATH";
|
||||||
case LONG: return "LONG";
|
case LONG: return "LONG";
|
||||||
|
case LONG_MATH: return "LONG_MATH";
|
||||||
case FLOAT: return "FLOAT";
|
case FLOAT: return "FLOAT";
|
||||||
|
case FLOAT_MATH: return "FLOAT_MATH";
|
||||||
case DOUBLE: return "DOUBLE";
|
case DOUBLE: return "DOUBLE";
|
||||||
|
case DOUBLE_MATH: return "DOUBLE_MATH";
|
||||||
case CHAR: return "CHAR";
|
case CHAR: return "CHAR";
|
||||||
case STRING: return "STRING";
|
case STRING: return "STRING";
|
||||||
case COLOR: return "COLOR";
|
case COLOR: return "COLOR";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue