alandonovan | 5c7d5aa | 2018-12-03 17:05:15 -0500 | [diff] [blame] | 1 | // Package repl provides a read/eval/print loop for Starlark. |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 2 | // |
| 3 | // It supports readline-style command editing, |
| 4 | // and interrupts through Control-C. |
| 5 | // |
| 6 | // If an input line can be parsed as an expression, |
| 7 | // the REPL parses and evaluates it and prints its result. |
| 8 | // Otherwise the REPL reads lines until a blank line, |
| 9 | // then tries again to parse the multi-line input as an |
| 10 | // expression. If the input still cannot be parsed as an expression, |
| 11 | // the REPL parses and executes it as a file (a list of statements), |
| 12 | // for side effects. |
Alan Donovan | 551f300 | 2018-11-01 09:44:00 -0400 | [diff] [blame] | 13 | package repl // import "go.starlark.net/repl" |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 14 | |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 15 | import ( |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 16 | "context" |
| 17 | "fmt" |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 18 | "io" |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 19 | "os" |
| 20 | "os/signal" |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 21 | |
| 22 | "github.com/chzyer/readline" |
alandonovan | 77c1099 | 2019-07-11 17:30:17 -0400 | [diff] [blame] | 23 | "go.starlark.net/resolve" |
Alan Donovan | 6beab7e | 2018-10-31 17:53:09 -0400 | [diff] [blame] | 24 | "go.starlark.net/starlark" |
| 25 | "go.starlark.net/syntax" |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var interrupted = make(chan os.Signal, 1) |
| 29 | |
| 30 | // REPL executes a read, eval, print loop. |
| 31 | // |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 32 | // Before evaluating each expression, it sets the Starlark thread local |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 33 | // variable named "context" to a context.Context that is cancelled by a |
| 34 | // SIGINT (Control-C). Client-supplied global functions may use this |
| 35 | // context to make long-running operations interruptable. |
| 36 | // |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 37 | func REPL(thread *starlark.Thread, globals starlark.StringDict) { |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 38 | signal.Notify(interrupted, os.Interrupt) |
| 39 | defer signal.Stop(interrupted) |
| 40 | |
| 41 | rl, err := readline.New(">>> ") |
| 42 | if err != nil { |
| 43 | PrintError(err) |
| 44 | return |
| 45 | } |
| 46 | defer rl.Close() |
| 47 | for { |
| 48 | if err := rep(rl, thread, globals); err != nil { |
| 49 | if err == readline.ErrInterrupt { |
| 50 | fmt.Println(err) |
| 51 | continue |
| 52 | } |
| 53 | break |
| 54 | } |
| 55 | } |
| 56 | fmt.Println() |
| 57 | } |
| 58 | |
| 59 | // rep reads, evaluates, and prints one item. |
| 60 | // |
| 61 | // It returns an error (possibly readline.ErrInterrupt) |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 62 | // only if readline failed. Starlark errors are printed. |
| 63 | func rep(rl *readline.Instance, thread *starlark.Thread, globals starlark.StringDict) error { |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 64 | // Each item gets its own context, |
| 65 | // which is cancelled by a SIGINT. |
| 66 | // |
| 67 | // Note: during Readline calls, Control-C causes Readline to return |
| 68 | // ErrInterrupt but does not generate a SIGINT. |
| 69 | ctx, cancel := context.WithCancel(context.Background()) |
| 70 | defer cancel() |
| 71 | go func() { |
| 72 | select { |
| 73 | case <-interrupted: |
| 74 | cancel() |
| 75 | case <-ctx.Done(): |
| 76 | } |
| 77 | }() |
| 78 | |
| 79 | thread.SetLocal("context", ctx) |
| 80 | |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 81 | eof := false |
| 82 | |
| 83 | // readline returns EOF, ErrInterrupted, or a line including "\n". |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 84 | rl.SetPrompt(">>> ") |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 85 | readline := func() ([]byte, error) { |
| 86 | line, err := rl.Readline() |
| 87 | rl.SetPrompt("... ") |
| 88 | if err != nil { |
| 89 | if err == io.EOF { |
| 90 | eof = true |
| 91 | } |
| 92 | return nil, err |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 93 | } |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 94 | return []byte(line + "\n"), nil |
| 95 | } |
| 96 | |
| 97 | // parse |
| 98 | f, err := syntax.ParseCompoundStmt("<stdin>", readline) |
| 99 | if err != nil { |
| 100 | if eof { |
| 101 | return io.EOF |
| 102 | } |
| 103 | PrintError(err) |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 104 | return nil |
| 105 | } |
| 106 | |
alandonovan | 77c1099 | 2019-07-11 17:30:17 -0400 | [diff] [blame] | 107 | // Treat load bindings as global (like they used to be) in the REPL. |
| 108 | // This is a workaround for github.com/google/starlark-go/issues/224. |
| 109 | // TODO(adonovan): not safe wrt concurrent interpreters. |
| 110 | // Come up with a more principled solution (or plumb options everywhere). |
| 111 | defer func(prev bool) { resolve.LoadBindsGlobally = prev }(resolve.LoadBindsGlobally) |
| 112 | resolve.LoadBindsGlobally = true |
| 113 | |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 114 | if expr := soleExpr(f); expr != nil { |
| 115 | // eval |
| 116 | v, err := starlark.EvalExpr(thread, expr, globals) |
| 117 | if err != nil { |
| 118 | PrintError(err) |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 119 | return nil |
| 120 | } |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 121 | |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 122 | // print |
| 123 | if v != starlark.None { |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 124 | fmt.Println(v) |
| 125 | } |
alandonovan | 28350e6 | 2019-10-21 14:58:36 -0400 | [diff] [blame] | 126 | } else if err := starlark.ExecREPLChunk(f, thread, globals); err != nil { |
| 127 | PrintError(err) |
| 128 | return nil |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return nil |
| 132 | } |
| 133 | |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 134 | func soleExpr(f *syntax.File) syntax.Expr { |
| 135 | if len(f.Stmts) == 1 { |
| 136 | if stmt, ok := f.Stmts[0].(*syntax.ExprStmt); ok { |
| 137 | return stmt.X |
| 138 | } |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 139 | } |
alandonovan | 30e71c6 | 2019-01-04 13:48:12 -0500 | [diff] [blame] | 140 | return nil |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // PrintError prints the error to stderr, |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 144 | // or its backtrace if it is a Starlark evaluation error. |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 145 | func PrintError(err error) { |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 146 | if evalErr, ok := err.(*starlark.EvalError); ok { |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 147 | fmt.Fprintln(os.Stderr, evalErr.Backtrace()) |
| 148 | } else { |
| 149 | fmt.Fprintln(os.Stderr, err) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // MakeLoad returns a simple sequential implementation of module loading |
| 154 | // suitable for use in the REPL. |
| 155 | // Each function returned by MakeLoad accesses a distinct private cache. |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 156 | func MakeLoad() func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 157 | type entry struct { |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 158 | globals starlark.StringDict |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 159 | err error |
| 160 | } |
| 161 | |
| 162 | var cache = make(map[string]*entry) |
| 163 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 164 | return func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 165 | e, ok := cache[module] |
| 166 | if e == nil { |
| 167 | if ok { |
| 168 | // request for package whose loading is in progress |
| 169 | return nil, fmt.Errorf("cycle in load graph") |
| 170 | } |
| 171 | |
| 172 | // Add a placeholder to indicate "load in progress". |
| 173 | cache[module] = nil |
| 174 | |
| 175 | // Load it. |
alandonovan | 2c1f362 | 2018-12-17 13:10:16 -0500 | [diff] [blame] | 176 | thread := &starlark.Thread{Name: "exec " + module, Load: thread.Load} |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 177 | globals, err := starlark.ExecFile(thread, module, nil, nil) |
alandonovan | 5596825 | 2017-12-22 15:50:31 -0500 | [diff] [blame] | 178 | e = &entry{globals, err} |
| 179 | |
| 180 | // Update the cache. |
| 181 | cache[module] = e |
| 182 | } |
| 183 | return e.globals, e.err |
| 184 | } |
| 185 | } |