diff options
| author | Marc Vertes <mvertes@free.fr> | 2025-11-29 19:46:34 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2025-11-29 19:46:34 +0100 |
| commit | f40a1c23467eef36f53635e525f8b25f591e8a45 (patch) | |
| tree | beae2442ecee1d734b9a676f7b176376a1d80af5 /interp/repl.go | |
| parent | fbc73922e9853d7e344e388f3fdfedb8fa323682 (diff) | |
chore: shorter name for packages, simpilfy vm values
Diffstat (limited to 'interp/repl.go')
| -rw-r--r-- | interp/repl.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/interp/repl.go b/interp/repl.go new file mode 100644 index 0000000..b8a5338 --- /dev/null +++ b/interp/repl.go @@ -0,0 +1,35 @@ +package interp + +import ( + "bufio" + "errors" + "fmt" + "io" + + "github.com/mvertes/parscan/scanner" +) + +// Repl executes an interactive line oriented Read Eval Print Loop (REPL). +func (i *Interp) Repl(in io.Reader) (err error) { + liner := bufio.NewScanner(in) + text, prompt := "", "> " + fmt.Print(prompt) + for liner.Scan() { + text += liner.Text() + res, err := i.Eval(text + "\n") + switch { + case err == nil: + if res.IsValid() { + fmt.Println(": ", res) + } + text, prompt = "", "> " + case errors.Is(err, scanner.ErrBlock): + prompt = ">> " + default: + fmt.Println("Error:", err) + text, prompt = "", "> " + } + fmt.Print(prompt) + } + return err +} |
