internal ICommand AstToCommandResult(AstNode node)
        {
            switch (node.Type)
            {
            case AstType.Error:
                throw new CommandException("Found an unconvertable ASTNode of type Error", CommandExceptionReason.InternalError);

            case AstType.Command:
                var cmd            = (AstCommand)node;
                var arguments      = new ICommand[cmd.Parameter.Count];
                int tailCandidates = 0;
                for (int i = cmd.Parameter.Count - 1; i >= 1; i--)
                {
                    var para = cmd.Parameter[i];
                    if (!(para is AstValue astVal) || astVal.StringType != StringType.FreeString)
                    {
                        break;
                    }

                    arguments[i] = new StringCommand(astVal.Value, astVal.TailString);
                    tailCandidates++;
                }
                for (int i = 0; i < cmd.Parameter.Count - tailCandidates; i++)
                {
                    arguments[i] = AstToCommandResult(cmd.Parameter[i]);
                }
                return(new AppliedCommand(RootCommand, arguments));

            case AstType.Value:
                return(new StringCommand(((AstValue)node).Value));

            default:
                throw Util.UnhandledDefault(node.Type);
            }
        }
Beispiel #2
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));
        }