Example #1
0
        /// <summary>
        /// csi.exe and vbi.exe entry point.
        /// </summary>
        private int RunInteractiveCore(ErrorLogger errorLogger)
        {
            Debug.Assert(_compiler.Arguments.IsInteractive);

            var sourceFiles = _compiler.Arguments.SourceFiles;

            if (sourceFiles.IsEmpty && _compiler.Arguments.DisplayLogo)
            {
                _compiler.PrintLogo(_console.Out);
            }

            if (_compiler.Arguments.DisplayHelp)
            {
                _compiler.PrintHelp(_console.Out);
                return(0);
            }

            SourceText code = null;
            IEnumerable <Diagnostic> errors = _compiler.Arguments.Errors;

            if (!sourceFiles.IsEmpty)
            {
                if (sourceFiles.Length > 1 || !sourceFiles[0].IsScript)
                {
                    errors = errors.Concat(new[] { Diagnostic.Create(_compiler.MessageProvider, _compiler.MessageProvider.ERR_ExpectedSingleScript) });
                }
                else
                {
                    var diagnostics = new List <DiagnosticInfo>();
                    code   = _compiler.ReadFileContent(sourceFiles[0], diagnostics);
                    errors = errors.Concat(diagnostics.Select(Diagnostic.Create));
                }
            }

            if (_compiler.ReportErrors(errors, _console.Out, errorLogger))
            {
                return(CommonCompiler.Failed);
            }

            var cancellationToken = new CancellationToken();
            var hostObject        = new CommandLineHostObject(_console.Out, _objectFormatter, cancellationToken);

            hostObject.Args = _compiler.Arguments.ScriptArguments.ToArray();

            var scriptOptions = GetScriptOptions(_compiler.Arguments);

            if (sourceFiles.IsEmpty)
            {
                RunInteractiveLoop(scriptOptions, hostObject);
            }
            else
            {
                RunScript(scriptOptions, code.ToString(), sourceFiles[0].Path, hostObject, errorLogger);
            }

            return(hostObject.ExitCode);
        }
Example #2
0
        private void RunInteractiveLoop(ScriptOptions options, CommandLineHostObject globals)
        {
            ScriptState <object> state = null;
            var cancellationToken      = globals.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();

                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);
                    }
                }
            }
        }
Example #3
0
        private void RunScript(ScriptOptions options, string code, string scriptPath, CommandLineHostObject globals, ErrorLogger errorLogger)
        {
            options = options.WithPath(scriptPath).WithIsInteractive(false);
            var script = Script.CreateInitialScript <object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null);

            try
            {
                script.RunAsync(globals, globals.CancellationToken).Wait();
            }
            catch (CompilationErrorException e)
            {
                _compiler.ReportErrors(e.Diagnostics, _console.Out, errorLogger);
                globals.ExitCode = CommonCompiler.Failed;
            }
        }
Example #4
0
        /// <summary>
        /// csi.exe and vbi.exe entry point.
        /// </summary>
        private int RunInteractiveCore(ErrorLogger errorLogger)
        {
            Debug.Assert(_compiler.Arguments.IsInteractive);

            var sourceFiles = _compiler.Arguments.SourceFiles;

            if (sourceFiles.IsEmpty && _compiler.Arguments.DisplayLogo)
            {
                _compiler.PrintLogo(_console.Out);
            }

            if (_compiler.Arguments.DisplayHelp)
            {
                _compiler.PrintHelp(_console.Out);
                return 0;
            }

            SourceText code = null;
            IEnumerable<Diagnostic> errors = _compiler.Arguments.Errors;
            if (!sourceFiles.IsEmpty)
            {
                if (sourceFiles.Length > 1 || !sourceFiles[0].IsScript)
                {
                    errors = errors.Concat(new[] { Diagnostic.Create(_compiler.MessageProvider, _compiler.MessageProvider.ERR_ExpectedSingleScript) });
                }
                else
                {
                    var diagnostics = new List<DiagnosticInfo>();
                    code = _compiler.ReadFileContent(sourceFiles[0], diagnostics);
                    errors = errors.Concat(diagnostics.Select(Diagnostic.Create));
                }
            }

            if (_compiler.ReportErrors(errors, _console.Out, errorLogger))
            {
                return CommonCompiler.Failed;
            }

            var cancellationToken = new CancellationToken();
            var hostObject = new CommandLineHostObject(_console.Out, _objectFormatter, cancellationToken);
            hostObject.Args = _compiler.Arguments.ScriptArguments.ToArray();

            var scriptOptions = GetScriptOptions(_compiler.Arguments);

            if (sourceFiles.IsEmpty)
            {
                RunInteractiveLoop(scriptOptions, hostObject);
            }
            else
            {
                RunScript(scriptOptions, code.ToString(), sourceFiles[0].Path, hostObject, errorLogger);
            }

            return hostObject.ExitCode;
        }
Example #5
0
        private void RunInteractiveLoop(ScriptOptions options, CommandLineHostObject globals)
        {
            ScriptState<object> state = null;
            var cancellationToken = globals.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();

                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);
                    }
                }
            }
        }
Example #6
0
 private void RunScript(ScriptOptions options, string code, string scriptPath, CommandLineHostObject globals, ErrorLogger errorLogger)
 {
     options = options.WithPath(scriptPath).WithIsInteractive(false);
     var script = Script.CreateInitialScript<object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null);
     try
     {
         script.RunAsync(globals, globals.CancellationToken).Wait();
     }
     catch (CompilationErrorException e)
     {
         _compiler.ReportErrors(e.Diagnostics, _console.Out, errorLogger);
         globals.ExitCode = CommonCompiler.Failed;
     }
 }