z_mirror/pesticide

git clone https://git.raiguard.me/z_mirror/pesticide

Caleb HeuerAdd stepping commands6cccd4f

trunk
2.3 KiB115 linesraw
1package command
2
3import (
4	"errors"
5	"fmt"
6	"os"
7	"path/filepath"
8	"strconv"
9	"strings"
10)
11
12type Command interface {
13	command()
14}
15
16type (
17	Backtrace struct{}
18	Break     struct {
19		File string
20		Line int
21	}
22	Continue struct{}
23	Down     int
24	Evaluate struct{ Expr string }
25	Launch   struct{ Name string }
26	Next     struct{}
27	Pause    struct{}
28	Quit     struct{}
29	Step     struct{}
30	StepOut  struct{}
31	Up       int
32)
33
34func (b Backtrace) command() {}
35func (b Break) command()     {}
36func (c Continue) command()  {}
37func (d Down) command()      {}
38func (e Evaluate) command()  {}
39func (l Launch) command()    {}
40func (s Next) command()      {}
41func (p Pause) command()     {}
42func (q Quit) command()      {}
43func (s Step) command()      {}
44func (s StepOut) command()   {}
45func (u Up) command()        {}
46
47func Parse(input string) (Command, error) {
48	args := strings.Split(input, " ")
49	if len(args) == 0 {
50		return nil, nil
51	}
52	switch args[0] {
53	case "backtrace", "bt":
54		return Backtrace{}, nil
55	case "break", "b":
56		return cmdParseBreak(args[1:])
57	case "continue", "c":
58		return Continue{}, nil
59	case "down", "dow":
60		return Down(1), nil
61	case "evaluate", "eval", "e":
62		return Evaluate{Expr: strings.Join(args[1:], " ")}, nil
63	case "launch", "l":
64		return cmdParseLaunch(args[1:])
65	case "pause", "p":
66		return Pause{}, nil
67	case "quit", "q":
68		return Quit{}, nil
69	case "step", "s":
70		return Step{}, nil
71	case "stepout", "finish", "fin":
72		return StepOut{}, nil
73	case "next", "n":
74		return Next{}, nil
75	case "up":
76		return Up(1), nil
77	default:
78		return nil, errors.New(fmt.Sprintf("Unknown command: %s", args[0]))
79	}
80}
81
82func cmdParseBreak(args []string) (Break, error) {
83	var b Break
84	if len(args) != 2 {
85		return b, errors.New("syntax: break <filename> <line>")
86	}
87	filename, err := filepath.Abs(args[0])
88	if err != nil {
89		return b, err
90	}
91	f, err := os.Open(filename)
92	defer f.Close()
93	if err != nil {
94		return b, err
95	}
96	// TODO: Validate that file exists
97	b.File = filename
98	line, err := strconv.ParseUint(args[1], 0, 32)
99	if err != nil {
100		return b, err
101	}
102	b.Line = int(line)
103	return b, nil
104}
105
106func cmdParseLaunch(args []string) (Launch, error) {
107	var l Launch
108	if len(args) == 0 {
109		return l, errors.New("Did not specify a configuration to launch")
110	} else if len(args) > 1 {
111		return l, errors.New("Too many arguments")
112	}
113	l.Name = args[0]
114	return l, nil
115}