-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
169 lines (151 loc) · 3.5 KB
/
repl.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package vesper
import (
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"github.com/chzyer/readline"
)
const (
red = "\033[0;31m"
green = "\033[0;32m"
blue = "\033[0;34m"
black = "\033[0;0m"
)
type replHandler struct {
rl *readline.Instance
vm *VM
line string
cmds []string
}
func (repl *replHandler) Eval(expr string) (string, bool, error) {
interrupted = false
repl.cmds = append(repl.cmds, expr)
whole := strings.Trim(strings.Join(repl.cmds, " "), " ")
more, err := repl.checkBalanced("(", ")")
if more || err != nil {
return "", more, err
}
more, err = repl.checkBalanced("[", "]")
if more || err != nil {
return "", more, err
}
more, err = repl.checkBalanced("{", "}")
if more || err != nil {
return "", more, err
}
if whole == "" {
return "", false, nil
}
lexpr, err := repl.vm.Read(String(whole), AnyType)
repl.cmds = nil
if err != nil {
return "", false, err
}
val, err := repl.vm.Eval(lexpr)
if err != nil {
return "", false, err
}
if val == nil {
return red + " Internal error -> Eval returned nil" + black, false, nil
}
return "-> " + Write(val), false, nil
}
func (repl *replHandler) checkBalanced(open string, close string) (bool, error) {
whole := strings.Trim(strings.Join(repl.cmds, " "), " ")
opens := len(strings.Split(whole, open))
closes := len(strings.Split(whole, close))
if opens > closes {
return true, nil
} else if closes > opens {
repl.cmds = nil
return false, fmt.Errorf("unbalanced '%s' encountered", close)
}
return false, nil
}
func (repl *replHandler) Prompt(more bool) string {
promptType := "*prompt*"
if more {
promptType = "*prompt-cont*"
}
prompt := GetGlobal(repl.vm.Intern(promptType))
if prompt != nil {
return prompt.String()
}
if more {
return ":| "
}
return ":> "
}
// REPL starts a REPL with the given VM.
func REPL(vm *VM) error {
var err error
interrupts = make(chan os.Signal, 1)
signal.Notify(interrupts, os.Interrupt)
defer signal.Stop(interrupts)
repl := replHandler{
vm: vm,
}
repl.rl, err = readline.NewEx(&readline.Config{
Prompt: repl.Prompt(false),
HistoryFile: filepath.Join(os.TempDir(), "readline_vesper.tmp"),
HistorySearchFold: true,
FuncFilterInputRune: filterInput,
})
if err != nil {
return err
}
defer func() { _ = repl.rl.Close() }()
for {
err = repl.read()
if err != nil {
switch err {
case io.EOF:
fmt.Println(repl.line)
return nil
case readline.ErrInterrupt: // Pressing Ctrl-C
if len(repl.line) == 0 {
if repl.cmds == nil {
return nil
}
}
// Erasing command buffer
repl.cmds = nil
continue
}
}
fmt.Println(repl.Prompt(len(repl.cmds) > 1) + repl.line)
fmt.Print(blue)
result, more, err := repl.Eval(repl.line)
fmt.Print(black)
if err != nil {
fmt.Println(red, "***", err, black)
repl.cmds = nil
repl.rl.SetPrompt(repl.Prompt(false))
continue
} else if more {
repl.rl.SetPrompt(repl.Prompt(true))
continue
} else {
fmt.Println(green + result + black)
repl.cmds = nil
repl.rl.SetPrompt(repl.Prompt(false))
continue
}
}
}
// filterInput just ignores Ctrl-z.
func filterInput(r rune) (rune, bool) {
return r, r != readline.CharCtrlZ
}
// read fetches one line from input, with the help of Readline library.
func (repl *replHandler) read() error {
repl.rl.Config.UniqueEditLine = true // required to update the prompt
line, err := repl.rl.Readline()
repl.rl.Config.UniqueEditLine = false
line = strings.TrimSpace(line)
repl.line = line
return err
}