Exemple #1
0
        /// <summary>
        /// parse and evaluate a command line<br/>
        /// 1. parse command line<br/>
        /// error or:<br/>
        /// 2. execute command<br/>
        ///     A. internal command (modules) or alias<br/>
        ///     B. underlying shell command (found in scan paths)<br/>
        ///      file: <br/>
        ///         C. file (batch)<br/>
        ///         D. non executable file<br/>
        ///     not a file:<br/>
        ///         E. unknown command<br/>
        /// </summary>
        /// <param name="context">command evaluation context</param>
        /// <param name="expr">expression to be evaluated</param>
        /// <param name="outputX">begin x location of the command line expression in the console if applyable</param>
        /// <param name="postAnalysisPreExecOutput">text to be outputed before any analysis output</param>
        /// <returns>data returned by the analysis and the evaluation of an expression (analysis error or commmand returns or command error)</returns>
        public ExpressionEvaluationResult Eval(
            CommandEvaluationContext context,
            string expr,
            int outputX = 0,
            string postAnalysisPreExecOutput = null)        // TODO: an eval options object would be nice
        {
            try
            {
                var  pipelineParseResults = ParseCommandLine(context, SyntaxAnalyzer, expr, ExternalParserExtension);
                bool allValid             = true;
                var  evalParses           = new List <ExpressionEvaluationResult>();

                // check pipeline syntax analysis
                foreach (var pipelineParseResult in pipelineParseResults)
                {
                    allValid &= pipelineParseResult.ParseResult.ParseResultType == ParseResultType.Valid;
                    var evalParse = AnalysisPipelineParseResult(
                        context,
                        pipelineParseResult,
                        expr,
                        outputX,
                        pipelineParseResult.ParseResult
                        );

                    evalParses.Add(evalParse);
                }

                // eventually output the post analysis pre exec content
                if (!string.IsNullOrEmpty(postAnalysisPreExecOutput))
                {
                    context.Out.Echo(postAnalysisPreExecOutput);
                }

                if (!allValid)
                {
                    // 💥syntax error in pipeline - break exec
                    var err = evalParses.FirstOrDefault();
                    context.ShellEnv.UpdateVarLastCommandReturn(expr, null, err == null ? ReturnCode.OK : GetReturnCode(err), err?.SyntaxError);
                    return(err);
                }

                // run pipeline
                var evalRes = PipelineProcessor.RunPipeline(context, pipelineParseResults.FirstOrDefault());

                // update shell env
                context.ShellEnv.UpdateVarLastCommandReturn(expr, evalRes.Result, GetReturnCode(evalRes), evalRes.EvalErrorText, evalRes.EvalError);
                return(evalRes);
            }
            catch (Exception pipelineException)
            {
                // code pipeline parse or execution error
                // update shell env
                context.ShellEnv.UpdateVarLastCommandReturn(expr, ReturnCode.Error, ReturnCode.Unknown, pipelineException.Message, pipelineException);
                context.Errorln(pipelineException.Message);
                return(new ExpressionEvaluationResult(expr, null, ParseResultType.NotIdentified, null, (int)ReturnCode.Error, pipelineException, pipelineException.Message));
            }
        }
        /// <summary>
        /// 1. parse command line
        /// 2. execute command or error
        ///     A. internal command (modules)
        ///     B. underlying shell command
        ///     C. unknown command
        /// </summary>
        /// <param name="expr">expression to be evaluated</param>
        /// <returns>return code</returns>
        public ExpressionEvaluationResult Eval(
            CommandEvaluationContext context,
            string expr,
            int outputX)
        {
            var  pipelineParseResults = Parse(context, _syntaxAnalyzer, expr);
            bool allValid             = true;
            var  evalParses           = new List <ExpressionEvaluationResult>();

            foreach (var pipelineParseResult in pipelineParseResults)
            {
                allValid &= pipelineParseResult.ParseResult.ParseResultType == ParseResultType.Valid;
                var evalRes = EvalParse(context, expr, outputX, pipelineParseResult.ParseResult);
                evalParses.Add(evalRes);
            }
            if (!allValid)
            {
                return(evalParses.FirstOrDefault());
            }
            return(PipelineProcessor.RunPipeline(context, pipelineParseResults.FirstOrDefault()));
        }