-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.go
executable file
·161 lines (152 loc) · 3.14 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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"gitlab.lazymio.cn/mio/miniplc0/vm"
)
var debugHelp = `Simple miniplc0 debugger.
You can use the abbreviation of a command.
[H]elp -- Show this message.
[N]ext -- Run a single instruction.
[L]ist n -- List n instructions.
[S]tack n -- Show n stack elemets.
[I]formation -- Show current information.
[Q]uit -- Quit the debugger.
`
// [R]estart -- Restart the debugging. Not impleneted
type cmd int32
const (
cNext cmd = iota
cList
cStack
cInformation
cRestart
cHelp
cQuit
)
type debuggerCommand struct {
C cmd
X int32
}
func newCommandFromString(line string) *debuggerCommand {
ln := strings.TrimSpace(line)
tokens := strings.Split(ln, " ")
if len(tokens) == 0 {
return nil
}
if len(tokens) == 1 {
switch strings.ToLower(tokens[0]) {
case "h":
fallthrough
case "help":
return &debuggerCommand{C: cHelp}
case "q":
fallthrough
case "quit":
return &debuggerCommand{C: cQuit}
case "r":
fallthrough
case "restart":
//return &debuggerCommand{C: cRestart}
return nil
case "i":
fallthrough
case "info":
fallthrough
case "infomation":
return &debuggerCommand{C: cInformation}
case "l":
fallthrough
case "list":
return &debuggerCommand{C: cList, X: 10}
case "s":
fallthrough
case "stack":
return &debuggerCommand{C: cStack, X: 20}
case "n":
fallthrough
case "next":
return &debuggerCommand{C: cNext}
}
return nil
}
if len(tokens) == 2 {
x, err := strconv.ParseInt(tokens[1], 10, 32)
if err != nil {
return nil
}
switch strings.ToLower(tokens[0]) {
case "l":
fallthrough
case "list":
return &debuggerCommand{C: cList, X: int32(x)}
case "s":
fallthrough
case "stack":
return &debuggerCommand{C: cStack, X: int32(x)}
}
return nil
}
return nil
}
// Debug debugs the epf file.
func Debug(file *os.File) {
epf, err := vm.NewEPFv1FromFile(file)
if err != nil {
panic(err)
}
v := vm.NewVMDefault(len(epf.GetInstructions()), epf.GetEntry())
v.Load(epf.GetInstructions())
scanner := bufio.NewScanner(os.Stdin)
fmt.Print(debugHelp)
for true {
fmt.Print(">")
scanner.Scan()
cmd := newCommandFromString(scanner.Text())
quit := false
if cmd == nil {
fmt.Println("Wrong format.\nType 'help' to see more.")
continue
}
switch cmd.C {
case cHelp:
fmt.Print(debugHelp)
case cQuit:
quit = true
break
case cRestart:
quit = true
break
case cNext:
if err = v.RunSingle(); err != nil {
if err == vm.ErrIllegalInstruction {
fmt.Println("The program has stopped.")
} else {
// Should have better user experience.
fmt.Println(err)
fmt.Println(*v)
os.Exit(0)
}
}
fmt.Printf("Next instruction: %v\n", *(v.NextInstruction()))
case cInformation:
fmt.Printf("IP=%v SP=%v\nInstructions[IP]:%v\n", v.IP, v.SP, *(v.NextInstruction()))
stackvalue := v.GetStackTop()
if stackvalue != nil {
fmt.Printf("Stack[SP-1]=%v\n", *stackvalue)
} else {
fmt.Println("Stack[SP-1]=[Invalid]")
}
case cList:
fmt.Println(v.InstructionGraph(cmd.X))
case cStack:
fmt.Println(v.StackGraph(cmd.X))
}
if quit {
break
}
}
}