Bläddra i källkod

Added #defines and conditionals to BSLFX pre-processor

BearishSun 10 år sedan
förälder
incheckning
ae6f918cdd
5 ändrade filer med 753 tillägg och 456 borttagningar
  1. 555 454
      BansheeSL/BsLexerFX.c
  2. 8 2
      BansheeSL/BsLexerFX.h
  3. 67 0
      BansheeSL/BsLexerFX.l
  4. 23 0
      BansheeSL/Include/BsASTFX.h
  5. 100 0
      BansheeSL/Source/BsASTFX.c

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 555 - 454
BansheeSL/BsLexerFX.c


+ 8 - 2
BansheeSL/BsLexerFX.h

@@ -226,6 +226,12 @@ void yyfree (void * ,yyscan_t yyscanner );
 #define CODEBLOCK_EQUALS 3
 #define CODEBLOCK_EQUALS 3
 #define CODEBLOCK 4
 #define CODEBLOCK 4
 #define CODEBLOCK_END 5
 #define CODEBLOCK_END 5
+#define DEFINE_COND 6
+#define UNDEF_COND 7
+#define CONDITIONAL_IF 8
+#define CONDITIONAL_IFN 9
+#define CONDITIONAL_ELIF 10
+#define CONDITIONAL_IGNORE 11
 
 
 #endif
 #endif
 
 
@@ -342,9 +348,9 @@ extern int yylex \
 #undef YY_DECL
 #undef YY_DECL
 #endif
 #endif
 
 
-#line 311 "BsLexerFX.l"
+#line 378 "BsLexerFX.l"
 
 
 
 
-#line 349 "BsLexerFX.h"
+#line 355 "BsLexerFX.h"
 #undef yyIN_HEADER
 #undef yyIN_HEADER
 #endif /* yyHEADER_H */
 #endif /* yyHEADER_H */

+ 67 - 0
BansheeSL/BsLexerFX.l

@@ -24,6 +24,12 @@ COMMENT			\/\/[^\n]*
 %x CODEBLOCK_EQUALS
 %x CODEBLOCK_EQUALS
 %x CODEBLOCK
 %x CODEBLOCK
 %x CODEBLOCK_END
 %x CODEBLOCK_END
+%x DEFINE_COND
+%x UNDEF_COND
+%x CONDITIONAL_IF
+%x CONDITIONAL_IFN
+%x CONDITIONAL_ELIF
+%x CONDITIONAL_IGNORE
 
 
 %%
 %%
 
 
@@ -260,6 +266,67 @@ DYNAMIC			{ yylval->intValue = BUV_Dynamic; return TOKEN_BUFFERUSAGE; }
 	includePop(yyextra);
 	includePop(yyextra);
 }
 }
 
 
+#define							{ BEGIN(DEFINE_COND); }
+<DEFINE_COND>{WS}				{ /* Skip blank */ }
+<DEFINE_COND>{IDENTIFIER}		{ addDefine(yyextra, yytext); BEGIN(INITIAL); }
+<DEFINE_COND>.					{ return yytext[0]; }
+
+#undef							{ BEGIN(UNDEF_COND); }
+<UNDEF_COND>{WS}				{ /* Skip blank */ }
+<UNDEF_COND>{IDENTIFIER}		{ removeDefine(yyextra, yytext); BEGIN(INITIAL); }
+<UNDEF_COND>.					{ return yytext[0]; }
+
+#ifdef							{ BEGIN(CONDITIONAL_IF); }
+<CONDITIONAL_IF>{WS}			{ /* Skip blank */ }
+<CONDITIONAL_IF>{IDENTIFIER}	{ 
+	int isEnabled = pushConditional(yyextra, hasDefine(yyextra, yytext));
+	if(!isEnabled)
+		BEGIN(CONDITIONAL_IGNORE);
+	else
+		BEGIN(INITIAL);
+}
+<CONDITIONAL_IF>.				{ return yytext[0]; }
+
+#ifndef							{ BEGIN(CONDITIONAL_IFN); }
+<CONDITIONAL_IFN>{WS}			{ /* Skip blank */ }
+<CONDITIONAL_IFN>{IDENTIFIER}	{ 
+	int isEnabled = pushConditional(yyextra, !hasDefine(yyextra, yytext));
+	if(!isEnabled)
+		BEGIN(CONDITIONAL_IGNORE);
+	else
+		BEGIN(INITIAL);
+}
+<CONDITIONAL_IFN>.				{ return yytext[0]; }
+
+#else							{ BEGIN(CONDITIONAL_IGNORE); }
+#elif							{ BEGIN(CONDITIONAL_IGNORE); }
+
+#endif							{ popConditional(yyextra); }
+
+<CONDITIONAL_IGNORE>{WS}		{ /* Skip */ }
+<CONDITIONAL_IGNORE>#ifdef		{ pushConditional(yyextra, 0); }
+<CONDITIONAL_IGNORE>#ifndef		{ pushConditional(yyextra, 0); }
+<CONDITIONAL_IGNORE>#else		{ 
+	if(switchConditional(yyextra))
+		BEGIN(INITIAL);
+}
+<CONDITIONAL_IGNORE>#elif		{ BEGIN(CONDITIONAL_ELIF); }
+<CONDITIONAL_IGNORE>#endif		{ 
+	if(popConditional(yyextra))
+		BEGIN(INITIAL);
+}
+<CONDITIONAL_IGNORE>.			{ /* Skip */ }
+
+<CONDITIONAL_ELIF>{WS}			{ /* Skip blank */ }
+<CONDITIONAL_ELIF>{IDENTIFIER}	{ 
+	int isEnabled = setConditional(yyextra, hasDefine(yyextra, yytext));
+	if(!isEnabled)
+		BEGIN(CONDITIONAL_IGNORE);
+	else
+		BEGIN(INITIAL);
+}
+<CONDITIONAL_ELIF>.				{ return yytext[0]; }
+
 	/* Code blocks */
 	/* Code blocks */
 Vertex			{ BEGIN(CODEBLOCK_HEADER); return TOKEN_VERTEX; }
 Vertex			{ BEGIN(CODEBLOCK_HEADER); return TOKEN_VERTEX; }
 Fragment		{ BEGIN(CODEBLOCK_HEADER); return TOKEN_FRAGMENT; }
 Fragment		{ BEGIN(CODEBLOCK_HEADER); return TOKEN_FRAGMENT; }

+ 23 - 0
BansheeSL/Include/BsASTFX.h

@@ -19,6 +19,7 @@ typedef struct tagASTFXNode ASTFXNode;
 typedef struct tagNodeLink NodeLink;
 typedef struct tagNodeLink NodeLink;
 typedef struct tagIncludeData IncludeData;
 typedef struct tagIncludeData IncludeData;
 typedef struct tagIncludeLink IncludeLink;
 typedef struct tagIncludeLink IncludeLink;
+typedef struct tagConditionalData ConditionalData;
 typedef struct tagCodeString CodeString;
 typedef struct tagCodeString CodeString;
 typedef enum tagFillModeValue FillModeValue;
 typedef enum tagFillModeValue FillModeValue;
 typedef enum tagCullModeValue CullModeValue;
 typedef enum tagCullModeValue CullModeValue;
@@ -210,6 +211,14 @@ struct tagIncludeLink
 	IncludeLink* next;
 	IncludeLink* next;
 };
 };
 
 
+struct tagConditionalData
+{
+	int selfEnabled;
+	int enabled;
+
+	ConditionalData* next;
+};
+
 struct tagCodeString
 struct tagCodeString
 {
 {
 	char* code;
 	char* code;
@@ -238,6 +247,11 @@ struct tagParseState
 	CodeString* codeStrings;
 	CodeString* codeStrings;
 	int numCodeStrings;
 	int numCodeStrings;
 	int numOpenBrackets;
 	int numOpenBrackets;
+
+	char** defines;
+	int numDefines;
+	int defineCapacity;
+	ConditionalData* conditionalStack;
 };
 };
 
 
 struct tagOptionInfo
 struct tagOptionInfo
@@ -295,6 +309,15 @@ void beginCodeBlock(ParseState* parseState);
 void appendCodeBlock(ParseState* parseState, char value);
 void appendCodeBlock(ParseState* parseState, char value);
 int getCodeBlockIndex(ParseState* parseState);
 int getCodeBlockIndex(ParseState* parseState);
 
 
+void addDefine(ParseState* parseState, const char* value);
+int hasDefine(ParseState* parseState, const char* value);
+void removeDefine(ParseState* parseState, const char* value);
+
+int pushConditional(ParseState* parseState, int state);
+int switchConditional(ParseState* parseState);
+int setConditional(ParseState* parseState, int state);
+int popConditional(ParseState* parseState);
+
 char* getCurrentFilename(ParseState* parseState);
 char* getCurrentFilename(ParseState* parseState);
 
 
 ParseState* parseStateCreate();
 ParseState* parseStateCreate();

+ 100 - 0
BansheeSL/Source/BsASTFX.c

@@ -240,6 +240,101 @@ char* getCurrentFilename(ParseState* parseState)
 	return parseState->includeStack->data->filename;
 	return parseState->includeStack->data->filename;
 }
 }
 
 
+void addDefine(ParseState* parseState, const char* value)
+{
+	int defineIdx = parseState->numDefines;
+	parseState->numDefines++;
+
+	if(parseState->numDefines > parseState->defineCapacity)
+	{
+		int newCapacity = parseState->defineCapacity * 2;
+		char** newDefines = mmalloc(parseState->memContext, newCapacity * sizeof(char*));
+
+		memcpy(newDefines, parseState->defines, parseState->defineCapacity);
+
+		mmfree(parseState->defines);
+		parseState->defines = newDefines;
+		parseState->defineCapacity = newCapacity;
+	}
+
+	parseState->defines[defineIdx] = mmalloc_strdup(parseState->memContext, value);
+}
+
+int hasDefine(ParseState* parseState, const char* value)
+{
+	for (int i = 0; i < parseState->numDefines; i++)
+	{
+		if (strcmp(parseState->defines[i], value) == 0)
+			return 1;
+	}
+
+	return 0;
+}
+
+void removeDefine(ParseState* parseState, const char* value)
+{
+	for (int i = 0; i < parseState->numDefines; i++)
+	{
+		if (strcmp(parseState->defines[i], value) == 0)
+		{
+			int remaining = parseState->numDefines - (i + 1);
+
+			if(remaining > 0)
+				memcpy(&parseState->defines[i], &parseState->defines[i + 1], remaining);
+
+			parseState->numDefines--;
+		}
+	}
+}
+
+int pushConditional(ParseState* parseState, int state)
+{
+	ConditionalData* conditional = mmalloc(parseState->memContext, sizeof(ConditionalData));
+	conditional->enabled = state && (parseState->conditionalStack == 0 || parseState->conditionalStack->enabled);
+	conditional->selfEnabled = state;
+	conditional->next = parseState->conditionalStack;
+
+	parseState->conditionalStack = conditional;
+
+	return conditional->enabled;
+}
+
+int switchConditional(ParseState* parseState)
+{
+	if (parseState->conditionalStack == 0)
+		return 1;
+
+	ConditionalData* conditional = parseState->conditionalStack;
+	return setConditional(parseState, !conditional->selfEnabled);
+}
+
+int setConditional(ParseState* parseState, int state)
+{
+	if (parseState->conditionalStack == 0)
+		return 1;
+
+	ConditionalData* conditional = parseState->conditionalStack;
+	ConditionalData* parent = conditional->next;
+
+	conditional->enabled = state && (parent == 0 || parent->enabled);
+	conditional->selfEnabled = state;
+
+	return conditional->enabled;
+}
+
+int popConditional(ParseState* parseState)
+{
+	if (parseState->conditionalStack == 0)
+		return 1;
+
+	ConditionalData* conditional = parseState->conditionalStack;
+	parseState->conditionalStack = conditional->next;
+
+	mmfree(conditional);
+
+	return parseState->conditionalStack == 0 || parseState->conditionalStack->enabled;
+}
+
 ParseState* parseStateCreate()
 ParseState* parseStateCreate()
 {
 {
 	ParseState* parseState = (ParseState*)malloc(sizeof(ParseState));
 	ParseState* parseState = (ParseState*)malloc(sizeof(ParseState));
@@ -259,6 +354,11 @@ ParseState* parseStateCreate()
 	parseState->errorMessage = 0;
 	parseState->errorMessage = 0;
 	parseState->errorFile = 0;
 	parseState->errorFile = 0;
 
 
+	parseState->conditionalStack = 0;
+	parseState->defineCapacity = 10;
+	parseState->numDefines = 0;
+	parseState->defines = mmalloc(parseState->memContext, parseState->defineCapacity * sizeof(char*));
+
 	nodePush(parseState, parseState->rootNode);
 	nodePush(parseState, parseState->rootNode);
 
 
 	return parseState;
 	return parseState;

Vissa filer visades inte eftersom för många filer har ändrats