forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.go
186 lines (163 loc) · 3.99 KB
/
debugger.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/antonmedv/expr/checker"
"github.com/antonmedv/expr/compiler"
"github.com/antonmedv/expr/optimizer"
"github.com/antonmedv/expr/parser"
. "github.com/antonmedv/expr/vm"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/sanity-io/litter"
)
func debugger() {
tree, err := parser.Parse(input())
check(err)
_, err = checker.Check(tree, nil)
check(err)
if opt {
err = optimizer.Optimize(&tree.Node, nil)
check(err)
}
program, err := compiler.Compile(tree, nil)
check(err)
vm := Debug()
app := tview.NewApplication()
table := tview.NewTable()
stack := tview.NewTable()
stack.
SetBorder(true).
SetTitle("Stack")
scope := tview.NewTable()
scope.
SetBorder(true).
SetTitle("Scope")
sub := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(stack, 0, 3, false).
AddItem(scope, 0, 1, false)
flex := tview.NewFlex().
AddItem(table, 0, 1, true).
AddItem(sub, 0, 1, false)
app.SetRoot(flex, true)
go func() {
out, _ := vm.Run(program, nil)
app.QueueUpdateDraw(func() {
sub.RemoveItem(scope)
result := tview.NewTextView()
result.
SetBorder(true).
SetTitle("Output")
result.SetText(litter.Sdump(out))
sub.AddItem(result, 0, 1, false)
})
}()
index := make(map[int]int)
for row, line := range strings.Split(program.Disassemble(), "\n") {
if line == "" {
continue
}
parts := strings.Split(line, "\t")
ip, err := strconv.Atoi(parts[0])
check(err)
index[ip] = row
table.SetCellSimple(row, 0, fmt.Sprintf("% *d", 5, ip))
for col := 1; col < len(parts); col++ {
table.SetCellSimple(row, col, parts[col])
}
for col := len(parts); col < 4; col++ {
table.SetCellSimple(row, col, "")
}
table.SetCell(row, 4, tview.NewTableCell("").SetExpansion(1))
}
draw := func(ip int) {
app.QueueUpdateDraw(func() {
for row := 0; row < table.GetRowCount(); row++ {
for col := 0; col < table.GetColumnCount(); col++ {
table.GetCell(row, col).SetBackgroundColor(tcell.ColorDefault)
}
}
if row, ok := index[ip]; ok {
table.Select(row, 0)
for col := 0; col < 5; col++ {
table.GetCell(row, col).SetBackgroundColor(tcell.ColorMediumBlue)
}
table.SetOffset(row-10, 0)
opcode := table.GetCell(row, 1).Text
if strings.HasPrefix(opcode, "OpJump") {
jump := table.GetCell(row, 3).Text
jump = strings.Trim(jump, "()")
ip, err := strconv.Atoi(jump)
if err == nil {
if row, ok := index[ip]; ok {
for col := 0; col < 5; col++ {
table.GetCell(row, col).SetBackgroundColor(tcell.ColorDimGrey)
}
}
}
}
}
stack.Clear()
for i, value := range vm.Stack() {
stack.SetCellSimple(i, 0, fmt.Sprintf("% *d: ", 2, i))
stack.SetCellSimple(i, 1, fmt.Sprintf("%+v", value))
}
stack.ScrollToEnd()
scope.Clear()
var keys []string
for k := range vm.Scope() {
keys = append(keys, k)
}
sort.Strings(keys)
row := 0
for _, name := range keys {
scope.SetCellSimple(row, 0, fmt.Sprintf("%v: ", name))
scope.SetCellSimple(row, 1, fmt.Sprintf("%v", vm.Scope()[name]))
row++
}
})
}
getSelectedPosition := func() int {
row, _ := table.GetSelection()
ip, err := strconv.Atoi(strings.TrimSpace(table.GetCell(row, 0).Text))
check(err)
return ip
}
autostep := false
var breakpoint int
go func() {
draw(0)
for ip := range vm.Position() {
draw(ip)
if autostep {
if breakpoint != ip {
time.Sleep(20 * time.Millisecond)
vm.Step()
} else {
autostep = false
}
}
}
}()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyDown || event.Key() == tcell.KeyUp {
table.SetSelectable(true, false)
}
if event.Key() == tcell.KeyEnter {
selectable, _ := table.GetSelectable()
if selectable {
table.SetSelectable(false, false)
breakpoint = getSelectedPosition()
autostep = true
}
vm.Step()
}
return event
})
err = app.Run()
check(err)
}