/
fns_repl.go
63 lines (57 loc) · 1.3 KB
/
fns_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
package main
import (
"fmt"
)
var replHelpMessage = `help display this message
import <string> import a module
dir list def names
quit quit this program`
var builtinReplFuncs []BuiltinFuncConfig = []BuiltinFuncConfig{
BuiltinFuncConfig{
Name: "help",
Args: []string{},
Targets: []string{"repl"},
Eval: func(self *BuiltinCall, ew ErrorWriter) Value {
return NewIO(
func(ioc IOContext) Value {
fmt.Fprintf(ioc.Stdout(), "%s", replHelpMessage)
return nil
},
self.ctx,
)
},
},
BuiltinFuncConfig{
Name: "dir",
Args: []string{},
Targets: []string{"repl"},
Eval: func(self *BuiltinCall, ew ErrorWriter) Value {
return NewIO(
func(ioc IOContext) Value {
// only unique names
strs := AssertReplIOContext(ioc).ListNames()
items := []Value{}
for _, str := range strs {
items = append(items, NewString(str, self.Context()))
}
return NewList(items, self.Context())
},
self.ctx,
)
},
},
BuiltinFuncConfig{
Name: "quit",
Args: []string{},
Targets: []string{"repl"},
Eval: func(self *BuiltinCall, ew ErrorWriter) Value {
return NewIO(
func(ioc IOContext) Value {
AssertReplIOContext(ioc).repl.Quit()
return nil
},
self.ctx,
)
},
},
}