예제 #1
0
        protected KernelBase()
        {
            _disposables = new CompositeDisposable();

            Pipeline = new KernelCommandPipeline(this);

            AddSetKernelMiddleware();

            AddDirectiveMiddleware();
        }
예제 #2
0
파일: KernelBase.cs 프로젝트: godsayyou/try
        protected KernelBase()
        {
            _disposables = new CompositeDisposable();

            Pipeline = new KernelCommandPipeline(this);

            Pipeline.AddMiddleware(async(command, pipelineContext, next) =>
            {
                switch (command)
                {
                case SubmitCode submitCode:

                    var modified = false;

                    var directiveParser = BuildDirectiveParser(pipelineContext);

                    var lines = new Queue <string>(
                        submitCode.Code.Split(new[] { "\r\n", "\n" },
                                              StringSplitOptions.None));

                    var unhandledLines = new List <string>();

                    while (lines.Count > 0)
                    {
                        var currentLine = lines.Dequeue();

                        var parseResult = directiveParser.Parse(currentLine);

                        if (parseResult.Errors.Count == 0)
                        {
                            modified = true;
                            await directiveParser.InvokeAsync(parseResult);
                        }
                        else
                        {
                            unhandledLines.Add(currentLine);
                        }
                    }

                    var code = string.Join("\n", unhandledLines);

                    if (modified)
                    {
                        if (string.IsNullOrWhiteSpace(code))
                        {
                            pipelineContext.OnExecute(context =>
                            {
                                context.OnNext(new CodeSubmissionEvaluated(submitCode));
                                context.OnCompleted();
                                return(Task.CompletedTask);
                            });

                            return;
                        }
                        else
                        {
                            submitCode.Code = code;
                        }
                    }

                    break;
                }

                await next(command, pipelineContext);
            });
        }