public void Generate(MethodInfo[] mi) { string @namespace = mi[0].DeclaringType.Namespace; string @class = mi[0].DeclaringType.Name; c = new StreamWriter(Path.Combine("src", @namespace + "_" + @class + ".cpp"), false); h = new StreamWriter(Path.Combine("src", @namespace + "_" + @class + ".h"), false); cs = new StringBuilder(); hs = new StringBuilder(); //Must include headers - translate framework types and functions hs.Append("#include \"..\\Framework\\types.h\"\n"); hs.Append("namespace " + @namespace + " { \n class " + @class + "{ \n"); RecurH.Push("}"); RecurH.Push("};"); cs.AppendLine("#include \"" + @namespace + "_" + @class + ".h\""); for (lineNum = 0; lineNum < mi.Length; lineNum++) { MethodInfo m = mi[lineNum]; MethodBodyReader r = new MethodBodyReader(m); //Save the temporary MSIL string methodname = m.Name; lines = r.GetBodyCode().Split('\n'); File.WriteAllText(Path.Combine("src/MSIL", @namespace + "_" + @class + "_" + methodname + ".MSIL"), r.GetBodyCode()); //Parse and setup function parameters ParameterInfo[] @params = m.GetParameters(); string par = ""; if (@params.Length != 0) { par = @params[0].ParameterType.Name + " v_0"; Vars["v_0"] = @params[0].ParameterType.Name; for (int tmp = 1; tmp < @params.Length; tmp++) { par += ", " + @params[tmp].ParameterType.Name + " v_" + tmp.ToString(); Vars["v_" + tmp.ToString()] = @params[tmp].ParameterType.Name; } } //Setup function declaration in header hs.AppendFormat("{0} {1}({2});", m.ReturnType.Namespace + "::" + m.ReturnType.Name, m.Name, par); //Setup function definition in source cs.AppendLine(m.ReturnType.Namespace + "::" + m.ReturnType.Name + " " + @namespace + "::" + @class + "::" + m.Name + "(" + par + "){" ); RecurC.Push("}"); //Define all variables foreach (LocalVariableInfo locals in m.GetMethodBody().LocalVariables) { Vars.Add("V_" + locals.LocalIndex, locals.LocalType.Namespace + "::" + locals.LocalType.Name); cs.AppendLine(locals.LocalType.Namespace + "::" + locals.LocalType.Name + " " + "V_" + locals.LocalIndex + ";"); Vars["V_" + locals.LocalIndex] = ""; } bool exit = false; #region Generate code lineNum = 0; do { for (; lineNum < lines.Length; lineNum++) { //skip empty lines if (lines[lineNum].Trim() != string.Empty) { //Remove all whitespace from the string string line = lines[lineNum].Trim(); //Get the offset curOffset = long.Parse(line.Split(':')[0]); line = line.Remove(line.Split(':')[0] + ":").Trim(); //Check if the line is of any interest to us foreach (string key in Translators.Keys) { //if it is, call the appropriate handler and update the tokens if (line.StartsWith(key)) { string[] f = Translators[key](line); if (!string.IsNullOrWhiteSpace(f[0])) cs.AppendLine(f[0]); if (!string.IsNullOrWhiteSpace(f[1])) hs.AppendLine(f[1]); } } } } if (backupOffset.Count > 0) { lineNum = backupOffset.Pop(); } else { exit = true; } if (@else.Count > 0) { if (@else.Pop()) { cs.AppendLine(RecurC.Pop() + " else { "); } } } while (!exit); #endregion #region Header Recursion tree while (RecurH.Count > 0) { hs.AppendLine(RecurH.Pop()); } h.Write(hs); h.Flush(); #endregion #region Code Recursion tree while (RecurC.Count > 0) { cs.AppendLine(RecurC.Pop()); } c.Write(cs); c.Flush(); #endregion Process.Start("AStyle", "--style=allman --recursive src/*.cpp src/*.h"); } }
private void lbAvailableMethodsList_SelectedValueChanged(object sender, EventArgs e) { try { MethodInfo mi = methods[lbAvailableMethodsList.SelectedIndex]; SDILReader.MethodBodyReader mr = new MethodBodyReader(mi); rchMethodBodyCode.Clear(); rchMethodBodyCode.Text = mr.GetBodyCode(); } catch { } }