Skip to content

Commit

Permalink
add -c flag for Starlark-in-java
Browse files Browse the repository at this point in the history
    **Related issues:**
    - add -c flag [#35](bazelbuild/starlark#35)
    - improve starlark-in-java installation documentation [#24](bazelbuild/starlark#24)

    **Examples:**
    $ bazel run :Starlark -- -c "print(1)"
    1
    $ bazel run :Starlark -- -c "print(1)" arg3
    USAGE: Starlark [-c "\<cmdLineProgram\>" | \<fileName\>]

    Closes #7683.

    PiperOrigin-RevId: 239028987
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent e176f8c commit 42997dc
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.syntax.BuildFileAST;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
import com.google.devtools.build.lib.syntax.Mutability;
import com.google.devtools.build.lib.syntax.ParserInput;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -55,10 +54,10 @@ public void handle(Event event) {
private final BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in, CHARSET));
private final Mutability mutability = Mutability.create("interpreter");
private final StarlarkThread thread =
StarlarkThread.builder(mutability)
private final Environment env =
Environment.builder(mutability)
.useDefaultSemantics()
.setGlobals(StarlarkThread.DEFAULT_GLOBALS)
.setGlobals(Environment.DEFAULT_GLOBALS)
.setEventHandler(PRINT_HANDLER)
.build();

Expand Down Expand Up @@ -86,12 +85,11 @@ private String prompt() {
}

/** Provide a REPL evaluating Starlark code. */
@SuppressWarnings("CatchAndPrintStackTrace")
public void readEvalPrintLoop() {
String line;
while ((line = prompt()) != null) {
String input;
while ((input = prompt()) != null) {
try {
Object result = EvalUtils.execOrEval(ParserInput.fromLines(line), thread);
Object result = BuildFileAST.eval(env, input);
if (result != null) {
System.out.println(Printer.repr(result));
}
Expand All @@ -116,8 +114,7 @@ public int executeFile(String path) {
/** Execute a Starlark command. */
public int execute(String content) {
try {
ParserInput input = ParserInput.create(content, null);
EvalUtils.execOrEval(input, thread);
BuildFileAST.eval(env, content);
return 0;
} catch (EvalException e) {
System.err.println(e.print());
Expand Down

0 comments on commit 42997dc

Please sign in to comment.