Allow evaluation from String

Lift the Evaluation code from the test files AbstractParserTestCase and
AbstractEvaluationTestCase into new files EvaluationContext.
Remove this code's dependency on FsApparatus (and thus to InMemoryFS),
by making the Lexer accept null as filename.
Also remove dependency on EventCollectionApparatus;
parameterized by an EventHandler.

Have the SkylarkSignatureProcessor use this Evaluation for defaultValue-s.

While refactoring evaluation, have SkylarkShell use it,
which fixes its ValidationEnvironment issues.

TODO: refactor the tests to use this new infrastructure.

--
MOS_MIGRATED_REVID=90824736
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkShell.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkShell.java
index 6c394f9..96ebf02 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkShell.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkShell.java
@@ -13,18 +13,12 @@
 // limitations under the License.
 package com.google.devtools.build.lib.syntax;
 
-import com.google.common.collect.ImmutableMap;
-import com.google.devtools.build.lib.events.Event;
-import com.google.devtools.build.lib.events.EventHandler;
-import com.google.devtools.build.lib.events.util.EventCollectionApparatus;
-import com.google.devtools.build.lib.packages.CachingPackageLocator;
 import com.google.devtools.build.lib.rules.SkylarkModules;
-import com.google.devtools.build.lib.vfs.Path;
-import com.google.devtools.build.lib.vfs.util.FsApparatus;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.nio.charset.Charset;
 
 /**
  * SkylarkShell is a standalone shell executing Skylark. This is intended for
@@ -33,62 +27,48 @@
  * bugs. Imports and includes are not supported.
  */
 class SkylarkShell {
-  static final EventCollectionApparatus syntaxEvents = new EventCollectionApparatus();
-  static final FsApparatus scratch = FsApparatus.newInMemory();
-  static final CachingPackageLocator locator = new AbstractParserTestCase.EmptyPackageLocator();
-  static final Path path = scratch.path("stdin");
 
-  private static void exec(String inputSource, Environment env) {
+  private static final String START_PROMPT = ">> ";
+  private static final String CONTINUATION_PROMPT = ".. ";
+
+  private final BufferedReader reader = new BufferedReader(
+      new InputStreamReader(System.in, Charset.defaultCharset()));
+  private final EvaluationContext ev =
+      SkylarkModules.newEvaluationContext(EvaluationContext.PRINT_HANDLER);
+
+  public String read() {
+    StringBuilder input = new StringBuilder();
+    ev.print(START_PROMPT);
     try {
-      ParserInputSource input = ParserInputSource.create(inputSource, path);
-      Lexer lexer = new Lexer(input, syntaxEvents.reporter());
-      Parser.ParseResult result =
-          Parser.parseFileForSkylark(lexer, syntaxEvents.reporter(), locator,
-              SkylarkModules.getValidationEnvironment(
-                  ImmutableMap.<String, SkylarkType>of()));
-
-      Object last = null;
-      for (Statement st : result.statements) {
-        if (st instanceof ExpressionStatement) {
-          last = ((ExpressionStatement) st).getExpression().eval(env);
-        } else {
-          st.exec(env);
-          last = null;
+      while (true) {
+        String line = reader.readLine();
+        if (line == null) {
+          return null;
         }
+        if (line.isEmpty()) {
+          return input.toString();
+        }
+        input.append("\n").append(line);
+        ev.print(CONTINUATION_PROMPT);
       }
-      if (last != null) {
-        System.out.println(last);
+    } catch (IOException io) {
+      io.printStackTrace();
+      return null;
+    }
+  }
+
+  public void readEvalPrintLoop() {
+    String input;
+    while ((input = read()) != null) {
+      try {
+        ev.println(EvalUtils.prettyPrintValue(ev.eval(input)));
+      } catch (Exception e) {
+        e.printStackTrace();
       }
-    } catch (Throwable e) { // Catch everything to avoid killing the shell.
-      e.printStackTrace();
     }
   }
 
   public static void main(String[] args) {
-    Environment env = SkylarkModules.getNewEnvironment(new EventHandler() {
-      @Override
-      public void handle(Event event) {
-        System.out.println(event.getMessage());
-      }
-    });
-    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
-    String currentInput = "";
-    String line;
-    System.out.print(">> ");
-    try {
-      while ((line = br.readLine()) != null) {
-        if (line.isEmpty()) {
-          exec(currentInput, env);
-          currentInput = "";
-          System.out.print(">> ");
-        } else {
-          currentInput = currentInput + "\n" + line;
-          System.out.print(".. ");
-        }
-      }
-    } catch (IOException io) {
-      io.printStackTrace();
-    }
+    new SkylarkShell().readEvalPrintLoop();
   }
 }