internal ParametricGate CreateParametricGate(MethodInfo method, object[] parameters) { string name = method.Name; string invName = name; string[] sep = new string[] { "Inverse" }; if (name.StartsWith("Inverse")) { string[] splitted = name.Split(sep, StringSplitOptions.RemoveEmptyEntries); invName = splitted[0]; } else { invName = "Inverse" + name; } MethodInfo computationMethod = FindExtension(name, parameters, LibExtensionGates); MethodInfo inverseMethod = FindExtension(invName, parameters, LibExtensionGates); MethodCode methodCode = FindMethodCode(computationMethod); string methodBody = null; if (methodCode != null) { methodBody = methodCode.Code; } for (int i = 0; i < parameters.Length; i++) { Type t = parameters[i].GetType(); if (t == typeof(RegisterRef)) { parameters[i] = ((RegisterRef)parameters[i]).ToRefModel(); } else if (t == typeof(Register)) { parameters[i] = (parameters[i] as Register).ToPartModel(); } else if (t == typeof(RegisterRef[])) { parameters[i] = ((RegisterRef[])parameters[i]) .Select <RegisterRef, RegisterRefModel>(x => x.ToRefModel()).ToArray(); } else if (t == typeof(Register[])) { parameters[i] = (parameters[i] as Register[]) .Select <Register, RegisterPartModel>(x => x.ToPartModel()).ToArray(); } } ParametricGate cg = new ParametricGate(method, computationMethod, inverseMethod, methodBody, parameters); return(cg); }
public Dictionary<string, List<MethodCode>> GetMethodsCodes(string code) { Dictionary<string, List<MethodCode>> bodies = new Dictionary<string, List<MethodCode>>(); string noComments = RemoveSingleLineComments(code); noComments = RemoveMultiLineComments(noComments); List<string> extDefs = GetExtensionDefs(noComments); char[] braces = new char[] { '{', '}' }; foreach (string def in extDefs) { int defInd = noComments.IndexOf(def); int ind = noComments.IndexOf('{', defInd + def.Length); int next = noComments.IndexOfAny(braces, ind + 1); int prev = ind; int opened = 1; while (opened > 0 && next > -1) { if (noComments[next] == '{') { opened++; } else { opened--; } prev = next; next = noComments.IndexOfAny(braces, next + 1); } if (opened == 0) { string body = noComments.Substring(defInd, prev - defInd + 1); string name = GetName(def); string paramDef = GetParamDef(def); paramDef = RemoveSqares(paramDef); paramDef = RemoveAngles(paramDef); paramDef = RemoveRounds(paramDef); paramDef = RemoveBraces(paramDef); string[] pars = GetParams(paramDef); string[] types = pars.Select(x => GetKeywordTypeName(x).Item2).ToArray(); MethodCode meth = new MethodCode() { Name = name, Code = body, ParametersTypes = types }; List<MethodCode> existed; if (bodies.TryGetValue(name, out existed)) { existed.Add(meth); } else { existed = new List<MethodCode>(); existed.Add(meth); bodies[name] = existed; } } } return bodies; }
public Dictionary <string, List <MethodCode> > GetMethodsCodes(string code) { Dictionary <string, List <MethodCode> > bodies = new Dictionary <string, List <MethodCode> >(); string noComments = RemoveSingleLineComments(code); noComments = RemoveMultiLineComments(noComments); List <string> extDefs = GetExtensionDefs(noComments); char[] braces = new char[] { '{', '}' }; foreach (string def in extDefs) { int defInd = noComments.IndexOf(def); int ind = noComments.IndexOf('{', defInd + def.Length); int next = noComments.IndexOfAny(braces, ind + 1); int prev = ind; int opened = 1; while (opened > 0 && next > -1) { if (noComments[next] == '{') { opened++; } else { opened--; } prev = next; next = noComments.IndexOfAny(braces, next + 1); } if (opened == 0) { string body = noComments.Substring(defInd, prev - defInd + 1); string name = GetName(def); string paramDef = GetParamDef(def); paramDef = RemoveSqares(paramDef); paramDef = RemoveAngles(paramDef); paramDef = RemoveRounds(paramDef); paramDef = RemoveBraces(paramDef); string[] pars = GetParams(paramDef); string[] types = pars.Select(x => GetKeywordTypeName(x).Item2).ToArray(); MethodCode meth = new MethodCode() { Name = name, Code = body, ParametersTypes = types }; List <MethodCode> existed; if (bodies.TryGetValue(name, out existed)) { existed.Add(meth); } else { existed = new List <MethodCode>(); existed.Add(meth); bodies[name] = existed; } } } return(bodies); }