示例#1
0
            private NumberingState GetNumberingStateForType(ProtoCore.Type type)
            {
                NumberingState ns;

                if (prefixNumberingMap.TryGetValue(type, out ns))
                {
                    return(ns);
                }

                var shortName = string.Empty;

                if (namingProvider != null)
                {
                    shortName = namingProvider.GetTypeDependentName(type);
                }

                if (!string.IsNullOrEmpty(shortName))
                {
                    ns = new NumberingState(shortName);
                    prefixNumberingMap[type] = ns;
                    return(ns);
                }

                return(defaultNS);
            }
示例#2
0
        /// <summary>
        /// Renumber variables used in astNode.
        /// </summary>
        /// <param name="astNode"></param>
        /// <param name="numberingMap"></param>
        /// <param name="variableMap"></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 other node's output port, it is not necessary to
                // do renumbering.
                if (inputMap.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);
        }