forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool_repl.go
161 lines (140 loc) · 3.31 KB
/
tool_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
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"go/scanner"
"os"
"strings"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/gnovm/pkg/repl"
"github.com/gnolang/gno/tm2/pkg/commands"
)
type replCfg struct {
rootDir string
initialCommand string
skipUsage bool
}
func newReplCmd() *commands.Command {
cfg := &replCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "repl",
ShortUsage: "repl [flags]",
ShortHelp: "starts a GnoVM REPL",
},
cfg,
func(_ context.Context, args []string) error {
return execRepl(cfg, args)
},
)
}
func (c *replCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.rootDir,
"root-dir",
"",
"clone location of github.com/gnolang/gno (gno tries to guess it)",
)
fs.StringVar(
&c.initialCommand,
"command",
"",
"initial command to run",
)
fs.BoolVar(
&c.skipUsage,
"skip-usage",
false,
"do not print usage",
)
}
func execRepl(cfg *replCfg, args []string) error {
if len(args) > 0 {
return flag.ErrHelp
}
if cfg.rootDir == "" {
cfg.rootDir = gnoenv.RootDir()
}
if !cfg.skipUsage {
fmt.Fprint(os.Stderr, `// Usage:
// gno> import "gno.land/p/demo/avl" // import the p/demo/avl package
// gno> func a() string { return "a" } // declare a new function named a
// gno> /src // print current generated source
// gno> /debug // activate the GnoVM debugger
// gno> /editor // enter in multi-line mode, end with ';'
// gno> /reset // remove all previously inserted code
// gno> println(a()) // print the result of calling a()
// gno> /exit // alternative to <Ctrl-D>
`)
}
return runRepl(cfg)
}
func runRepl(cfg *replCfg) error {
r := repl.NewRepl()
if cfg.initialCommand != "" {
handleInput(r, cfg.initialCommand)
}
fmt.Fprint(os.Stdout, "gno> ")
inEdit := false
prev := ""
liner := bufio.NewScanner(os.Stdin)
for liner.Scan() {
line := liner.Text()
if l := strings.TrimSpace(line); l == ";" {
line, inEdit = "", false
} else if l == "/editor" {
line, inEdit = "", true
fmt.Fprintln(os.Stdout, "// enter a single ';' to quit and commit")
}
if prev != "" {
line = prev + "\n" + line
prev = ""
}
if inEdit {
fmt.Fprint(os.Stdout, "... ")
prev = line
continue
}
if err := handleInput(r, line); err != nil {
var goScanError scanner.ErrorList
if errors.As(err, &goScanError) {
// We assune that a Go scanner error indicates an incomplete Go statement.
// Append next line and retry.
prev = line
} else {
fmt.Fprintln(os.Stderr, err)
}
}
if prev == "" {
fmt.Fprint(os.Stdout, "gno> ")
} else {
fmt.Fprint(os.Stdout, "... ")
}
}
return nil
}
// handleInput executes specific "/" commands, or evaluates input as Gno source code.
func handleInput(r *repl.Repl, input string) error {
switch strings.TrimSpace(input) {
case "/reset":
r.Reset()
case "/debug":
r.Debug()
case "/src":
fmt.Fprintln(os.Stdout, r.Src())
case "/exit":
os.Exit(0)
case "":
// Avoid to increase the repl execution counter if no input.
default:
out, err := r.Process(input)
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, out)
}
return nil
}