コード例 #1
0
        public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            string result;

            if (arguments.Count == 0)
            {
                if (returnTypes.Contains(CommandResultType.Command))
                {
                    return(new CommandCommandResult(this));
                }
                result = string.Empty;
            }
            else
            {
                var comResult = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String });
                result = ((StringCommandResult)comResult).Content;
            }

            var commandResults = XCommandSystem.FilterList(commands, result).ToArray();

            if (commandResults.Length > 1)
            {
                throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall);
            }
            if (commandResults.Length == 0)
            {
                throw new CommandException("No matching command", CommandExceptionReason.AmbiguousCall);
            }


            var argSubList = arguments.TrySegment(1);

            return(commandResults[0].Value.Execute(info, argSubList, returnTypes));
        }
コード例 #2
0
        public ICommandResult Execute(ExecutionInformation info, string command, IReadOnlyList <CommandResultType> returnTypes)
        {
            var ast = CommandParser.ParseCommandRequest(command);
            var cmd = AstToCommandResult(ast);

            return(cmd.Execute(info, StaticList.Empty <ICommand>(), returnTypes));
        }
コード例 #3
0
        public override ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            if (arguments.Count == 0)
            {
                return(base.Execute(info, arguments, returnTypes));
            }

            var result = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.Command, CommandResultType.String });

            if (result.ResultType == CommandResultType.String)
            {
                // Use cached result so we don't execute the first argument twice
                var passArgs = new ICommand[arguments.Count];
                passArgs[0] = new StringCommand(((StringCommandResult)result).Content);
                arguments.CopyTo(1, passArgs, 1);
                return(base.Execute(info, passArgs, returnTypes));
            }
            return(((CommandCommandResult)result).Command.Execute(info, arguments.TrySegment(1), returnTypes));
        }
コード例 #4
0
        /// <summary>
        /// Try to fit the given arguments to the underlying function.
        /// This function will throw an exception if the parameters can't be applied.
        /// The parameters that are extracted from the arguments will be returned if they can be applied successfully.
        /// </summary>
        /// <param name="info">The ExecutionInformation.</param>
        /// <param name="arguments">The arguments that are applied to this function.</param>
        /// <param name="returnTypes">The possible return types.</param>
        /// <param name="availableArguments">How many arguments could be set.</param>
        public object[] FitArguments(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes, out int availableArguments)
        {
            var parameters = new object[CommandParameter.Length];

            // availableArguments: Iterate through arguments
            // p: Iterate through parameters
            availableArguments = 0;
            for (int p = 0; p < parameters.Length; p++)
            {
                var arg = CommandParameter[p];
                if (arg == typeof(ExecutionInformation))
                {
                    parameters[p] = info;
                }
                else if (arg == typeof(IReadOnlyList <ICommand>))
                {
                    parameters[p] = arguments;
                }
                else if (arg == typeof(IReadOnlyList <CommandResultType>))
                {
                    parameters[p] = returnTypes;
                }
                // Only add arguments if we still have some
                else if (availableArguments < arguments.Count)
                {
                    if (arg.IsArray)                     // array
                    {
                        var typeArr = arg.GetElementType();
                        var args    = Array.CreateInstance(typeArr, arguments.Count - availableArguments);
                        try
                        {
                            for (int i = 0; i < args.Length; i++, availableArguments++)
                            {
                                var argResult  = ((StringCommandResult)arguments[availableArguments].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String })).Content;
                                var convResult = ConvertParam(argResult, typeArr);
                                args.SetValue(convResult, i);
                            }
                        }
                        catch (FormatException ex) { throw new CommandException("Could not convert to " + arg.Name, ex, CommandExceptionReason.CommandError); }
                        catch (OverflowException ex) { throw new CommandException("The number is too big.", ex, CommandExceptionReason.CommandError); }

                        parameters[p] = args;
                    }
                    else                     // primitive value
                    {
                        var argResult = ((StringCommandResult)arguments[availableArguments].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String })).Content;
                        try { parameters[p] = ConvertParam(argResult, arg); }
                        catch (FormatException ex) { throw new CommandException("Could not convert to " + UnwrapType(arg).Name, ex, CommandExceptionReason.CommandError); }
                        catch (OverflowException ex) { throw new CommandException("The number is too big.", ex, CommandExceptionReason.CommandError); }

                        availableArguments++;
                    }
                }
                else
                {
                    parameters[p] = GetDefault(arg);
                }
            }

            // Check if we were able to set enough arguments
            if (availableArguments < Math.Min(parameters.Length, RequiredParameters) && !returnTypes.Contains(CommandResultType.Command))
            {
                throw new CommandException("Not enough arguments for function " + internCommand.Name, CommandExceptionReason.MissingParameter);
            }

            return(parameters);
        }