-
-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathcompile_op.go
461 lines (416 loc) · 10.7 KB
/
compile_op.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package eval
import (
"os"
"sync"
"github.com/elves/elvish/parse"
)
// Op is an operation on an EvalCtx.
type Op struct {
Func OpFunc
Begin, End int
}
// OpFunc is the body of an Op.
type OpFunc func(*EvalCtx)
// Exec executes an Op.
func (op Op) Exec(ec *EvalCtx) {
ec.begin, ec.end = op.Begin, op.End
op.Func(ec)
}
func (cp *compiler) chunk(n *parse.Chunk) OpFunc {
ops := cp.pipelineOps(n.Pipelines)
return func(ec *EvalCtx) {
for _, op := range ops {
op.Exec(ec)
}
// Check for interrupts after the chunk.
// We also check for interrupts before each pipeline, so there is no
// need to check it before the chunk or after each pipeline.
ec.CheckInterrupts()
}
}
const pipelineChanBufferSize = 32
func (cp *compiler) pipeline(n *parse.Pipeline) OpFunc {
ops := cp.formOps(n.Forms)
return func(ec *EvalCtx) {
ec.CheckInterrupts()
bg := n.Background
if bg {
ec = ec.fork("background job " + n.SourceText())
ec.intCh = nil
ec.background = true
if ec.Editor != nil {
// TODO: Redirect output in interactive mode so that the line
// editor does not get messed up.
}
}
nforms := len(ops)
var wg sync.WaitGroup
wg.Add(nforms)
errors := make([]*Exception, nforms)
var nextIn *Port
// For each form, create a dedicated evalCtx and run asynchronously
for i, op := range ops {
hasChanInput := i > 0
newEc := ec.fork("[form op]")
if i > 0 {
newEc.ports[0] = nextIn
}
if i < nforms-1 {
// Each internal port pair consists of a (byte) pipe pair and a
// channel.
// os.Pipe sets O_CLOEXEC, which is what we want.
reader, writer, e := os.Pipe()
if e != nil {
throwf("failed to create pipe: %s", e)
}
ch := make(chan Value, pipelineChanBufferSize)
newEc.ports[1] = &Port{
File: writer, Chan: ch, CloseFile: true, CloseChan: true}
nextIn = &Port{
File: reader, Chan: ch, CloseFile: true, CloseChan: false}
}
thisOp := op
thisError := &errors[i]
go func() {
err := newEc.PEval(thisOp)
// Logger.Printf("closing ports of %s", newEc.context)
ClosePorts(newEc.ports)
if err != nil {
*thisError = err.(*Exception)
}
wg.Done()
if hasChanInput {
// If the command has channel input, drain it. This
// mitigates the effect of erroneous pipelines like
// "range 100 | cat"; without draining the pipeline will
// lock up.
for range newEc.ports[0].Chan {
}
}
}()
}
if bg {
// Background job, wait for form termination asynchronously.
go func() {
wg.Wait()
msg := "job " + n.SourceText() + " finished"
err := ComposeExceptionsFromPipeline(errors)
if err != nil {
msg += ", errors = " + err.Error()
}
if ec.Editor != nil {
m := ec.Editor.ActiveMutex()
m.Lock()
defer m.Unlock()
if ec.Editor.Active() {
ec.Editor.Notify("%s", msg)
} else {
ec.ports[2].File.WriteString(msg + "\n")
}
} else {
ec.ports[2].File.WriteString(msg + "\n")
}
}()
} else {
wg.Wait()
maybeThrow(ComposeExceptionsFromPipeline(errors))
}
}
}
func (cp *compiler) form(n *parse.Form) OpFunc {
var saveVarsOps []LValuesOp
var assignmentOps []Op
if len(n.Assignments) > 0 {
assignmentOps = cp.assignmentOps(n.Assignments)
if n.Head == nil && n.Vars == nil {
// Permanent assignment.
return func(ec *EvalCtx) {
for _, op := range assignmentOps {
op.Exec(ec)
}
}
}
for _, a := range n.Assignments {
v, r := cp.lvaluesOp(a.Left)
saveVarsOps = append(saveVarsOps, v, r)
}
logger.Println("temporary assignment of", len(n.Assignments), "pairs")
}
var specialOpFunc OpFunc
if n.Head != nil {
headStr, ok := oneString(n.Head)
if ok {
compileForm, ok := builtinSpecials[headStr]
if ok {
// special form
specialOpFunc = compileForm(cp, n)
} else {
// Ignore the output. If a matching function exists it will be
// captured and eventually the Evaler executes it. If not,
// nothing happens here and the Evaler executes an external
// command.
_, ns, name := ParseAndFixVariable(headStr)
cp.registerVariableGet(ns + ":" + FnPrefix + name)
// XXX Dynamic head names should always refer to external
// commands.
}
}
}
argOps := cp.compoundOps(n.Args)
var headOp ValuesOp
var spaceyAssignOp Op
if n.Head != nil {
headOp = cp.compoundOp(n.Head)
} else {
varsOp, restOp := cp.lvaluesMulti(n.Vars)
argsOp := ValuesOp{
func(ec *EvalCtx) []Value {
var vs []Value
for _, op := range argOps {
vs = append(vs, op.Exec(ec)...)
}
return vs
},
-1, -1,
}
if len(argOps) > 0 {
argsOp.Begin = argOps[0].Begin
argsOp.End = argOps[len(argOps)-1].End
}
spaceyAssignOp = Op{
makeAssignmentOpFunc(varsOp, restOp, argsOp),
n.Begin(), argsOp.End,
}
}
optsOp := cp.mapPairs(n.Opts)
redirOps := cp.redirOps(n.Redirs)
// TODO: n.ErrorRedir
begin, end := n.Begin(), n.End()
// ec here is always a subevaler created in compiler.pipeline, so it can
// be safely modified.
return func(ec *EvalCtx) {
// Temporary assignment.
if len(saveVarsOps) > 0 {
// There is a temporary assignment.
// Save variables.
var saveVars []Variable
var saveVals []Value
for _, op := range saveVarsOps {
saveVars = append(saveVars, op.Exec(ec)...)
}
for i, v := range saveVars {
// XXX(xiaq): If the variable to save is a elemVariable, save
// the outermost variable instead.
if elemVar, ok := v.(*elemVariable); ok {
v = elemVar.variable
saveVars[i] = v
}
val := v.Get()
saveVals = append(saveVals, val)
logger.Printf("saved %s = %s", v, val)
}
// Do assignment.
for _, op := range assignmentOps {
op.Exec(ec)
}
// Defer variable restoration. Will be executed even if an error
// occurs when evaling other part of the form.
defer func() {
for i, v := range saveVars {
val := saveVals[i]
if val == nil {
// XXX Old value is nonexistent. We should delete the
// variable. However, since the compiler now doesn't delete
// it, we don't delete it in the evaler either.
val = String("")
}
v.Set(val)
logger.Printf("restored %s = %s", v, val)
}
}()
}
// redirs
for _, redirOp := range redirOps {
redirOp.Exec(ec)
}
if specialOpFunc != nil {
specialOpFunc(ec)
} else {
var headFn Callable
var args []Value
if headOp.Func != nil {
// head
headFn = ec.ExecAndUnwrap("head of command", headOp).One().Callable()
// args
for _, argOp := range argOps {
args = append(args, argOp.Exec(ec)...)
}
}
// opts
// XXX This conversion should be avoided.
opts := optsOp(ec)[0].(Map)
convertedOpts := make(map[string]Value)
opts.IteratePair(func(k, v Value) bool {
if ks, ok := k.(String); ok {
convertedOpts[string(ks)] = v
} else {
throwf("Option key must be string, got %s", k.Kind())
}
return true
})
ec.begin, ec.end = begin, end
if headFn != nil {
headFn.Call(ec, args, convertedOpts)
} else {
spaceyAssignOp.Exec(ec)
}
}
}
}
func allTrue(vs []Value) bool {
for _, v := range vs {
if !ToBool(v) {
return false
}
}
return true
}
func (cp *compiler) assignment(n *parse.Assignment) OpFunc {
variablesOp, restOp := cp.lvaluesOp(n.Left)
valuesOp := cp.compoundOp(n.Right)
return makeAssignmentOpFunc(variablesOp, restOp, valuesOp)
}
func makeAssignmentOpFunc(variablesOp, restOp LValuesOp, valuesOp ValuesOp) OpFunc {
return func(ec *EvalCtx) {
variables := variablesOp.Exec(ec)
rest := restOp.Exec(ec)
// If any LHS ends up being nil, assign an empty string to all of them.
//
// This is to fix #176, which only happens in the top level of REPL; in
// other cases, a failure in the evaluation of the RHS causes this
// level to fail, making the variables unaccessible.
//
// XXX(xiaq): Should think about how to get rid of this.
defer fixNilVariables(variables)
defer fixNilVariables(rest)
values := valuesOp.Exec(ec)
if len(rest) > 1 {
throw(ErrMoreThanOneRest)
}
if len(rest) == 1 {
if len(variables) > len(values) {
throw(ErrArityMismatch)
}
} else {
if len(variables) != len(values) {
throw(ErrArityMismatch)
}
}
for i, variable := range variables {
variable.Set(values[i])
}
if len(rest) == 1 {
rest[0].Set(NewList(values[len(variables):]...))
}
}
}
func fixNilVariables(vs []Variable) {
for _, v := range vs {
if _, isBlackhole := v.(BlackholeVariable); isBlackhole {
continue
}
if v.Get() == nil {
v.Set(String(""))
}
}
}
func (cp *compiler) literal(n *parse.Primary, msg string) string {
switch n.Type {
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
return n.Value
default:
cp.compiling(n)
cp.errorf(msg)
return "" // not reached
}
}
const defaultFileRedirPerm = 0644
// redir compiles a Redir into a op.
func (cp *compiler) redir(n *parse.Redir) OpFunc {
var dstOp ValuesOp
if n.Left != nil {
dstOp = cp.compoundOp(n.Left)
}
srcOp := cp.compoundOp(n.Right)
sourceIsFd := n.RightIsFd
mode := n.Mode
flag := makeFlag(mode)
if flag == -1 {
// TODO: Record and get redirection sign position
cp.errorf("bad redirection sign")
}
return func(ec *EvalCtx) {
var dst int
if dstOp.Func == nil {
// use default dst fd
switch mode {
case parse.Read:
dst = 0
case parse.Write, parse.ReadWrite, parse.Append:
dst = 1
default:
// XXX should report parser bug
panic("bad RedirMode; parser bug")
}
} else {
// dst must be a valid fd
dst = ec.ExecAndUnwrap("Fd", dstOp).One().NonNegativeInt()
}
ec.growPorts(dst + 1)
// Logger.Printf("closing old port %d of %s", dst, ec.context)
ec.ports[dst].Close()
srcUnwrap := ec.ExecAndUnwrap("redirection source", srcOp).One()
if sourceIsFd {
src := srcUnwrap.FdOrClose()
if src == -1 {
// close
ec.ports[dst] = &Port{}
} else {
ec.ports[dst] = ec.ports[src].Fork()
}
} else {
switch src := srcUnwrap.Any().(type) {
case String:
f, err := os.OpenFile(string(src), flag, defaultFileRedirPerm)
if err != nil {
throwf("failed to open file %s: %s", src.Repr(NoPretty), err)
}
ec.ports[dst] = &Port{
File: f, Chan: BlackholeChan,
CloseFile: true,
}
case File:
ec.ports[dst] = &Port{
File: src.inner, Chan: BlackholeChan,
CloseFile: false,
}
case Pipe:
var f *os.File
switch mode {
case parse.Read:
f = src.r
case parse.Write:
f = src.w
default:
cp.errorf("can only use < or > with pipes")
}
ec.ports[dst] = &Port{
File: f, Chan: BlackholeChan,
CloseFile: false,
}
default:
srcUnwrap.error("string or file", "%s", src.Kind())
}
}
}
}