2018-09-18 22:42:38 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/MontFerret/ferret/pkg/compiler"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime"
|
|
|
|
"github.com/chzyer/readline"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-09-23 08:05:05 +02:00
|
|
|
func Repl(version string, opts Options) {
|
2018-09-18 22:42:38 +02:00
|
|
|
ferret := compiler.New()
|
|
|
|
|
|
|
|
fmt.Printf("Welcome to Ferret REPL %s\n", version)
|
|
|
|
fmt.Println("Please use `exit` or `Ctrl-D` to exit this program.")
|
|
|
|
|
|
|
|
rl, err := readline.NewEx(&readline.Config{
|
|
|
|
Prompt: "> ",
|
|
|
|
InterruptPrompt: "^C",
|
|
|
|
EOFPrompt: "exit",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rl.Close()
|
|
|
|
|
|
|
|
var commands []string
|
2018-09-23 08:28:26 +02:00
|
|
|
var multiline bool
|
2018-09-23 08:05:05 +02:00
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
timer := NewTimer()
|
|
|
|
|
|
|
|
for {
|
|
|
|
line, err := rl.Readline()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-23 08:28:26 +02:00
|
|
|
if strings.HasPrefix(line, "%") {
|
|
|
|
line = line[1:]
|
|
|
|
|
|
|
|
multiline = !multiline
|
|
|
|
}
|
|
|
|
|
|
|
|
if multiline {
|
|
|
|
commands = append(commands, line)
|
2018-09-18 22:42:38 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
commands = append(commands, line)
|
2018-09-23 08:28:26 +02:00
|
|
|
query := strings.TrimSpace(strings.Join(commands, "\n"))
|
2018-09-18 22:42:38 +02:00
|
|
|
commands = make([]string, 0, 10)
|
|
|
|
|
2018-09-23 08:28:26 +02:00
|
|
|
if query == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
program, err := ferret.Compile(query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to parse the query")
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Start()
|
|
|
|
|
2018-09-23 08:05:05 +02:00
|
|
|
out, err := program.Run(
|
|
|
|
context.Background(),
|
|
|
|
runtime.WithBrowser(opts.Cdp),
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
fmt.Println(timer.Print())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to execute the query")
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
}
|
|
|
|
}
|