1616#include < algorithm>
1717#include < ctype.h>
1818#include < string.h>
19+ #include < stack>
20+ #include < iostream>
1921
2022namespace M4
2123{
@@ -930,6 +932,7 @@ static const char* GetBinaryOpName(HLSLBinaryOp binaryOp)
930932 case HLSLBinaryOp_Sub: return " -" ;
931933 case HLSLBinaryOp_Mul: return " *" ;
932934 case HLSLBinaryOp_Div: return " /" ;
935+ case HLSLBinaryOp_Mod: return " %" ;
933936 case HLSLBinaryOp_Less: return " <" ;
934937 case HLSLBinaryOp_Greater: return " >" ;
935938 case HLSLBinaryOp_LessEqual: return " <=" ;
@@ -1118,6 +1121,25 @@ static CompareFunctionsResult CompareFunctions(HLSLTree* tree, const HLSLFunctio
11181121 }
11191122 }
11201123
1124+ // // Dump matched function argument types
1125+ // std::string arg1;
1126+ // std::string arg2;
1127+ // const HLSLArgument* argument1 = function1->argument;
1128+ // const HLSLArgument* argument2 = function2->argument;
1129+ // for (int i = 0; i < call->numArguments; ++i)
1130+ // {
1131+ // arg1 += GetTypeName(argument1->type);
1132+ // arg1 += " ";
1133+ // arg2 += GetTypeName(argument2->type);
1134+ // arg2 += " ";
1135+
1136+ // argument1 = argument1->nextArgument;
1137+ // argument2 = argument2->nextArgument;
1138+ // }
1139+
1140+ // std::cout << "(" << arg1 << ") vs (" << arg2 << ")" << std::endl;
1141+
1142+
11211143 return FunctionsEqual;
11221144
11231145}
@@ -1726,7 +1748,7 @@ bool HLSLParser::ParseStatement(HLSLStatement*& statement, const HLSLType& retur
17261748 return false ;
17271749 }
17281750 BeginScope ();
1729- if (!ParseDeclaration (forStatement->initialization ))
1751+ if (!ParseDeclaration (forStatement->initialization ) && ! ParseExpression (forStatement-> initializationWithoutType ) )
17301752 {
17311753 return false ;
17321754 }
@@ -2047,6 +2069,7 @@ bool HLSLParser::AcceptBinaryOperator(int priority, HLSLBinaryOp& binaryOp)
20472069 case ' -' : binaryOp = HLSLBinaryOp_Sub; break ;
20482070 case ' *' : binaryOp = HLSLBinaryOp_Mul; break ;
20492071 case ' /' : binaryOp = HLSLBinaryOp_Div; break ;
2072+ case ' %' : binaryOp = HLSLBinaryOp_Mod; break ;
20502073 case ' <' : binaryOp = HLSLBinaryOp_Less; break ;
20512074 case ' >' : binaryOp = HLSLBinaryOp_Greater; break ;
20522075 case HLSLToken_LessEqual: binaryOp = HLSLBinaryOp_LessEqual; break ;
@@ -2196,7 +2219,7 @@ bool HLSLParser::ParseBinaryExpression(int priority, HLSLExpression*& expression
21962219 {
21972220 const char * srcTypeName = GetTypeName (expression2->expressionType );
21982221 const char * dstTypeName = GetTypeName (expression1->expressionType );
2199- m_tokenizer.Error (" ':' no possible conversion from from '%s' to '%s'" , srcTypeName, dstTypeName);
2222+ m_tokenizer.Error (" ':' no possible conversion from '%s' to '%s'" , srcTypeName, dstTypeName);
22002223 return false ;
22012224 }
22022225
@@ -3468,14 +3491,47 @@ bool HLSLParser::ApplyPreprocessor(const char* fileName, const char* buffer, siz
34683491 index++;
34693492 }
34703493
3471- // Fouth pass, search and replace define uses
3494+ // Fouth pass, search and replace preprocessor directives
3495+ std::stack<bool > isCodeActive;
3496+ isCodeActive.push (true );
34723497 m_tokenizer = HLSLTokenizer (fileName, buffer, length);
34733498 sourcePreprocessed.clear ();
34743499 while (m_tokenizer.GetToken () != HLSLToken_EndOfStream)
34753500 {
34763501 bool addOriginalSource = true ;
34773502
3478- if (m_tokenizer.GetToken () == HLSLToken_PreprocessorDefine)
3503+ if (m_tokenizer.GetToken () == HLSLToken_PreprocessorIf)
3504+ {
3505+ while (m_tokenizer.GetToken () != HLSLToken_IntLiteral && m_tokenizer.GetToken () != HLSLToken_EndOfLine)
3506+ {
3507+ m_tokenizer.Next (false );
3508+ }
3509+
3510+ if (m_tokenizer.GetToken () == HLSLToken_IntLiteral)
3511+ {
3512+ isCodeActive.push (m_tokenizer.GetInt () != 0 );
3513+ }
3514+ else
3515+ {
3516+ m_tokenizer.Error (" #if evaluation failed: not an integer" );
3517+ return false ;
3518+ }
3519+ addOriginalSource = false ;
3520+ }
3521+ else if (m_tokenizer.GetToken () == HLSLToken_PreprocessorElse)
3522+ {
3523+ // Invert stack state
3524+ bool state = isCodeActive.top ();
3525+ isCodeActive.pop ();
3526+ isCodeActive.push (!state);
3527+ addOriginalSource = false ;
3528+ }
3529+ else if (m_tokenizer.GetToken () == HLSLToken_PreprocessorEndif)
3530+ {
3531+ isCodeActive.pop ();
3532+ addOriginalSource = false ;
3533+ }
3534+ else if (m_tokenizer.GetToken () == HLSLToken_PreprocessorDefine)
34793535 {
34803536 // Skip macros definition
34813537 while (m_tokenizer.GetToken () != HLSLToken_EndOfLine)
@@ -3485,10 +3541,14 @@ bool HLSLParser::ApplyPreprocessor(const char* fileName, const char* buffer, siz
34853541
34863542 addOriginalSource = false ;
34873543 }
3488- else if (m_tokenizer.GetToken () == HLSLToken_Identifier)
3544+ else if (m_tokenizer.GetToken () == HLSLToken_Identifier && isCodeActive. top () )
34893545 {
34903546 ProcessMacroFromIdentifier (sourcePreprocessed, addOriginalSource);
34913547 }
3548+ else if (!isCodeActive.top ())
3549+ {
3550+ addOriginalSource = false ;
3551+ }
34923552
34933553 if (addOriginalSource)
34943554 {
@@ -3500,7 +3560,7 @@ bool HLSLParser::ApplyPreprocessor(const char* fileName, const char* buffer, siz
35003560 }
35013561
35023562
3503- return true ;
3563+ return isCodeActive. size () == 1 ;
35043564}
35053565
35063566
@@ -3849,6 +3909,7 @@ bool HLSLParser::AcceptType(bool allowVoid, HLSLType& type/*, bool acceptFlags*/
38493909
38503910 if (!Expect (' >' ))
38513911 {
3912+ m_tokenizer.Error (" Syntax error: '>' expected for sampler type" );
38523913 return false ;
38533914 }
38543915 }
0 commit comments