blob: 73af8a7bddf77cede01bfd0ffdfdae25525fc78a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
}
|