summaryrefslogtreecommitdiff
path: root/interpreter/repl.go
diff options
context:
space:
mode:
Diffstat (limited to 'interpreter/repl.go')
-rw-r--r--interpreter/repl.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/interpreter/repl.go b/interpreter/repl.go
new file mode 100644
index 0000000..73af8a7
--- /dev/null
+++ b/interpreter/repl.go
@@ -0,0 +1,35 @@
+package interpreter
+
+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
+}