/// <summary> /// Map variable to shorter name. /// </summary> /// <param name="core"></param> /// <param name="astNode"></param> /// <param name="shortNameMap"></param> /// <param name="nameGenerator"></param> /// <param name="mappedVariables"></param> /// <param name="typeHints"></param> private static void ShortNameMapping( ProtoCore.Core core, AssociativeNode astNode, Dictionary <string, string> shortNameMap, TypeDependentNameGenetrator nameGenerator, HashSet <string> mappedVariables, Dictionary <string, ProtoCore.Type> typeHints) { Action <IdentifierNode> func = n => { string shortName; if (shortNameMap.TryGetValue(n.Value, out shortName)) { if (string.IsNullOrEmpty(shortName)) { shortName = GetNextShortName(nameGenerator, typeHints, mappedVariables, n.Value); shortNameMap[n.Value] = shortName; } n.Value = n.Name = shortName; } }; IdentifierVisitor visitor = new IdentifierVisitor(func, core); astNode.Accept(visitor); }
/// <summary> /// Remap variables. /// </summary> /// <param name="core"></param> /// <param name="astNode"></param> /// <param name="renamingMap"></param> /// <param name="outputMap"></param> /// <param name="mappedVariable"></param> private static void VariableRemapping( ProtoCore.Core core, AssociativeNode astNode, Dictionary <string, string> renamingMap, Dictionary <string, string> outputMap, HashSet <string> mappedVariable) { Action <IdentifierNode> func = n => { string newIdent; if (renamingMap.TryGetValue(n.Value, out newIdent)) { var ident = n.Value; n.Value = n.Name = newIdent; outputMap[ident] = newIdent; mappedVariable.Add(newIdent); } }; IdentifierVisitor visitor = new IdentifierVisitor(func, core); astNode.Accept(visitor); }
/// <summary> /// Renumber variables used in astNode. /// </summary> /// <param name="core"></param> /// <param name="astNode"></param> /// <param name="node"></param> /// <param name="numberingMap"></param> /// <param name="renamingMap"></param> /// <param name="inputMap"></param> /// <param name="outputMap"></param> /// <param name="mappedVariables"></param> private static void VariableNumbering( ProtoCore.Core core, AssociativeNode astNode, NodeModel node, Dictionary <string, NumberingState> numberingMap, Dictionary <string, string> renamingMap, Dictionary <string, string> inputMap, Dictionary <string, string> outputMap, HashSet <string> mappedVariables) { Action <IdentifierNode> func = n => { var ident = n.Value; // This ident is from external non-code block node's output // port, or it is from a temporary variable in code block node, // it is not necessary to do renumbering because the name is // unique. if (inputMap.ContainsKey(ident) || /*IsTempVarFromCodeBlockNode(ident, renamingMap)*/ renamingMap.ContainsKey(ident)) { return; } NumberingState ns; if (numberingMap.TryGetValue(ident, out ns)) { // ident already defined somewhere else. So we continue to // bump its ID until numbered variable won't conflict with // all existing variables. if (ns.IsNewSession) { ns.BumpID(); while (mappedVariables.Contains(ns.NumberedVariable)) { ns.BumpID(); } ns.IsNewSession = false; } // ident already defined somewhere else, but we already // renumber this variable, so continue to use re-numbered // one. } else { // It is a new variable. But we need to check if some other // variables are renamed to this one because of renumbering. // If there is confliction, continue to bump its ID. ns = new NumberingState(ident); numberingMap[ident] = ns; while (mappedVariables.Contains(ns.NumberedVariable)) { ns.BumpID(); } } if (ns.ID != 0) { var numberedVar = ns.NumberedVariable; n.Name = n.Value = numberedVar; // If this variable is numbered, and it is also output to // other node, we should remap the output variable to // this re-numbered variable. string name = string.Format("{0}%{1}", ident, node.GUID); if (renamingMap.ContainsKey(name)) { var mappedName = renamingMap[name]; renamingMap[mappedName] = numberedVar; // Record in output map. if (outputMap.ContainsKey(mappedName)) { outputMap[mappedName] = numberedVar; } } } // If this one is not the variable that going to be renamed, then // add to mapped variable set if (!renamingMap.ContainsKey(ns.NumberedVariable)) { mappedVariables.Add(ns.NumberedVariable); } }; IdentifierVisitor visitor = new IdentifierVisitor(func, core); astNode.Accept(visitor); }