private string fixBody(string originalBody, List <TypeName> existingInformation, bool parentFunctionIsStatic) { string modified = originalBody; Regex blocks = new Regex(@"{.*}"); //string[] outOfBlocks = Regex.Split(modified, "\\{.*\\}"); Dictionary <string, COOPClass> vars = new Dictionary <string, COOPClass>(); if (!parentFunctionIsStatic) { foreach (var parentClassVarName in parentClass.VarNames.Keys) { vars.TryAdd(parentClassVarName, parentClass.VarNames[parentClassVarName]); } } foreach (TypeName typeName in existingInformation) { vars.TryAdd(typeName.name.Remove(0, 2), typeName.@class); vars.Add(typeName.name, typeName.@class); } // Fix Variables Regex declareOnly = new Regex(@"(?<type>\w+)(\s+(?<names>\w+(,\s*\w+\s*)*));"); Regex declareAndAssign = new Regex(@"(?<type>\w+)\s+(?<name>\w+)\s*=\s*[^;]+;"); foreach (Match match in declareOnly.Matches(modified)) { string type = match.Groups["type"].Value; if (ReservedWords.isReserved(type)) { continue; } string[] decs = Regex.Split(match.Groups["names"].Value, "\\s*,\\s*"); COOPClass coopClass = classHierarchy.getClass(type); foreach (string dec in decs) { if (ReservedWords.isReserved(dec)) { continue; } vars.Add(dec, coopClass); } modified = modified.Replace(type, coopClass.convertToC() + " "); } foreach (Match match in declareAndAssign.Matches(modified)) { string type = match.Groups["type"].Value; string name = match.Groups["name"].Value; COOPClass coopClass = classHierarchy.getClass(type); vars.Add(name, coopClass); modified = Regex.Replace(modified, $"\\s+{type}\\s+", coopClass.convertToC() + " "); } foreach (COOPClass availableClass in availableClasses) { Regex constructorCall = new Regex($"new\\+({availableClass.Name})"); modified = constructorCall.Replace(modified, "__init__" + availableClass.Name); } //Fix all input parameters to correct types foreach (TypeName typeName in existingInformation) { modified = $"\t{[email protected]()} {typeName.name.Remove(0, 2)} = ({[email protected]()}) {typeName.name};\n" + modified; } //Regex functionCall = new Regex ("(?<caller>\\.+)\\s*\\.\\s*(?<function>\\w+)\\s*\\((?<inputs>\\s*(\\w+|\"[^\"]*\"|'.')\\s*(,\\s*(\\w+|\"[^\"]*\"|'.')\\s*)*)?\\)"); Regex functionCall = new Regex("[a-zA-Z_]\\w*\\s*(\\(.*\\))\\s*?(\\.\\s*[a-zA-Z_]\\w*(\\(.*\\))?)+"); FunctionCallConverter functionCallConverter = new FunctionCallConverter(originalNameAndInputTypesToMangledName, originalNameAndInputTypesToisStatic, functionToReturnType, availableClasses, vars); functionCallConverter.parentClass = parentClass; functionCallConverter.hierarchy = classHierarchy; foreach (Match match in functionCall.Matches(modified)) { string f = match.Value; string fixedStr = functionCallConverter.convert(f, classHierarchy)[0].ConvertedFunctionCall; modified = modified.Replace(match.Value, fixedStr); } /* * foreach (Match match in functionCall.Matches(modified)) { * string functionName = match.Groups["function"].Value; * string inputsFull = match.Groups["inputs"].Value; * string[] inputs = Regex.Split(inputsFull, "\\s*,\\s*"); * * Regex symbol = new Regex("\\w+"), character = new Regex("'.'"), @string = new Regex("\".*\""); * Regex integer = new Regex("\\d+.?"); * Regex floatingPoint = new Regex("\\d+\\.\\d+"); * List<COOPClass> inputTypes = new List<COOPClass>(); * foreach (string input in from s in inputs select "__" + s) { * * if (@string.IsMatch(input)) { * inputTypes.Add(COOPClass.String); * }else if (character.IsMatch(input)) { * inputTypes.Add(COOPPrimitives.@byte); * }else if (integer.IsMatch(input)) { * inputTypes.Add(COOPPrimitives.integer); * }else if (floatingPoint.IsMatch(input)) { * inputTypes.Add(COOPPrimitives.@float); * }else if (symbol.IsMatch(input)) { * * if(vars.TryGetValue(input, out COOPClass type)){ * inputTypes.Add(type); * } * } * } * * * string name = getMangledName(functionName, inputTypes); * * string callerSymbol = match.Groups["caller"].Value; * bool isStatic = getIsStatic(functionName, inputTypes); * if (!isStatic) { * * * if (name == "") { * inputTypes.Insert(0, null); * var line = classHierarchy.getLineage(vars[callerSymbol]); * line.Reverse(); * foreach (COOPClass coopClass in line) { * inputTypes[0] = coopClass; * name = getMangledName(functionName, inputTypes); * if (name != "") break; * } * } * } * * string inputParams = match.Groups["inputs"].Value; * if (!isStatic) { * * if(!(from f in existingInformation select f.name).Contains(callerSymbol) && * !vars.ContainsKey(callerSymbol)) { * callerSymbol = "this->" + callerSymbol; * } * inputParams = callerSymbol + ", " + inputParams; * } * * string modifiedCall = name + $"({inputParams})"; * int index = match.Index; * string temp = modified.Substring(0, index); * temp += modifiedCall; * temp += modified.Substring(index + match.Value.Length); * modified = temp; * } */ return(modified); }