-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexpr.go
132 lines (128 loc) · 3.44 KB
/
expr.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
package main
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"os"
"strconv"
"strings"
)
func expr(e string) error {
ex, err := parser.ParseExpr(e)
if err != nil {
return err
}
switch ex := ex.(type) {
case *ast.CallExpr:
switch fun := ex.Fun.(type) {
case *ast.SelectorExpr:
k := fun.X.(*ast.Ident).Name
if v, ok := workspace[k]; ok {
switch fun.Sel.Name {
case "delete":
if len(ex.Args) != 1 {
return errors.New("keep() expects exactly one argument")
}
return v.Delete(ex.Args[0].(*ast.BasicLit).Value)
case "dedup":
if len(ex.Args) != 0 {
return errors.New("dedup() expects no arguments")
}
v.Dedup()
return nil
case "keep":
if len(ex.Args) != 1 {
return errors.New("delete() expects exactly one argument")
}
return v.Keep(ex.Args[0].(*ast.BasicLit).Value)
case "save":
if len(ex.Args) != 1 {
return errors.New("save() expects exactly one argument")
}
fn := strings.Trim(ex.Args[0].(*ast.BasicLit).Value, "\"")
if _, err := os.Stat(fn); err == nil {
pmpt := fmt.Sprintf("File %s already exists, overwrite it? [Y]/n: ", fn)
var confirm string
if confirm, err = line.Prompt(pmpt); err != nil {
return err
}
confirm = strings.ToLower(strings.TrimSpace(confirm))
if confirm != "y" && confirm != "" {
return nil
}
}
if err := v.Save(fn); err != nil {
return err
}
fmt.Printf("Goroutines are saved to file %s.\n", fn)
case "search":
var err error
offset := 0
limit := 10
switch len(ex.Args) {
case 0:
return errors.New("search() expects at least one argument")
case 1:
case 2:
offset, err = strconv.Atoi(ex.Args[1].(*ast.BasicLit).Value)
if err != nil {
return fmt.Errorf("invalid argument %s", ex.Args[1])
}
case 3:
offset, err = strconv.Atoi(ex.Args[1].(*ast.BasicLit).Value)
if err != nil {
return fmt.Errorf("invalid argument 'offset' %s", ex.Args[1])
}
limit, err = strconv.Atoi(ex.Args[2].(*ast.BasicLit).Value)
if err != nil {
return fmt.Errorf("invalid argument 'limit' %s", ex.Args[2])
}
default:
return errors.New("search() expects at most three arguments")
}
v.Search(ex.Args[0].(*ast.BasicLit).Value, offset, limit)
return nil
case "show":
var err error
offset := 0
limit := 10
switch len(ex.Args) {
case 0:
case 1:
offset, err = strconv.Atoi(ex.Args[0].(*ast.BasicLit).Value)
if err != nil {
return fmt.Errorf("invalid argument %s", ex.Args[0])
}
case 2:
offset, err = strconv.Atoi(ex.Args[0].(*ast.BasicLit).Value)
if err != nil {
return fmt.Errorf("invalid argument 'offset' %s", ex.Args[0])
}
limit, err = strconv.Atoi(ex.Args[1].(*ast.BasicLit).Value)
if err != nil {
return fmt.Errorf("invalid argument 'limit' %s", ex.Args[1])
}
default:
return errors.New("show() expects at least one and at most two arguments")
}
v.Show(offset, limit)
return nil
default:
return fmt.Errorf("unknown instrution")
}
}
default:
return fmt.Errorf("unknown instrution")
}
case *ast.Ident:
if v, ok := workspace[ex.String()]; ok {
v.Summary()
} else {
return fmt.Errorf("variable %s not found in workspace", e)
}
default:
return fmt.Errorf("unknown instrution")
}
return nil
}