Explorar el Código

Add support for calculated defines to parser (#2463)

* Add support for calculated defines to parser

* Regenerate parser output
pull/2467/head
lazaray hace 2 años
cometido por GitHub
padre
commit
aa318674e8
No se encontró ninguna clave conocida en la base de datos para esta firma ID de clave GPG: 4AEE18F83AFDEB23
Se han modificado 5 ficheros con 156 adiciones y 19 borrados
  1. +2
    -2
      parser/raylib_api.json
  2. +2
    -2
      parser/raylib_api.lua
  3. +2
    -2
      parser/raylib_api.txt
  4. +2
    -2
      parser/raylib_api.xml
  5. +148
    -11
      parser/raylib_parser.c

+ 2
- 2
parser/raylib_api.json Ver fichero

@ -26,13 +26,13 @@
},
{
"name": "DEG2RAD",
"type": "UNKNOWN",
"type": "FLOAT_MATH",
"value": "(PI/180.0f)",
"description": ""
},
{
"name": "RAD2DEG",
"type": "UNKNOWN",
"type": "FLOAT_MATH",
"value": "(180.0f/PI)",
"description": ""
},

+ 2
- 2
parser/raylib_api.lua Ver fichero

@ -26,13 +26,13 @@ return {
},
{
name = "DEG2RAD",
type = "UNKNOWN",
type = "FLOAT_MATH",
value = "(PI/180.0f)",
description = ""
},
{
name = "RAD2DEG",
type = "UNKNOWN",
type = "FLOAT_MATH",
value = "(180.0f/PI)",
description = ""
},

+ 2
- 2
parser/raylib_api.txt Ver fichero

@ -23,12 +23,12 @@ Define 004: PI
Description:
Define 005: DEG2RAD
Name: DEG2RAD
Type: UNKNOWN
Type: FLOAT_MATH
Value: (PI/180.0f)
Description:
Define 006: RAD2DEG
Name: RAD2DEG
Type: UNKNOWN
Type: FLOAT_MATH
Value: (180.0f/PI)
Description:
Define 007: RL_MALLOC(sz)

+ 2
- 2
parser/raylib_api.xml Ver fichero

@ -5,8 +5,8 @@
<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="PI" type="FLOAT" value="3.14159265358979323846" desc="" />
<Define name="DEG2RAD" type="UNKNOWN" value="(PI/180.0f)" desc="" />
<Define name="RAD2DEG" type="UNKNOWN" value="(180.0f/PI)" desc="" />
<Define name="DEG2RAD" type="FLOAT_MATH" value="(PI/180.0f)" 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_CALLOC(n,sz)" type="MACRO" value="calloc(n,sz)" desc="" />
<Define name="RL_REALLOC(ptr,sz)" type="MACRO" value="realloc(ptr,sz)" desc="" />

+ 148
- 11
parser/raylib_parser.c Ver fichero

@ -84,7 +84,22 @@
//----------------------------------------------------------------------------------
// 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
typedef struct DefineInfo {
@ -459,6 +474,124 @@ int main(int argc, char* argv[])
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++;
}
defineCount = defineIndex;
@ -1251,16 +1384,20 @@ static const char *StrDefineType(DefineType type)
{
switch (type)
{
case UNKNOWN: return "UNKNOWN";
case GUARD: return "GUARD";
case MACRO: return "MACRO";
case INT: return "INT";
case LONG: return "LONG";
case FLOAT: return "FLOAT";
case DOUBLE: return "DOUBLE";
case CHAR: return "CHAR";
case STRING: return "STRING";
case COLOR: return "COLOR";
case UNKNOWN: return "UNKNOWN";
case GUARD: return "GUARD";
case MACRO: return "MACRO";
case INT: return "INT";
case INT_MATH: return "INT_MATH";
case LONG: return "LONG";
case LONG_MATH: return "LONG_MATH";
case FLOAT: return "FLOAT";
case FLOAT_MATH: return "FLOAT_MATH";
case DOUBLE: return "DOUBLE";
case DOUBLE_MATH: return "DOUBLE_MATH";
case CHAR: return "CHAR";
case STRING: return "STRING";
case COLOR: return "COLOR";
}
return "";
}

Cargando…
Cancelar
Guardar