forked from fjballest/clive
/
xp.go
140 lines (134 loc) · 2.39 KB
/
xp.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
/*
Evaluate expressions.
*/
package xp
import (
"clive/zx"
"clive/dbg"
"clive/app"
"clive/app/opt"
"errors"
"fmt"
"sort"
"strings"
"time"
)
type xCmd {
*opt.Flags
*app.Ctx
result interface{}
quiet bool
}
func (x *xCmd) expr(s string) (result interface{}, err error) {
defer func() {
if x := recover(); x != nil {
result = nil
err = fmt.Errorf("failed: %s", x)
}
}()
var v yySymType
l := newLex(s)
if debugLex {
for c := l.Lex(&v); c != 0; c = l.Lex(&v) {
}
return nil, nil
}
yyParse(l)
return l.result, nil
}
func (x *xCmd) xp(in chan interface{}) error {
out := app.Out()
d := zx.Dir{"uname": "stdin"}
var sts, err error
var res interface{}
nln := 0
doselect {
case s := <-x.Sig:
app.Dprintf("got sig %s\n", s)
app.Fatal(dbg.ErrIntr)
case m, ok := <-in:
if !ok {
app.Dprintf("eof\n")
break
}
switch m := m.(type) {
case []byte:
e := strings.TrimSpace(string(m))
app.Dprintf("got %T '%s'\n", m, e)
nln++
if e == "" {
continue
}
res, err = x.expr(e)
if err != nil {
app.Warn("%s:%d: %s", d["uname"], nln, err)
sts = err
}
if !x.quiet {
if t, ok := res.(time.Time); ok {
res = t.Format(opt.TimeFormat)
}
ok = app.Printf("%v\n", res) == nil
}
case zx.Dir:
d = m
ok = out <- m
nln = 0
default:
app.Dprintf("got %T\n", m)
ok = out <- m
}
if !ok {
app.Exits(cerror(out))
}
}
if sts == nil {
sts = cerror(in)
}
if sts != nil {
return sts
}
if x, ok := res.(bool); ok && !x {
return errors.New("false")
}
return nil
}
func Run() {
x := &xCmd{Ctx: app.AppCtx()}
x.Flags = opt.New("[expr]")
x.NewFlag("D", "debug", &x.Debug)
x.NewFlag("L", "debug lex", &debugLex)
x.NewFlag("Y", "debug yacc", &debugYacc)
bhelp := false
x.NewFlag("F", "report known functions and exit", &bhelp)
defer func() {
debugLex = false
debugYacc = false
}()
x.NewFlag("q", "do not print values as they are evaluated", &x.quiet)
args, err := x.Parse(x.Args)
if err != nil {
app.Warn("%s", err)
x.Usage()
app.Exits("usage")
}
if bhelp {
fns := []string{}
for k := range funcs {
fns = append(fns, k)
}
sort.Sort(sort.StringSlice(fns))
for _, b := range fns {
app.Printf("%s\n", b)
}
return
}
in := app.In()
if len(args) != 0 {
in = make(chan interface{}, 1)
in <- []byte(strings.Join(args, " ") + "\n")
close(in)
}
in = app.Lines(in)
app.Exits(x.xp(in))
}