@Nullable
  private ScriptDescriptor doAnalyze(
      @NotNull JetFile psiFile, @NotNull ReplMessageCollectorWrapper messageCollector) {
    scriptDeclarationFactory.setDelegateFactory(
        new FileBasedDeclarationProviderFactory(
            resolveSession.getStorageManager(), Collections.singletonList(psiFile)));

    TopDownAnalysisContext context =
        topDownAnalyzer.analyzeDeclarations(
            topDownAnalysisContext.getTopDownAnalysisMode(), Collections.singletonList(psiFile));

    if (trace.get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, psiFile) == null) {
      trace.record(
          BindingContext.FILE_TO_PACKAGE_FRAGMENT,
          psiFile,
          resolveSession.getPackageFragment(FqName.ROOT));
    }

    boolean hasErrors =
        AnalyzerWithCompilerReport.reportDiagnostics(
            trace.getBindingContext().getDiagnostics(), messageCollector.getMessageCollector());
    if (hasErrors) {
      return null;
    }

    ScriptDescriptor scriptDescriptor = context.getScripts().get(psiFile.getScript());
    lastLineScope = trace.get(BindingContext.SCRIPT_SCOPE, scriptDescriptor);
    if (lastLineScope == null) {
      throw new IllegalStateException("last line scope is not initialized");
    }

    return scriptDescriptor;
  }
 private static void prepareForTheNextReplLine(@NotNull TopDownAnalysisContext c) {
   c.getScripts().clear();
 }