コード例 #1
0
        private static IExecutionCommand Parse(List <Token> tokens, uint start, uint end)
        {
            IParseElement element = Parser.Parse(tokens, start, end, tokenInfo, groupInfo);

            bool isGroup;
            IExecutionCommand command = Parse(true, element, out isGroup);

            if (isGroup)
            {
                return(new MethodCallCommand(command, new IExecutionCommand[0]));
            }
            return(command);
        }
コード例 #2
0
        bool ParseLine(string line)
        {
            if (line.Trim().ToLower() == "exit")
            {
                return(false);
            }

            // We extract command.
            IExecutionCommand command = null;

            try
            {
                command = ExecutionCommandParser.Parse(line);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("Parsing failed: {0}", ex);
            }

            if (command == null)
            {
                return(true);
            }

            try
            {
                ExecutionContext context = new ExecutionContext();
                context.Error    = Console.Error;
                context.Input    = Console.In;
                context.Output   = Console.Out;
                context.ShellApp = this;

                object[] dummy;
                int      r = command.Exec(context, out dummy);
                if (r < 0)
                {
                    context.Error.WriteLine("Executing command failed with result {0}.", r);
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("Execution failed: {0}", ex);
            }


            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Creates a context for sub-command execution.
        /// </summary>
        internal ExecutionContext CreateSubContext(ExecutionContext context, IExecutionCommand command)
        {
            // Optimization.
            if (command is ConstantCommand)
            {
                return(context);
            }

            ExecutionContext newContext = context.Clone();

            newContext.Input      = silentConsole.In;
            newContext.Output     = silentConsole.Out;
            newContext.Error      = silentConsole.Error;
            newContext.IsParallel = false;

            return(newContext);
        }
コード例 #4
0
 /// <summary>
 /// Method call parameters.
 /// </summary>
 /// <param name="methodName"></param>
 /// <param name="parameters"></param>
 public MethodCallCommand(IExecutionCommand methodName, IExecutionCommand[] parameters)
 {
     this.methodName = methodName;
     this.parameters = parameters;
 }
コード例 #5
0
 public ParallelCommand(IExecutionCommand commands1, IExecutionCommand commands2)
 {
     this.commands1 = commands1;
     this.commands2 = commands2;
 }
コード例 #6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="first"></param>
 /// <param name="second"></param>
 public AndCommand(IExecutionCommand first, IExecutionCommand second)
 {
     this.first  = first;
     this.second = second;
 }
コード例 #7
0
ファイル: PipeCommand.cs プロジェクト: zigaosolin/SharpMedia
 /// <summary>
 /// Pipes.
 /// </summary>
 /// <param name="inCommand"></param>
 /// <param name="outCommand"></param>
 public PipeCommand(IExecutionCommand inCommand, IExecutionCommand outCommand)
 {
     this.inCommand  = inCommand;
     this.outCommand = outCommand;
 }
コード例 #8
0
        static IExecutionCommand Parse(bool executable, IParseElement element, out bool isGroup)
        {
            isGroup = false;

            if (element is Group)
            {
                Group group = (Group)element;

                if (group.GroupInfo == internalExpression)
                {
                    isGroup = true;
                    return(Parse(true, group.InternalElement));
                }
                else if (group.GroupInfo == groupExpression)
                {
                    isGroup = true;
                    // We skip group expression.
                    return(Parse(true, group.InternalElement));
                }
            }
            else if (element is Identifier)
            {
                Identifier id = (Identifier)element;

                if (!executable)
                {
                    return(new ConstantCommand(id.Token.TokenValue));
                }
                else
                {
                    return(new MethodCallCommand(new ConstantCommand(id.Token.TokenValue), new IExecutionCommand[0]));
                }
            }
            else if (element is SplitToken)
            {
                SplitToken token = (SplitToken)element;

                if (token.TokenInfo == logicalAnd)
                {
                    return(new AndCommand(Parse(true, token.LeftElement), Parse(true, token.RightElement)));
                }
                else if (token.TokenInfo == logicalOr)
                {
                    return(new OrCommand(Parse(true, token.LeftElement), Parse(true, token.RightElement)));
                }
                else if (token.TokenInfo == parallel)
                {
                    return(new ParallelCommand(Parse(true, token.LeftElement), Parse(true, token.RightElement)));
                }
                else if (token.TokenInfo == pipe)
                {
                    return(new PipeCommand(Parse(true, token.LeftElement), Parse(true, token.RightElement)));
                }
            }
            else if (element is ElementSequence)
            {
                ElementSequence token = (ElementSequence)element;

                // We have a method call.
                IExecutionCommand        methodName = Parse(false, token.Elements[0]);
                List <IExecutionCommand> parameters = new List <IExecutionCommand>();
                for (int i = 1; i < token.Elements.Length; i++)
                {
                    parameters.Add(Parse(false, token.Elements[i]));
                }

                return(new MethodCallCommand(methodName, parameters.ToArray()));
            }

            throw new Exception("Could not parse command.");
        }