Exemplo n.º 1
0
        private void RunInteractiveLoop(ScriptOptions options, string initialScriptCodeOpt, CancellationToken cancellationToken)
        {
            var globals = new InteractiveScriptGlobals(_console.Out, _objectFormatter);
            globals.Args.AddRange(_compiler.Arguments.ScriptArguments);

            ScriptState<object> state = null;

            if (initialScriptCodeOpt != null)
            {
                var script = Script.CreateInitialScript<object>(_scriptCompiler, initialScriptCodeOpt, options, globals.GetType(), assemblyLoaderOpt: null);

                Compilation compilation;
                TryBuildAndRun(script, globals, ref state, out compilation, cancellationToken);
            }

            while (true)
            {
                _console.Out.Write("> ");
                var input = new StringBuilder();
                string line;
                bool cancelSubmission = false;

                while (true)
                {
                    line = _console.In.ReadLine();
                    if (line == null)
                    {
                        if (input.Length == 0)
                        {
                            return;
                        }

                        cancelSubmission = true;
                        break;
                    }

                    input.AppendLine(line);

                    var tree = _scriptCompiler.ParseSubmission(SourceText.From(input.ToString()), cancellationToken);
                    if (_scriptCompiler.IsCompleteSubmission(tree))
                    {
                        break;
                    }

                    _console.Out.Write(". ");
                }

                if (cancelSubmission)
                {
                    continue;
                }

                string code = input.ToString();

                if (IsHelpCommand(code))
                {
                    DisplayHelpText();
                    continue;
                }

                Script<object> newScript;
                if (state == null)
                {
                    newScript = Script.CreateInitialScript<object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null);
                }
                else
                {
                    newScript = state.Script.ContinueWith(code, options);
                }

                Compilation newCompilation;
                if (!TryBuildAndRun(newScript, globals, ref state, out newCompilation, cancellationToken))
                {
                    continue;
                }

                bool hasValue;
                ITypeSymbol resultType = newCompilation.GetSubmissionResultType(out hasValue);
                if (hasValue)
                {
                    if (resultType != null && resultType.SpecialType == SpecialType.System_Void)
                    {
                        _console.Out.WriteLine(_objectFormatter.VoidDisplayString);
                    }
                    else
                    {
                        globals.Print(state.ReturnValue);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static ScriptOptions UpdateOptions(ScriptOptions options, InteractiveScriptGlobals globals)
        {
            var currentMetadataResolver = (RuntimeMetadataReferenceResolver)options.MetadataResolver;
            var currentSourceResolver = (CommonCompiler.LoggingSourceFileResolver)options.SourceResolver;

            string newWorkingDirectory = Directory.GetCurrentDirectory();
            var newReferenceSearchPaths = ImmutableArray.CreateRange(globals.ReferencePaths);
            var newSourceSearchPaths = ImmutableArray.CreateRange(globals.SourcePaths);

            // remove references and imports from the options, they have been applied and will be inherited from now on:
            return options.
                RemoveImportsAndReferences().
                WithMetadataResolver(currentMetadataResolver.
                    WithRelativePathResolver(
                        currentMetadataResolver.PathResolver.
                            WithBaseDirectory(newWorkingDirectory).
                            WithSearchPaths(newReferenceSearchPaths))).
                WithSourceResolver(currentSourceResolver.
                        WithBaseDirectory(newWorkingDirectory).
                        WithSearchPaths(newSourceSearchPaths));
        }
Exemplo n.º 3
0
            public void Initialize(Type replServiceProviderType, string cultureName)
            {
                Debug.Assert(replServiceProviderType != null);
                Debug.Assert(cultureName != null);

                Debug.Assert(_metadataFileProvider == null);
                Debug.Assert(_assemblyLoader == null);
                Debug.Assert(_replServiceProvider == null);

                // TODO (tomat): we should share the copied files with the host
                _metadataFileProvider = new MetadataShadowCopyProvider(
                    Path.Combine(Path.GetTempPath(), "InteractiveHostShadow"),
                    noShadowCopyDirectories: s_systemNoShadowCopyDirectories,
                    documentationCommentsCulture: new CultureInfo(cultureName));

                _assemblyLoader = new InteractiveAssemblyLoader(_metadataFileProvider);

                _replServiceProvider = (ReplServiceProvider)Activator.CreateInstance(replServiceProviderType);

                _globals = new InteractiveScriptGlobals(Console.Out, _replServiceProvider.ObjectFormatter);
            }
Exemplo n.º 4
0
        private bool TryBuildAndRun(Script<object> newScript, InteractiveScriptGlobals globals, ref ScriptState<object> state, ref ScriptOptions options, CancellationToken cancellationToken)
        {
            var diagnostics = newScript.Compile(cancellationToken);
            DisplayDiagnostics(diagnostics);
            if (diagnostics.HasAnyErrors())
            {
                return false;
            }

            try
            {
                var task = (state == null) ?
                    newScript.RunAsync(globals, cancellationToken) :
                    newScript.ContinueAsync(state, cancellationToken);

                state = task.GetAwaiter().GetResult();
            }
            catch (FileLoadException e) when (e.InnerException is InteractiveAssemblyLoaderException)
            {
                var oldColor = _console.ForegroundColor;
                try
                {
                    _console.ForegroundColor = ConsoleColor.Red;
                    _console.Out.WriteLine(e.InnerException.Message);
                }
                finally
                {
                    _console.ForegroundColor = oldColor;
                }

                return false;
            }
            catch (Exception e)
            {
                DisplayException(e);
                return false;
            }

            options = UpdateOptions(options, globals);

            return true;
        }
Exemplo n.º 5
0
        private void BuildAndRun(Script<object> newScript, InteractiveScriptGlobals globals, ref ScriptState<object> state, ref ScriptOptions options, bool displayResult, CancellationToken cancellationToken)
        {
            var diagnostics = newScript.Compile(cancellationToken);
            DisplayDiagnostics(diagnostics);
            if (diagnostics.HasAnyErrors())
            {
                return;
            }

            var task = (state == null) ?
                newScript.RunAsync(globals, catchException: e => true, cancellationToken: cancellationToken) :
                newScript.RunFromAsync(state, catchException: e => true, cancellationToken: cancellationToken);

            state = task.GetAwaiter().GetResult();
            if (state.Exception != null)
            {
                DisplayException(state.Exception);
            }
            else if (displayResult && newScript.HasReturnValue())
            {
                globals.Print(state.ReturnValue);
            }

            options = UpdateOptions(options, globals);
        }
Exemplo n.º 6
0
        private void RunInteractiveLoop(ScriptOptions options, CancellationToken cancellationToken)
        {
            var globals = new InteractiveScriptGlobals(_console.Out, _objectFormatter);
            globals.Args.AddRange(_compiler.Arguments.ScriptArguments);

            ScriptState <object> state = null;

            while (true)
            {
                _console.Out.Write("> ");
                var input = new StringBuilder();
                string line;
                bool cancelSubmission = false;

                while (true)
                {
                    line = _console.In.ReadLine();
                    if (line == null)
                    {
                        if (input.Length == 0)
                        {
                            return;
                        }

                        cancelSubmission = true;
                        break;
                    }

                    input.AppendLine(line);

                    var tree = _scriptCompiler.ParseSubmission(SourceText.From(input.ToString()), cancellationToken);
                    if (_scriptCompiler.IsCompleteSubmission(tree))
                    {
                        break;
                    }

                    _console.Out.Write(". ");
                }

                if (cancelSubmission)
                {
                    continue;
                }

                string code = input.ToString();

                Script<object> newScript;
                if (state == null)
                {
                    newScript = Script.CreateInitialScript<object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null);
                }
                else
                {
                    newScript = state.Script.ContinueWith(code, options);
                }

                var newCompilation = newScript.GetCompilation();

                try
                {
                    newScript.Build(cancellationToken);

                    // display warnings:
                    DisplayDiagnostics(newCompilation.GetDiagnostics(cancellationToken).Where(d => d.Severity == DiagnosticSeverity.Warning));
                }
                catch (CompilationErrorException e)
                {
                    DisplayDiagnostics(e.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error || d.Severity == DiagnosticSeverity.Warning));
                    continue;
                }

                try
                {
                    var task = (state == null) ?
                        newScript.RunAsync(globals, cancellationToken) :
                        newScript.ContinueAsync(state, cancellationToken);

                    state = task.GetAwaiter().GetResult();
                }
                catch (Exception e)
                {
                    DisplayException(e);
                    continue;
                }

                bool hasValue;
                ITypeSymbol resultType = newCompilation.GetSubmissionResultType(out hasValue);
                if (hasValue)
                {
                    if (resultType != null && resultType.SpecialType == SpecialType.System_Void)
                    {
                        _console.Out.WriteLine(_objectFormatter.VoidDisplayString);
                    }
                    else
                    {
                        globals.Print(state.ReturnValue);
                    }
                }
            }
        }
Exemplo n.º 7
0
        private void RunInteractiveLoop(ScriptOptions options, string initialScriptCodeOpt, CancellationToken cancellationToken)
        {
            var globals = new InteractiveScriptGlobals(_console.Out, _objectFormatter);

            globals.Args.AddRange(_compiler.Arguments.ScriptArguments);

            ScriptState <object> state = null;

            if (initialScriptCodeOpt != null)
            {
                var script = Script.CreateInitialScript <object>(_scriptCompiler, initialScriptCodeOpt, options, globals.GetType(), assemblyLoaderOpt: null);
                TryBuildAndRun(script, globals, ref state, ref options, cancellationToken);
            }

            while (true)
            {
                _console.Out.Write("> ");
                var    input = new StringBuilder();
                string line;
                bool   cancelSubmission = false;

                while (true)
                {
                    line = _console.In.ReadLine();
                    if (line == null)
                    {
                        if (input.Length == 0)
                        {
                            return;
                        }

                        cancelSubmission = true;
                        break;
                    }

                    input.AppendLine(line);

                    var tree = _scriptCompiler.ParseSubmission(SourceText.From(input.ToString()), cancellationToken);
                    if (_scriptCompiler.IsCompleteSubmission(tree))
                    {
                        break;
                    }

                    _console.Out.Write(". ");
                }

                if (cancelSubmission)
                {
                    continue;
                }

                string code = input.ToString();

                if (IsHelpCommand(code))
                {
                    DisplayHelpText();
                    continue;
                }

                Script <object> newScript;
                if (state == null)
                {
                    newScript = Script.CreateInitialScript <object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null);
                }
                else
                {
                    newScript = state.Script.ContinueWith(code, options);
                }

                if (!TryBuildAndRun(newScript, globals, ref state, ref options, cancellationToken))
                {
                    continue;
                }

                if (newScript.HasReturnValue())
                {
                    globals.Print(state.ReturnValue);
                }
            }
        }
Exemplo n.º 8
0
        private void RunInteractiveLoop(ScriptOptions options, CancellationToken cancellationToken)
        {
            var globals = new InteractiveScriptGlobals(_console.Out, _objectFormatter);

            globals.Args.AddRange(_compiler.Arguments.ScriptArguments);

            ScriptState <object> state = null;

            while (true)
            {
                _console.Out.Write("> ");
                var    input = new StringBuilder();
                string line;
                bool   cancelSubmission = false;

                while (true)
                {
                    line = _console.In.ReadLine();
                    if (line == null)
                    {
                        if (input.Length == 0)
                        {
                            return;
                        }

                        cancelSubmission = true;
                        break;
                    }

                    input.AppendLine(line);

                    var tree = _scriptCompiler.ParseSubmission(SourceText.From(input.ToString()), cancellationToken);
                    if (_scriptCompiler.IsCompleteSubmission(tree))
                    {
                        break;
                    }

                    _console.Out.Write(". ");
                }

                if (cancelSubmission)
                {
                    continue;
                }

                string code = input.ToString();

                Script <object> newScript;
                if (state == null)
                {
                    newScript = Script.CreateInitialScript <object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null);
                }
                else
                {
                    newScript = state.Script.ContinueWith(code, options);
                }

                var newCompilation = newScript.GetCompilation();

                try
                {
                    newScript.Build(cancellationToken);

                    // display warnings:
                    DisplayDiagnostics(newCompilation.GetDiagnostics(cancellationToken).Where(d => d.Severity == DiagnosticSeverity.Warning));
                }
                catch (CompilationErrorException e)
                {
                    DisplayDiagnostics(e.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error || d.Severity == DiagnosticSeverity.Warning));
                    continue;
                }

                try
                {
                    var task = (state == null) ?
                               newScript.RunAsync(globals, cancellationToken) :
                               newScript.ContinueAsync(state, cancellationToken);

                    state = task.GetAwaiter().GetResult();
                }
                catch (Exception e)
                {
                    DisplayException(e);
                    continue;
                }

                bool        hasValue;
                ITypeSymbol resultType = newCompilation.GetSubmissionResultType(out hasValue);
                if (hasValue)
                {
                    if (resultType != null && resultType.SpecialType == SpecialType.System_Void)
                    {
                        _console.Out.WriteLine(_objectFormatter.VoidDisplayString);
                    }
                    else
                    {
                        globals.Print(state.ReturnValue);
                    }
                }
            }
        }