public static FindChildOf ( Node node, string childName ) : Node | ||
node | Node | |
childName | string | |
Résultat | 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 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 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 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 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); } }
// 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); }
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); }