/// <summary> /// Convert a newer variable or an existing variable. /// Keeps the current value of this variable. /// </summary> /// <param name="proc">process object</param> /// <param name="converter">converter object</param> /// <param name="varName">name of the variable</param> /// <param name="desiredDataType">data type to convert</param> /// <returns>the conversion string result</returns> public static string ConvertVariableType(IProcessInstance proc, ICodeConverter converter, string varName, EnumDataType desiredDataType) { // store the result in the scope with the varName parameter if (proc.CurrentScope.Exists(varName)) { string result = String.Empty; // get the variable infos IData myVar = proc.CurrentScope.GetVariable(varName); // specific prefix is used with desired data type if (myVar.TypeExists(desiredDataType)) { // if the desired data type is not the current type if (myVar.DataType != desiredDataType) { TypedVariable tv = new TypedVariable(myVar.Name, myVar.Prefix, myVar.DataType, desiredDataType); result = tv.MoveType(converter, proc.CurrentScope); } } else { TypedVariable tv = new TypedVariable(myVar.Name, myVar.Prefix, myVar.DataType, desiredDataType); result = tv.MoveType(converter, proc.CurrentScope); } return(result); } else { throw new ArgumentException("La variable '" + varName + "' n'existe pas dans le scope"); } }
public static void IsolatedFunction(ICompilateurInstance comp, IProcessInstance proc, ICodeConverter converter, IsolatedFunctionDelegate d) { if (converter.CurrentFunction.IsStaticControlFlow) { Dictionary <string, IParameter> varNames, leftNames; Action <KeyValuePair <string, IParameter> > action; TypedVariable tv; IFunction prec = converter.CurrentFunction; IFunction thisFunc = new Function(); thisFunc.IndentSize = prec.IndentSize; thisFunc.IsVisible = true; thisFunc.StrictName = "inner_" + comp.Unique.ComputeNewString(); thisFunc.PropagateControlFlow(prec); // ajoute la fonction dans la liste converter.ImplementedFunctions.Add(thisFunc); // change la fonction courante converter.SetCurrentFunction(thisFunc); d(); // retourne à la fonction précédente pour traiter le changement de type converter.SetCurrentFunction(prec); // supprime la fonction de la liste converter.ImplementedFunctions.Remove(thisFunc); leftNames = new Dictionary <string, IParameter>(); // élimine les variables passées en paramètre qui n'ont pas changé de type thisFunc.Parameters.ForEach(new Action <IParameter>(delegate(IParameter par) { if (!leftNames.ContainsKey(par.VariableName) && par.Order != EnumParameterOrder.E_NEW) { leftNames.Add(par.VariableName, par); } })); // enregistre le nom distinct des variables passées en paramètre qui soient mutable et initiale varNames = new Dictionary <string, IParameter>(); thisFunc.Parameters.ForEach(new Action <IParameter>(delegate(IParameter par) { if (!varNames.ContainsKey(par.VariableName) && leftNames.ContainsKey(par.VariableName) && par.IsMutableParameter && par.Order == EnumParameterOrder.E_NEW) { varNames.Add(par.VariableName, par); } })); // modifie toutes les variables en SimpleType action = new Action <KeyValuePair <string, IParameter> >(delegate(KeyValuePair <string, IParameter> kv) { Regex reg = new Regex(@"\$\[([^:]*):([a-z]+_" + kv.Key + @")\]"); thisFunc.Source = reg.Replace(thisFunc.Source, new MatchEvaluator(delegate(Match m) { string type = m.Groups[1].Value; string name = m.Groups[2].Value; int pos = name.IndexOf('_'); string result = String.Empty; if (pos != -1) { result = "$[" + type + ":st_" + name.Substring(pos + 1) + "]"; } else { result = "$[" + type + ":st_" + name + "]"; } return(result); })); tv = new TypedVariable(kv.Value.VariableName, kv.Value.FormalParameter, kv.Value.DataType, EnumDataType.E_SIMPLETYPE); tv.MoveType(converter, proc.CurrentScope); }); foreach (KeyValuePair <string, IParameter> kv in varNames.Except(leftNames)) { action(kv); } // copie de l'implémentation de cette fonction dans la fonction en cours prec.Source += thisFunc.Source; // copies des variables locales de cette fonction dans la fonction en cours thisFunc.LocalVariables.ForEach(new Action <IStructure>(delegate(IStructure loc) { if (!converter.CurrentFunction.LocalVariables.Exists(new Predicate <IStructure>(delegate(IStructure cur) { return(cur.PrefixedFieldName == loc.PrefixedFieldName); }))) { converter.CurrentFunction.LocalVariables.Add(loc); } })); // mettre à jour les paramètres foreach (KeyValuePair <string, IParameter> kv in varNames.Except(leftNames)) { List <IParameter> pars = thisFunc.Parameters.FindAll(new Predicate <IParameter>(delegate(IParameter par) { return(par.VariableName == kv.Key); })); if (pars.Count > 1) { pars.ForEach(new Action <IParameter>(delegate(IParameter update) { if (update.Order == EnumParameterOrder.E_LAST) { tv = new TypedVariable(update.VariableName, update.FormalParameter, EnumDataType.E_SIMPLETYPE, update.DataType); tv.MoveType(converter, proc.CurrentScope); } })); } else if (pars.Count == 1) { tv = new TypedVariable(pars[0].VariableName, pars[0].FormalParameter, EnumDataType.E_SIMPLETYPE, pars[0].DataType); tv.MoveType(converter, proc.CurrentScope); } } } else { d(); } }