Skip to content

Commit 8e6e992

Browse files
committed
Integrate repl with debugger
1 parent 24608fb commit 8e6e992

File tree

5 files changed

+77
-17
lines changed

5 files changed

+77
-17
lines changed

debug/debugger.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ func StartDebugger(program *Program, env interface{}) {
5858
}()
5959

6060
index := make(map[int]int)
61-
for row, line := range strings.Split(program.Disassemble(), "\n") {
61+
var buf strings.Builder
62+
program.Opcodes(&buf)
63+
64+
for row, line := range strings.Split(buf.String(), "\n") {
6265
if line == "" {
6366
continue
6467
}

repl/go.mod

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ go 1.20
44

55
require (
66
github.com/antonmedv/expr v0.0.0
7+
github.com/antonmedv/expr/debug v0.0.0
78
github.com/chzyer/readline v1.5.1
89
)
910

10-
require golang.org/x/sys v0.11.0 // indirect
11+
require (
12+
github.com/gdamore/encoding v1.0.0 // indirect
13+
github.com/gdamore/tcell v1.3.0 // indirect
14+
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
15+
github.com/mattn/go-runewidth v0.0.8 // indirect
16+
github.com/rivo/tview v0.0.0-20200219210816-cd38d7432498 // indirect
17+
github.com/rivo/uniseg v0.1.0 // indirect
18+
golang.org/x/sys v0.11.0 // indirect
19+
golang.org/x/text v0.3.2 // indirect
20+
)
1121

1222
replace github.com/antonmedv/expr => ../
23+
24+
replace github.com/antonmedv/expr/debug => ../debug

repl/go.sum

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
12
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
23
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
34
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
@@ -7,16 +8,36 @@ github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38
78
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
89
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
910
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
12+
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
13+
github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
14+
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
15+
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
16+
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
17+
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
18+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
19+
github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0=
20+
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
1021
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1122
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
23+
github.com/rivo/tview v0.0.0-20200219210816-cd38d7432498 h1:4CFNy7/q7P06AsIONZzuWy7jcdqEmYQvOZ9FAFZdbls=
24+
github.com/rivo/tview v0.0.0-20200219210816-cd38d7432498/go.mod h1:6lkG1x+13OShEf0EaOCaTQYyB7d5nSbb181KtjlS+84=
25+
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
26+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
1227
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1328
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1429
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1530
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
1631
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
32+
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
33+
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1734
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1835
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
1936
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
38+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
39+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
40+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2041
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2142
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2243
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

repl/repl.go

+32-13
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,54 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67

78
"github.com/antonmedv/expr"
89
"github.com/antonmedv/expr/builtin"
10+
"github.com/antonmedv/expr/debug"
911
"github.com/antonmedv/expr/vm"
1012
"github.com/chzyer/readline"
1113
)
1214

15+
var keywords = []string{
16+
"exit", "opcodes", "debug",
17+
18+
// Predicate functions:
19+
"map", "filter", "all",
20+
"any", "none", "one",
21+
22+
// Operators:
23+
"and", "or", "in", "not", "not in",
24+
"contains", "matches", "startsWith", "endsWith",
25+
}
26+
1327
func main() {
14-
extra := []string{
15-
"exit",
16-
"opcodes",
17-
"map",
18-
"filter",
19-
"all",
20-
"any",
21-
"none",
22-
"one",
23-
}
2428
rl, err := readline.NewEx(&readline.Config{
2529
Prompt: "> ",
26-
AutoComplete: completer{append(builtin.Names, extra...)},
30+
AutoComplete: completer{append(builtin.Names, keywords...)},
2731
})
2832
if err != nil {
2933
panic(err)
3034
}
3135
defer rl.Close()
3236

37+
env := map[string]interface{}{
38+
"ENV": os.Environ(),
39+
}
3340
var program *vm.Program
41+
3442
for {
3543
line, err := rl.Readline()
3644
if err != nil { // io.EOF when Ctrl-D is pressed
3745
break
3846
}
3947
line = strings.TrimSpace(line)
48+
4049
if line == "exit" {
4150
break
4251
}
52+
4353
if line == "opcodes" {
4454
if program == nil {
4555
fmt.Println("no program")
@@ -49,12 +59,21 @@ func main() {
4959
continue
5060
}
5161

52-
program, err = expr.Compile(line, expr.Env(nil))
62+
if line == "debug" {
63+
if program == nil {
64+
fmt.Println("no program")
65+
continue
66+
}
67+
debug.StartDebugger(program, env)
68+
continue
69+
}
70+
71+
program, err = expr.Compile(line, expr.Env(env), expr.ExperimentalPipes())
5372
if err != nil {
5473
fmt.Printf("compile error: %s\n", err)
5574
continue
5675
}
57-
output, err := expr.Run(program, nil)
76+
output, err := expr.Run(program, env)
5877
if err != nil {
5978
fmt.Printf("runtime error: %s\n", err)
6079
continue

vm/program.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vm
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"reflect"
78
"regexp"
89
"strings"
@@ -27,6 +28,12 @@ type Program struct {
2728
func (program *Program) Disassemble() string {
2829
var buf bytes.Buffer
2930
w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
31+
program.Opcodes(w)
32+
_ = w.Flush()
33+
return buf.String()
34+
}
35+
36+
func (program *Program) Opcodes(w io.Writer) {
3037
ip := 0
3138
for ip < len(program.Bytecode) {
3239
pp := ip
@@ -275,6 +282,4 @@ func (program *Program) Disassemble() string {
275282
_, _ = fmt.Fprintf(w, "%v\t%#x (unknown)\n", ip, op)
276283
}
277284
}
278-
_ = w.Flush()
279-
return buf.String()
280285
}

0 commit comments

Comments
 (0)