static public bool ConvertIntoCodeIfCommand(ref string code)
        {
            var tempCode = code;

            // Remove last semicolon.
            tempCode = tempCode.TrimEnd(';');

            var commandLength = tempCode.IndexOf(" ");

            if (commandLength == -1)
            {
                commandLength = tempCode.Length;
            }
            var command = tempCode.Substring(0, commandLength);

            if (!HasRegistered(command))
            {
                return(false);
            }

            // Remove command and get only arguments.
            var argsStr = tempCode.Substring(commandLength);

            // Store parentheses.
            var parentheses = CommandUtil.ConvertParenToPlaceholder(tempCode);

            tempCode = parentheses.output;

            // Store quatation blocks.
            var quates = CommandUtil.ConvertQuateToPlaceholder(tempCode);

            tempCode = quates.output;

            // Split arguments with space.
            var args = argsStr.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);

            // Convert the command into the Class.Method() style.
            tempCode = string.Format("{0}(\"{1}\"{2}{3});",
                                     typeof(RuntimeCommands).FullName + ".Call",
                                     command,
                                     args.Length > 0 ? ", " : "",
                                     string.Join(", ", args));

            // Replace temporary quates placeholders to actual expressions.
            tempCode = CommandUtil.ConvertPlaceholderToBlock(tempCode, quates);

            // Replace temporary parentheses placeholders to actual expressions.
            tempCode = CommandUtil.ConvertPlaceholderToBlock(tempCode, parentheses);

            code = tempCode;

            return(true);
        }
Example #2
0
        static public bool ConvertIntoCodeIfCommand(ref string code)
        {
            // NOTE: If two or more commands that have a same name are registered,
            //       the first one will be used here. It is not correct but works well because
            //       evaluation will be done after expanding the command to the code.

            // To consider commands with spaces, check if the head of the given code is consistent
            // with any command name. command list is ranked in descending order of the command string length.
            var tmpCode     = code;
            var commandInfo = Commands.GetAll().FirstOrDefault(
                x => (tmpCode.Length >= x.command.Length) &&
                (tmpCode.Substring(0, x.command.Length) == x.command));

            // There is no command:
            if (commandInfo == null)
            {
                return(false);
            }

            // Remove last semicolon.
            code = code.TrimEnd(';');

            // Check command format
            if (commandInfo.HasArguments())
            {
                if (code.Substring(0, commandInfo.command.Length) != commandInfo.command)
                {
                    return(false);
                }
            }
            else
            {
                if (code.Length > commandInfo.command.Length)
                {
                    return(false);
                }
            }

            // Remove command and get only arguments.
            code = code.Substring(commandInfo.command.Length);

            // Store parentheses.
            var parentheses = CommandUtil.ConvertParenToPlaceholder(code);

            code = parentheses.output;

            // Store quatation blocks.
            var quates = CommandUtil.ConvertQuateToPlaceholder(code);

            code = quates.output;

            // Split arguments with space.
            var args = code.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);

            // Convert the command into the Class.Method() style.
            code = commandInfo.GetFormat(args);

            // Replace temporary quates placeholders to actual expressions.
            code = CommandUtil.ConvertPlaceholderToBlock(code, quates);

            // Replace temporary parentheses placeholders to actual expressions.
            code = CommandUtil.ConvertPlaceholderToBlock(code, parentheses);

            return(true);
        }