Exemplo n.º 1
0
        /// <summary>
        /// Adds a block containing a procedure definition to the managed list.  If a procedure
        /// by that name is already defined, creates a new unique name for the procedure and renames the block.
        /// </summary>
        public void AddDefinition(Block block)
        {
            string procedureName = GetProcedureName(block);

            if (string.IsNullOrEmpty(procedureName) || mProcedureDefinitions.ContainsKey(procedureName))
            {
                return;
            }

            procedureName = mNameMgr.GetDistinctName(procedureName);
            SetProcedureName(block, procedureName);
            mProcedureDefinitions.Add(procedureName, block);
            mProcedureCallers.Add(procedureName, new List <Block>());

            FireUpdate(new ProcedureUpdateData(((ProcedureMutator)block.Mutator).ProcedureInfo, block));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Define a function to be included in the generated code.
        /// The first time this is called with a given desiredName,
        /// the code is saved and an actual name is generated.
        /// Subsequent calls with the same desiredName have no effect but have the same return value.
        /// It is up to the caller to make sure the same desiredName is not used for different code values.
        /// The code gets output when Blockly.Generator.finish() is called.
        /// </summary>
        /// <param name="desiredName"></param>
        /// <param name="code"></param>
        /// <returns>The actual name of the new function.  This may differ from desiredName if the former has already been taken by the user.</returns>
        public string ProvideFunction(string desiredName, string code)
        {
            KeyValuePair <string, string> funcPair;

            if (mFuncMap.TryGetValue(desiredName, out funcPair))
            {
                return(funcPair.Key);
            }

            string funcName = mVariableNames.GetDistinctName(desiredName);
            string codeText = code.Replace(FUNCTION_NAME_PLACEHOLDER, funcName);
            // Change all '  ' indents into the desired indent.
            // To avoid an infinite loop of replacements, change all indents to '\0'
            // character first, then replace them all with the indent.
            // We are assuming that no provided functions contain a literal null char.
            string oldCodeText = null;

            while (!codeText.Equals(oldCodeText))
            {
                oldCodeText = codeText;
                Regex regex = new Regex(@"^((  )*)  ");
                codeText = regex.Replace(codeText, "$1\0");
            }
            codeText = codeText.Replace("\0", this.Indent);
            mFuncMap[desiredName] = new KeyValuePair <string, string>(funcName, codeText);
            return(funcName);
        }