Skip to content

Commit dddb897

Browse files
authored
Drop command pattern (dop251#2)
* drop command pattern in debugger as it's not very useful * Drop Result in debugger * Unexport StringToLines
1 parent 71d572e commit dddb897

File tree

1 file changed

+19
-95
lines changed

1 file changed

+19
-95
lines changed

debugger.go

+19-95
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ type Debugger struct {
2020
active bool
2121
}
2222

23-
type Result struct {
24-
Value interface{}
25-
Err error
26-
}
27-
2823
func NewDebugger(vm *vm) *Debugger {
2924
dbg := &Debugger{
3025
vm: vm,
@@ -107,48 +102,7 @@ func (dbg *Debugger) Breakpoints() ([]Breakpoint, error) {
107102
return dbg.breakpoints, nil
108103
}
109104

110-
func (dbg *Debugger) Next() Result {
111-
cmd := NextCommand{}
112-
return cmd.execute(dbg)
113-
}
114-
115-
func (dbg *Debugger) Continue() Result {
116-
cmd := ContinueCommand{}
117-
return cmd.execute(dbg)
118-
}
119-
120-
func (dbg *Debugger) StepIn() Result {
121-
cmd := StepInCommand{}
122-
return cmd.execute(dbg)
123-
}
124-
125-
func (dbg *Debugger) StepOut() Result {
126-
cmd := StepOutCommand{}
127-
return cmd.execute(dbg)
128-
}
129-
130-
func (dbg *Debugger) Exec(expr string) Result {
131-
cmd := ExecCommand{expression: expr}
132-
return cmd.execute(dbg)
133-
}
134-
135-
func (dbg *Debugger) Print(varName string) Result {
136-
cmd := PrintCommand{varName: varName}
137-
return cmd.execute(dbg)
138-
}
139-
140-
func (dbg *Debugger) List() Result {
141-
cmd := ListCommand{}
142-
return cmd.execute(dbg)
143-
}
144-
145-
type Command interface {
146-
execute() (interface{}, error)
147-
}
148-
149-
type NextCommand struct{}
150-
151-
func (*NextCommand) execute(dbg *Debugger) Result {
105+
func (dbg *Debugger) Next() error {
152106
// TODO: implement proper error propagation
153107
lastLine := dbg.Line()
154108
dbg.updateCurrentLine()
@@ -165,12 +119,10 @@ func (*NextCommand) execute(dbg *Debugger) Result {
165119
dbg.vm.prg.code[dbg.vm.pc].exec(dbg.vm)
166120
}
167121
dbg.updateLastLine(lastLine)
168-
return Result{Value: nil, Err: nil}
122+
return nil
169123
}
170124

171-
type ContinueCommand struct{}
172-
173-
func (*ContinueCommand) execute(dbg *Debugger) Result {
125+
func (dbg *Debugger) Continue() error {
174126
// TODO: implement proper error propagation
175127
lastLine := dbg.Line()
176128
dbg.updateCurrentLine()
@@ -180,74 +132,46 @@ func (*ContinueCommand) execute(dbg *Debugger) Result {
180132
// TODO: wait for command
181133
dbg.updateCurrentLine()
182134
dbg.updateLastLine(lastLine)
183-
return Result{Value: nil, Err: nil}
135+
return nil
184136
}
185137
dbg.vm.prg.code[dbg.vm.pc].exec(dbg.vm)
186138
dbg.updateCurrentLine()
187139
}
188140
dbg.updateLastLine(lastLine)
189-
return Result{Value: nil, Err: nil}
190-
}
191-
192-
type StepInCommand struct{}
193-
194-
func (*StepInCommand) execute(dbg *Debugger) Result {
195-
return Result{Value: nil, Err: errors.New("not implemented yet")}
196-
}
197-
198-
type StepOutCommand struct{}
199-
200-
func (*StepOutCommand) execute(dbg *Debugger) Result {
201-
return Result{Value: nil, Err: errors.New("not implemented yet")}
202-
}
203-
204-
type ExecCommand struct {
205-
expression string
141+
return nil
206142
}
207143

208-
func (e *ExecCommand) execute(dbg *Debugger) Result {
209-
if e.expression == "" {
210-
return Result{Value: nil, Err: errors.New("nothing to execute")}
144+
func (dbg *Debugger) Exec(expr string) (Value, error) {
145+
if expr == "" {
146+
return nil, errors.New("nothing to execute")
211147
}
212-
val, err := dbg.eval(e.expression)
148+
val, err := dbg.eval(expr)
213149

214150
lastLine := dbg.Line()
215151
dbg.updateLastLine(lastLine)
216-
return Result{Value: val, Err: err}
217-
}
218-
219-
type PrintCommand struct {
220-
varName string
152+
return val, err
221153
}
222154

223-
func (p *PrintCommand) execute(dbg *Debugger) Result {
224-
if p.varName == "" {
225-
return Result{Value: "", Err: errors.New("please specify variable name")}
155+
func (dbg *Debugger) Print(varName string) (string, error) {
156+
if varName == "" {
157+
return "", errors.New("please specify variable name")
226158
}
227-
val, err := dbg.getValue(p.varName)
159+
val, err := dbg.getValue(varName)
228160

229161
if val == Undefined() {
230-
return Result{Value: fmt.Sprint(dbg.vm.prg.values), Err: err}
162+
return fmt.Sprint(dbg.vm.prg.values), err
231163
} else {
232164
// FIXME: val.ToString() causes debugger to exit abruptly
233-
return Result{Value: fmt.Sprint(val), Err: err}
165+
return fmt.Sprint(val), err
234166
}
235167
}
236168

237-
type ListCommand struct{}
238-
239-
func (*ListCommand) execute(dbg *Debugger) Result {
169+
func (dbg *Debugger) List() ([]string, error) {
240170
// TODO probably better to get only some of the lines, but fine for now
241-
val, err := StringToLines(dbg.vm.prg.src.Source())
242-
return Result{Value: val, Err: err}
171+
return stringToLines(dbg.vm.prg.src.Source())
243172
}
244173

245-
type (
246-
EmptyCommand struct{}
247-
NewLineCommand struct{}
248-
)
249-
250-
func StringToLines(s string) (lines []string, err error) {
174+
func stringToLines(s string) (lines []string, err error) {
251175
scanner := bufio.NewScanner(strings.NewReader(s))
252176
for scanner.Scan() {
253177
lines = append(lines, scanner.Text())

0 commit comments

Comments
 (0)