public bool MarkToReplaceIfNodeIsOfTokenType(Production node, string tokenType, string to) { Node isOfType = GrammaticaNodeUtils.FindChildOf(node, new string[4] { "Variable_Declaration", "SemanticalParameters", "InOutSemanticalParameters", tokenType }); if (isOfType == null) { isOfType = GrammaticaNodeUtils.FindChildOf(node, new string[4] { "Variable_Declaration", "SemanticalParameters", "InputSemanticalParameters", tokenType }); } if (isOfType == null) { isOfType = GrammaticaNodeUtils.FindChildOf(node, new string[4] { "Variable_Declaration", "SemanticalParameters", "OutputSemanticalParameters", tokenType }); } if (isOfType != null) { Token name = (Token)GrammaticaNodeUtils.FindChildOf(node, new string[2] { "Variable_Declaration", "IDENTIFIER" }); replaceIdentifier.Add(name.GetImage(), to); return(true); } return(false); }
public override Node ExitFunctionOrVariableDeclaration(Production node) { DependencyGraphNode antiga = dependencyGraph.SearchDependant(((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage()); // se a função já existir if (antiga != null) { // trocar a nova pela n em todo o grafo. dependencyGraph.ReplaceDependant("Nova Função", antiga); } else { dependencyGraph.SearchDependant("Nova Função").SetDependencyName(((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage()); } functionScope = null; if (((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage().Equals(activeMainFunction)) { RemoveTypeFromMain(node); } scopeVars = new ArrayList(); replaceIdentifier = new Hashtable(); return(node); }
public override Node ExitStatement(Production node) { if (replaceReturn == false) { return(node); } Node n = GrammaticaNodeUtils.FindChildOf(node, new string[1] { "Return_Statement" }); if (n != null) { Node returnStatement = GrammaticaNodeUtils.FindChildOf(n, new string[1] { "RETURN" }); if (returnStatement != null) { ArrayList returnStatementChildren = GrammaticaNodeUtils.GetChildren((Production)n); returnStatementChildren.Insert(1, GrammaticaNodeUtils.CreateEqualToken()); returnStatementChildren.Insert(1, GrammaticaNodeUtils.CreateSpaceToken()); } } return(node); }
public override Node ExitAtom(Production node) { Token identifier = (Token)GrammaticaNodeUtils.FindChildOf(node, new string[2] { "Type", "IDENTIFIER" }); // Adds only globalVars. if (identifier != null && !scopeVars.Contains(identifier.GetImage())) { dependencyGraph.SearchDependant(identifier.GetImage()).AddCallsBy(functionScope); } ArrayList identifiers = GrammaticaNodeUtils.FindChildrenOf(node, new string[2] { "Identifier_Composed_Required", "IDENTIFIER" }); // Adds only globalVars. for (int i = 0; i < identifiers.Count; i++) { if (!scopeVars.Contains(((Token)identifiers[i]).GetImage())) { dependencyGraph.SearchDependant(((Token)identifiers[i]).GetImage()).AddCallsBy(functionScope); } } return(node); }
public override Node ExitFunctionConstructorCallOrVariableDeclaration(Production node) { Token identifier = (Token)GrammaticaNodeUtils.FindChildOf(node, new string[2] { "Type", "IDENTIFIER" }); if (identifier == null) { return(node); } replaceIntrinsicFunc(identifier); // mul(term, term) => term * term if (identifier.GetImage().Equals("mul")) { identifier.AddValue(""); Node virgula = GrammaticaNodeUtils.FindChildOf(node, new string[2] { "PartOf_Constructor_Call", "COMMA" }); virgula.AddValue(" * "); } // cross(T,N) => cross(N,T). if (identifier.GetImage().Equals("cross")) { Production listOfParam = (Production)GrammaticaNodeUtils.FindChildOf(node, "PartOf_Constructor_Call"); Node exp1 = GrammaticaNodeUtils.FindChildOf(listOfParam, "Expression", 1); Node exp2 = GrammaticaNodeUtils.FindChildOf(listOfParam, "Expression", 2); GrammaticaNodeUtils.SwapChildrenPosition(listOfParam, exp1, exp2); } // add dependent function Node n = GrammaticaNodeUtils.FindChildOf(node, "PartOf_Constructor_Call"); if (n != null && identifier != null && functionScope != null) { // function call or constructor call. dependencyGraph.SearchDependant(identifier.GetImage()).AddCallsBy(functionScope); } // Adds only globalVars. if (!scopeVars.Contains(identifier.GetImage())) { dependencyGraph.SearchDependant(identifier.GetImage()).AddCallsBy(functionScope); } return(node); }
public Node RemoveTypeFromMain(Production node) { Production tempType = (Production)GrammaticaNodeUtils.FindChildOf(node, "Type"); ArrayList tempVoid = GrammaticaNodeUtils.GetChildren(tempType); if (tempVoid != null) { tempVoid.RemoveAt(0); tempVoid.Insert(0, GrammaticaNodeUtils.CreateVoidToken()); } return(node); }
public Node RemoveSemanticParams(Production node) { ArrayList lista = GrammaticaNodeUtils.GetChildren(node); ArrayList trash = new ArrayList(); for (int i = 0; i < lista.Count; i++) { if ((((Node)lista[i]).GetName().Equals("DOUBLE_DOT"))) { trash.Add(lista[i]); } if ((((Node)lista[i]).GetName().Equals("SemanticalParameters"))) { trash.Add(lista[i]); } if ((((Node)lista[i]).GetName().Equals("Register_Func"))) { trash.Add(lista[i]); } if ((((Node)lista[i]).GetName().Equals("Packoffset_Func"))) { trash.Add(lista[i]); } } for (int j = 0; j < trash.Count; j++) { lista.Remove(trash[j]); } trash = new ArrayList(); // Remove spaces left for (int i = lista.Count - 1; i >= 0; i--) { if ((((Node)lista[i]).GetName().Equals("WS"))) { trash.Add(lista[i]); } else { break; } } for (int j = 0; j < trash.Count; j++) { lista.Remove(trash[j]); } return(node); }
// Dado um nodo pai, e o índice do nodo filho, esta função busca em índeces anteriores e posteriores // os comentários pertinentes e move para dentro do nodeToPut. // O objetivo desta função é preparar os nodos para serem movidos, levando consigo seus comentários. public static void MovePreviousLineRelatedCommentToInsideANode(Production nodeToSearch, int indexOfTheRelatedInstruction, Production nodeToPut) { ArrayList whereSearch = GrammaticaNodeUtils.GetChildren(nodeToSearch); ArrayList wherePut = GrammaticaNodeUtils.GetChildren(nodeToPut); int WSPos = indexOfTheRelatedInstruction - 1; // WS is always one before Function_Param if (WSPos >= 0) { Production WS = (Production)whereSearch[WSPos]; if (WS.GetName().Equals("WS")) // WS is optional { ArrayList tokens = GrammaticaNodeUtils.GetChildren(WS); // Search the first newline int afterTheFirstNewLine = tokens.Count; for (int j = tokens.Count - 3; j > 0; j--) { if (((Token)tokens[j]).GetName().Equals("NEWLINE") || ((Token)tokens[j]).GetName().Equals("NEWLINE2") || ((Token)tokens[j]).GetName().Equals("NEWLINE3")) { afterTheFirstNewLine = j; break; } } // add all after the first newline to variable declaration ArrayList toRemove = new ArrayList(); for (int j = afterTheFirstNewLine; j < tokens.Count; j++) { // adds to param childrens which will be moved to outside the function if (!((Token)tokens[j]).GetName().Equals("WHITESPACE") && !((Token)tokens[j]).GetName().Equals("TAB")) { wherePut.Insert(0, tokens[j]); toRemove.Add(tokens[j]); } } for (int j = 0; j < toRemove.Count; j++) { tokens.Remove(toRemove[j]); } } } }
public override Node ExitPartOfVariableDeclaration(Production node) { // record all variable declarations inside this function. Token variableDeclaration = (Token)GrammaticaNodeUtils.FindChildOf(node, new string[1] { "IDENTIFIER" }); if (variableDeclaration != null) { //Console.WriteLine(variableDeclaration.GetImage()); scopeVars.Add(variableDeclaration.GetImage()); if (removeRegAndPack == true) { RemoveSemanticParams(node); } } return(node); }
public override Node ExitParameters(Production node) { ArrayList listOfParams = GrammaticaNodeUtils.GetChildren(node); ArrayList trash = new ArrayList(); for (int i = 0; i < listOfParams.Count; i++) { if ((((Node)listOfParams[i]).GetName().Equals("WS"))) { trash.Add(listOfParams[i]); } } for (int j = 0; j < trash.Count; j++) { listOfParams.Remove(trash[j]); } return(node); }
public bool MarkIfNodeIsFuncSemanticalParameter(Production node, string tokenType, string to) { ArrayList lista = GrammaticaNodeUtils.GetChildren(node); for (int i = 0; i < lista.Count; i++) { if ((((Node)lista[i]).GetName().Equals("SemanticalParameters"))) { Token name = (Token)GrammaticaNodeUtils.FindChildOf(node, new string[3] { "SemanticalParameters", "InOutSemanticalParameters", tokenType }); if (name != null) { replaceReturnFor = to; return(true); } return(false); } } return(false); }
/** * Move the global vars to outside function declaration. */ public override Node ExitFile(Production node) { ArrayList lista = GrammaticaNodeUtils.GetChildren(node); Node mainFunction = null; for (int i = 0; i < lista.Count; i++) { mainFunction = (Node)lista[i]; if (mainFunction.GetName().Equals("Function_OR_Variable_Declaration")) { // move related comment in previous line to inside this function. GrammaticaNodeUtils.MovePreviousLineRelatedCommentToInsideANode(node, i, (Production)mainFunction); } } // search for the first main function. for (int i = 0; i < lista.Count; i++) { mainFunction = (Node)lista[i]; if (mainFunction.GetName().Equals("Function_OR_Variable_Declaration")) { if (mainFunctions.Contains(((Token)GrammaticaNodeUtils.FindChildOf(mainFunction, "IDENTIFIER")).GetImage())) { break; } } } // put the global vars before the first MainFunction foreach (Node var in globalVars) { lista.Insert(lista.IndexOf(mainFunction), var); //GrammaticaNodeUtils.SetParent(var, node); } return(node); }
public override Node ExitFunctionPart(Production node) { if (MarkIfNodeIsFuncSemanticalParameter(node, "COLOR", "gl_FragColor") || MarkIfNodeIsFuncSemanticalParameter(node, "NORMAL", "gl_Normal") // || MarkIfNodeIsFuncSemanticalParameter(node, "POSITION", "gl_Position", false) // || MarkIfNodeIsFuncSemanticalParameter(node, "POSITION", "gl_Vertex", true) // || MarkIfNodeIsFuncSemanticalParameter(node, "TEXCOORD0", "gl_MultiTexCoord0", true) ) { RemoveSemanticParams(node); } if (replaceReturn == true) { replaceReturn = false; Node funcBody = GrammaticaNodeUtils.FindChildOf(node, "Function_Body"); if (funcBody != null) { for (int i = 0; i < funcBody.GetChildCount(); i++) { Node tempStatement = GrammaticaNodeUtils.FindChildOf(funcBody.GetChildAt(i), "Return_Statement"); if (tempStatement != null) { Token tempReturn = (Token)GrammaticaNodeUtils.FindChildOf(tempStatement, "RETURN"); if (tempReturn != null) { tempReturn.RemoveAllValues(); tempReturn.AddValue(replaceReturnFor); } } } } } return(base.ExitFunctionPart(node)); }
/** print the node image and call the print melthod to its children*/ void WriteValuesRecursive(Node node) { if (node is Token) { if (((Token)node).GetImage().Equals(mainFunction)) { streamWriter.Write("main"); } else { WriteToken((Token)node); } } // exports only the dependencies of the function. if (node.GetName().Equals("Function_OR_Variable_Declaration")) { if ((functionsToExport.Contains(((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage())) || ((GrammaticaNodeUtils.FindChildOf(node, "Variable_Declaration_PART")) != null)) { WriteChildren(node); } } else if (node.GetName().Equals("Variable_Declaration") && node.GetParent().GetName().Equals("File")) { // Affects only variables moved out from function declarations. if (functionsToExport.Contains(((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage())) { WriteChildren(node); } } else { WriteChildren(node); } }
// Dado um nodo pai, e o índice do nodo filho, esta função busca em índeces anteriores e posteriores // os comentários pertinentes e move para dentro do nodeToPut. // O objetivo desta função é preparar os nodos para serem movidos, levando consigo seus comentários. public static void MoveSameLineRelatedCommentToInsideANode(Production nodeToSearch, int indexOfTheRelatedInstruction, Production nodeToPut) { ArrayList whereSearch = GrammaticaNodeUtils.GetChildren(nodeToSearch); ArrayList wherePut = GrammaticaNodeUtils.GetChildren(nodeToPut); // find related comment after the comma and before the \n Production relatedWS = null; Token relatedComma = null; // PARAM [WS] COMMA [WS] int WSPos = indexOfTheRelatedInstruction + 2; // WS is always one before Function_Param if (WSPos <= whereSearch.Count) { relatedWS = (Production)whereSearch[WSPos]; if (!relatedWS.GetName().Equals("WS")) // First WS is optional { relatedWS = null; relatedComma = (Token)whereSearch[WSPos]; WSPos++; if (WSPos <= whereSearch.Count) { relatedWS = (Production)whereSearch[WSPos]; } } else { relatedComma = (Token)whereSearch[WSPos - 1]; } } // move to related comment after the comma and before the \n if (relatedWS != null) { ArrayList toRemove = new ArrayList(); // add all after the comma and before newline ArrayList tokens = GrammaticaNodeUtils.GetChildren(relatedWS); for (int j = 0; j < tokens.Count; j++) { // adds to param childrens which will be moved to outside the function if (((Token)tokens[j]).GetName().Equals("NEWLINE") || ((Token)tokens[j]).GetName().Equals("NEWLINE2") || ((Token)tokens[j]).GetName().Equals("NEWLINE3")) { wherePut.Add(tokens[j]); toRemove.Add(tokens[j]); break; } wherePut.Add(tokens[j]); toRemove.Add(tokens[j]); } tokens.Clear(); for (int j = 0; j < toRemove.Count; j++) { tokens.Remove(toRemove[j]); } } // remove comma whereSearch.Remove(relatedComma); }
public bool MarkToReplaceIfNodeIsOfTokenType(Production node, string tokenType, string to, bool isIN) { Node isOfType = GrammaticaNodeUtils.FindChildOf(node, new string[4] { "Variable_Declaration", "SemanticalParameters", "InOutSemanticalParameters", tokenType }); if (isOfType == null) { isOfType = GrammaticaNodeUtils.FindChildOf(node, new string[4] { "Variable_Declaration", "SemanticalParameters", "InputSemanticalParameters", tokenType }); } if (isOfType == null) { isOfType = GrammaticaNodeUtils.FindChildOf(node, new string[4] { "Variable_Declaration", "SemanticalParameters", "OutputSemanticalParameters", tokenType }); } bool ok = false; if (isIN) { if (GrammaticaNodeUtils.FindChildOf(node, new string [2] { "In_out_inout", "INOUT" }) != null || GrammaticaNodeUtils.FindChildOf(node, new string [2] { "In_out_inout", "IN" }) != null || GrammaticaNodeUtils.FindChildOf(node, new string [2] { "In_out_inout", "OUT" }) == null || GrammaticaNodeUtils.FindChildOf(node, new string [1] { "In_out_inout" }) == null ) { ok = true; } } else { if (GrammaticaNodeUtils.FindChildOf(node, new string [2] { "In_out_inout", "INOUT" }) != null || GrammaticaNodeUtils.FindChildOf(node, new string [2] { "In_out_inout", "OUT" }) != null ) { ok = true; } } if (ok && isOfType != null) { Token name = (Token)GrammaticaNodeUtils.FindChildOf(node, new string[2] { "Variable_Declaration", "IDENTIFIER" }); replaceIdentifier.Add(name.GetImage(), to); return(true); } return(false); }
// For each param in every main HLSL function // Move the uniform and varying params outside the function with his own comments public override Node ExitListOfParams(Production node) { ArrayList listOfParams = GrammaticaNodeUtils.GetChildren(node); // Only main mainFunctions. // Based on the presence of in, out or inout keywords if (!isMain) { return(node); } isMain = false; // Searching params that will be removed at ExitFunctionParam for (int i = 0; i < listOfParams.Count; i++) { if (!(listOfParams[i] is Production)) { continue; } Production functionParam = (Production)listOfParams[i]; functionParam = (Production)CheckIfThisNodeIsSemanticAndRemoveIfItIs(functionParam); // Only uniform params are moved Node n = GrammaticaNodeUtils.FindChildOf(functionParam, new string[1] { "Variable_Declaration" }); if (n != null) // Param found // Getting childrens of it { Production variableDeclaration = (Production)n; variableDeclaration = (Production)RemoveSemanticParams(variableDeclaration); ArrayList variableDeclarationChildren = GrammaticaNodeUtils.GetChildren(variableDeclaration); // add dot_comma and new line variableDeclarationChildren.Add(GrammaticaNodeUtils.CreateDotCommaToken()); // move related comment in previous line to inside this param. GrammaticaNodeUtils.MovePreviousLineRelatedCommentToInsideANode(node, i, variableDeclaration); // move related commnet in the same line after the comma but before \n to this param. GrammaticaNodeUtils.MoveSameLineRelatedCommentToInsideANode(node, i, variableDeclaration); // add varying to all non-uniform parameters Node hasUniform = GrammaticaNodeUtils.FindChildOf(variableDeclaration, new string[2] { "Storage_Class", "UNIFORM" }); if (hasUniform == null) { GrammaticaNodeUtils.GetChildren(variableDeclaration).Insert(0, GrammaticaNodeUtils.CreateSpaceToken()); GrammaticaNodeUtils.GetChildren(variableDeclaration).Insert(0, GrammaticaNodeUtils.CreateVaryingToken()); GrammaticaNodeUtils.GetChildren(variableDeclaration).Insert(0, GrammaticaNodeUtils.CreateNewLineToken()); } // move uniform to outside. globalVars.Add(variableDeclaration); // remove from param. GrammaticaNodeUtils.GetChildren(functionParam).Remove(variableDeclaration); } } ArrayList trash = new ArrayList(); // cleaning the trash: Spaces, commas and newlines before the ) of the function for (int i = listOfParams.Count - 1; i >= 0; i--) { if (listOfParams[i] is Production) { if (((Production)listOfParams[i]).GetChildCount() == 0 || ((Production)listOfParams[i]).GetName().Equals("WS")) { trash.Add(listOfParams[i]); } else { break; } } if (listOfParams[i] is Token) { if (((Token)listOfParams[i]).GetName().Equals("COMMA")) { trash.Add(listOfParams[i]); } } } for (int j = 0; j < trash.Count; j++) { listOfParams.Remove(trash[j]); } return(node); }