diff options
author | Francois-Rene Rideau <tunes@google.com> | 2015-04-13 22:45:18 +0000 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2015-04-14 14:32:29 +0000 |
commit | 36979614627b71826bcbb136c920405a38550e6e (patch) | |
tree | e5ec114fa49207769bed078dde0c8f97655db1be | |
parent | cbebfc7222cf0f1ded7461eac6b75bf6f698dc21 (diff) |
Shrink EvaluationContext a bit
Move some functionality to its only current client SkylarkShell,
mark some accessor @VisibleForTesting and remove those not currently used for testing.
--
MOS_MIGRATED_REVID=91033050
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/EvaluationContext.java | 36 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/SkylarkShell.java | 20 |
2 files changed, 20 insertions, 36 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/EvaluationContext.java b/src/main/java/com/google/devtools/build/lib/syntax/EvaluationContext.java index 8ccca055d5..4966fb8717 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/EvaluationContext.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/EvaluationContext.java @@ -57,13 +57,6 @@ public final class EvaluationContext { } }; - public static final EventHandler PRINT_HANDLER = new EventHandler() { - @Override - public void handle(Event event) { - System.out.print(event.getMessage()); - } - }; - public static EvaluationContext newBuildContext(EventHandler eventHandler, Environment env, boolean parsePython) { return new EvaluationContext(eventHandler, env, null, parsePython); @@ -86,21 +79,14 @@ public final class EvaluationContext { return newSkylarkContext(new SkylarkEnvironment(eventHandler), new ValidationEnvironment()); } + /** Base context for Skylark evaluation for internal use only, while initializing builtins */ + static final EvaluationContext SKYLARK_INITIALIZATION = newSkylarkContext(FAIL_FAST_HANDLER); + + @VisibleForTesting public Environment getEnvironment() { return env; } - public EventHandler getEventHandler() { - return eventHandler; - } - - public ValidationEnvironment getValidationEnvironment() { - return validationEnv; - } - - /** Base context for Skylark evaluation for internal use only, while initializing builtins */ - static final EvaluationContext SKYLARK_INITIALIZATION = newSkylarkContext(FAIL_FAST_HANDLER); - /** Mock package locator */ private static final class EmptyPackageLocator implements CachingPackageLocator { @Override @@ -201,20 +187,6 @@ public final class EvaluationContext { return env.lookup(varname); } - /** Print a String in this context */ - public void print(String msg) { - if (msg != null) { - eventHandler.handle(new Event(EventKind.STDOUT, null, msg)); - } - } - - /** Print a String in this context */ - public void println(String msg) { - if (msg != null) { - print(msg + "\n"); - } - } - /** Evaluate a series of statements */ public Object eval(String... input) throws EvalException, InterruptedException { return eval(parseFile(input)); 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 96ebf02883..f00d8f5ab7 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,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.lib.syntax; +import com.google.devtools.build.lib.events.Event; +import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.rules.SkylarkModules; import java.io.BufferedReader; @@ -31,14 +33,21 @@ class SkylarkShell { private static final String START_PROMPT = ">> "; private static final String CONTINUATION_PROMPT = ".. "; + public static final EventHandler PRINT_HANDLER = new EventHandler() { + @Override + public void handle(Event event) { + System.out.println(event.getMessage()); + } + }; + private final BufferedReader reader = new BufferedReader( new InputStreamReader(System.in, Charset.defaultCharset())); private final EvaluationContext ev = - SkylarkModules.newEvaluationContext(EvaluationContext.PRINT_HANDLER); + SkylarkModules.newEvaluationContext(PRINT_HANDLER); public String read() { StringBuilder input = new StringBuilder(); - ev.print(START_PROMPT); + System.out.print(START_PROMPT); try { while (true) { String line = reader.readLine(); @@ -49,7 +58,7 @@ class SkylarkShell { return input.toString(); } input.append("\n").append(line); - ev.print(CONTINUATION_PROMPT); + System.out.print(CONTINUATION_PROMPT); } } catch (IOException io) { io.printStackTrace(); @@ -61,7 +70,10 @@ class SkylarkShell { String input; while ((input = read()) != null) { try { - ev.println(EvalUtils.prettyPrintValue(ev.eval(input))); + Object result = ev.eval(input); + if (result != null) { + System.out.println(EvalUtils.prettyPrintValue(result)); + } } catch (Exception e) { e.printStackTrace(); } |