-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathcompile_op.go
575 lines (519 loc) · 12.9 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package eval
import (
"errors"
"fmt"
"os"
"sync"
"github.com/elves/elvish/eval/vals"
"github.com/elves/elvish/eval/vars"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
"github.com/xiaq/persistent/hashmap"
)
// Op is an operation on an Frame.
type Op struct {
Body OpBody
Begin, End int
}
// OpBody is the body of an Op.
type OpBody interface {
Invoke(*Frame) error
}
// Exec executes an Op.
func (op Op) Exec(fm *Frame) error {
fm.begin, fm.end = op.Begin, op.End
return op.Body.Invoke(fm)
}
func (cp *compiler) chunk(n *parse.Chunk) OpBody {
return chunkOp{cp.pipelineOps(n.Pipelines)}
}
type chunkOp struct {
subops []Op
}
func (op chunkOp) Invoke(fm *Frame) error {
for _, subop := range op.subops {
err := subop.Exec(fm)
if err != nil {
return err
}
}
// 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.
if fm.IsInterrupted() {
return ErrInterrupted
}
return nil
}
func (cp *compiler) pipeline(n *parse.Pipeline) OpBody {
return &pipelineOp{n.Background, n.SourceText(), cp.formOps(n.Forms)}
}
type pipelineOp struct {
bg bool
source string
subops []Op
}
const pipelineChanBufferSize = 32
func (op *pipelineOp) Invoke(fm *Frame) error {
if fm.IsInterrupted() {
return ErrInterrupted
}
if op.bg {
fm = fm.fork("background job" + op.source)
fm.intCh = nil
fm.background = true
fm.Evaler.numBgJobs++
if fm.Editor != nil {
// TODO: Redirect output in interactive mode so that the line
// editor does not get messed up.
}
}
nforms := len(op.subops)
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 op.subops {
hasChanInput := i > 0
newFm := fm.fork("[form op]")
if i > 0 {
newFm.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 {
return fmt.Errorf("failed to create pipe: %s", e)
}
ch := make(chan interface{}, pipelineChanBufferSize)
newFm.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 := newFm.Eval(thisOp)
newFm.Close()
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 newFm.ports[0].Chan {
}
}
}()
}
if op.bg {
// Background job, wait for form termination asynchronously.
go func() {
wg.Wait()
fm.Evaler.numBgJobs--
msg := "job " + op.source + " finished"
err := ComposeExceptionsFromPipeline(errors)
if err != nil {
msg += ", errors = " + err.Error()
}
if fm.Evaler.notifyBgJobSuccess || err != nil {
if fm.Editor != nil {
fm.Editor.Notify("%s", msg)
} else {
fm.ports[2].File.WriteString(msg + "\n")
}
}
}()
return nil
} else {
wg.Wait()
return ComposeExceptionsFromPipeline(errors)
}
}
func (cp *compiler) form(n *parse.Form) OpBody {
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 seqOp{assignmentOps}
}
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")
}
// Depending on the type of the form, exactly one of the three below will be
// set.
var (
specialOpFunc OpBody
headOp ValuesOp
spaceyAssignOp Op
)
// Forward declaration; needed when compiling assignment forms.
var argOps []ValuesOp
if n.Head != nil {
headStr, ok := oneString(n.Head)
if ok {
compileForm, ok := builtinSpecials[headStr]
if ok {
// Special form.
specialOpFunc = compileForm(cp, n)
} else {
var headOpFunc ValuesOpBody
explode, ns, name := ParseVariableRef(headStr)
if !explode && cp.registerVariableGet(ns, name+FnSuffix) {
// $head~ resolves.
headOpFunc = variableOp{false, ns, name + FnSuffix}
} else {
// Fall back to $e:head~.
headOpFunc = literalValues(ExternalCmd{headStr})
}
headOp = ValuesOp{headOpFunc, n.Head.Begin(), n.Head.End()}
}
} else {
// Head exists and is not a literal string. Evaluate as a normal
// expression.
headOp = cp.compoundOp(n.Head)
}
} else {
// Assignment form.
varsOp, restOp := cp.lvaluesMulti(n.Vars)
// This cannot be replaced with newSeqValuesOp as it depends on the fact
// that argOps will be changed later.
argsOp := ValuesOp{
funcValuesOp(func(fm *Frame) ([]interface{}, error) {
var values []interface{}
for _, op := range argOps {
moreValues, err := op.Exec(fm)
if err != nil {
return nil, err
}
values = append(values, moreValues...)
}
return values, nil
}), -1, -1}
if len(argOps) > 0 {
argsOp.Begin = argOps[0].Begin
argsOp.End = argOps[len(argOps)-1].End
}
spaceyAssignOp = Op{
&assignmentOp{varsOp, restOp, argsOp},
n.Begin(), argsOp.End,
}
}
argOps = cp.compoundOps(n.Args)
optsOp := cp.mapPairs(n.Opts)
redirOps := cp.redirOps(n.Redirs)
// TODO: n.ErrorRedir
return &formOp{saveVarsOps, assignmentOps, redirOps, specialOpFunc, headOp, argOps, optsOp, spaceyAssignOp, n.Begin(), n.End()}
}
type formOp struct {
saveVarsOps []LValuesOp
assignmentOps []Op
redirOps []Op
specialOpBody OpBody
headOp ValuesOp
argOps []ValuesOp
optsOp ValuesOpBody
spaceyAssignOp Op
begin, end int
}
func (op *formOp) Invoke(fm *Frame) (errRet error) {
// ec here is always a subevaler created in compiler.pipeline, so it can
// be safely modified.
// Temporary assignment.
if len(op.saveVarsOps) > 0 {
// There is a temporary assignment.
// Save variables.
var saveVars []vars.Var
var saveVals []interface{}
for _, op := range op.saveVarsOps {
moreSaveVars, err := op.Exec(fm)
if err != nil {
return err
}
saveVars = append(saveVars, moreSaveVars...)
}
for i, v := range saveVars {
// XXX(xiaq): If the variable to save is a elemVariable, save
// the outermost variable instead.
if u := vars.HeadOfElement(v); u != nil {
v = u
saveVars[i] = v
}
val := v.Get()
saveVals = append(saveVals, val)
logger.Printf("saved %s = %s", v, val)
}
// Do assignment.
for _, subop := range op.assignmentOps {
err := subop.Exec(fm)
if err != nil {
return err
}
}
// 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 = ""
}
err := v.Set(val)
if err != nil {
errRet = err
}
logger.Printf("restored %s = %s", v, val)
}
}()
}
// redirs
for _, redirOp := range op.redirOps {
err := redirOp.Exec(fm)
if err != nil {
return err
}
}
if op.specialOpBody != nil {
return op.specialOpBody.Invoke(fm)
}
var headFn Callable
var args []interface{}
if op.headOp.Body != nil {
// head
headFn = fm.ExecAndUnwrap("head of command", op.headOp).One().Callable()
// args
for _, argOp := range op.argOps {
moreArgs, err := argOp.Exec(fm)
if err != nil {
return err
}
args = append(args, moreArgs...)
}
}
// opts
// XXX This conversion should be avoided.
optValues, err := op.optsOp.Invoke(fm)
if err != nil {
return err
}
opts := optValues[0].(hashmap.Map)
convertedOpts := make(map[string]interface{})
for it := opts.Iterator(); it.HasElem(); it.Next() {
k, v := it.Elem()
if ks, ok := k.(string); ok {
convertedOpts[ks] = v
} else {
return fmt.Errorf("Option key must be string, got %s", vals.Kind(k))
}
}
fm.begin, fm.end = op.begin, op.end
if headFn != nil {
return headFn.Call(fm, args, convertedOpts)
} else {
return op.spaceyAssignOp.Exec(fm)
}
}
func allTrue(vs []interface{}) bool {
for _, v := range vs {
if !vals.Bool(v) {
return false
}
}
return true
}
func (cp *compiler) assignment(n *parse.Assignment) OpBody {
variablesOp, restOp := cp.lvaluesOp(n.Left)
valuesOp := cp.compoundOp(n.Right)
return &assignmentOp{variablesOp, restOp, valuesOp}
}
// ErrMoreThanOneRest is returned when the LHS of an assignment contains more
// than one rest variables.
var ErrMoreThanOneRest = errors.New("more than one @ lvalue")
type assignmentOp struct {
variablesOp LValuesOp
restOp LValuesOp
valuesOp ValuesOp
}
func (op *assignmentOp) Invoke(fm *Frame) (errRet error) {
variables, err := op.variablesOp.Exec(fm)
if err != nil {
return err
}
rest, err := op.restOp.Exec(fm)
if err != nil {
return err
}
// 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, &errRet)
defer fixNilVariables(rest, &errRet)
values, err := op.valuesOp.Exec(fm)
if err != nil {
return err
}
if len(rest) > 1 {
return ErrMoreThanOneRest
}
if len(rest) == 1 {
if len(variables) > len(values) {
return ErrArityMismatch
}
} else {
if len(variables) != len(values) {
return ErrArityMismatch
}
}
for i, variable := range variables {
err := variable.Set(values[i])
if err != nil {
return err
}
}
if len(rest) == 1 {
err := rest[0].Set(vals.MakeList(values[len(variables):]...))
if err != nil {
return err
}
}
return nil
}
func fixNilVariables(vs []vars.Var, perr *error) {
for _, v := range vs {
if vars.IsBlackhole(v) {
continue
}
if v.Get() == nil {
err := v.Set("")
*perr = util.Errors(*perr, err)
}
}
}
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) OpBody {
var dstOp ValuesOp
if n.Left != nil {
dstOp = cp.compoundOp(n.Left)
}
flag := makeFlag(n.Mode)
if flag == -1 {
// TODO: Record and get redirection sign position
cp.errorf("bad redirection sign")
}
return &redirOp{dstOp, cp.compoundOp(n.Right), n.RightIsFd, n.Mode, flag}
}
type redirOp struct {
dstOp ValuesOp
srcOp ValuesOp
srcIsFd bool
mode parse.RedirMode
flag int
}
func (op *redirOp) Invoke(fm *Frame) error {
var dst int
if op.dstOp.Body == nil {
// use default dst fd
switch op.mode {
case parse.Read:
dst = 0
case parse.Write, parse.ReadWrite, parse.Append:
dst = 1
default:
panic("bad RedirMode; parser bug")
}
} else {
// dst must be a valid fd
dst = fm.ExecAndUnwrap("Fd", op.dstOp).One().NonNegativeInt()
}
fm.growPorts(dst + 1)
// Logger.Printf("closing old port %d of %s", dst, ec.context)
fm.ports[dst].Close()
srcUnwrap := fm.ExecAndUnwrap("redirection source", op.srcOp).One()
if op.srcIsFd {
src := srcUnwrap.FdOrClose()
if src == -1 {
// close
fm.ports[dst] = &Port{}
} else {
fm.ports[dst] = fm.ports[src].Fork()
}
} else {
switch src := srcUnwrap.Any().(type) {
case string:
f, err := os.OpenFile(src, op.flag, defaultFileRedirPerm)
if err != nil {
return fmt.Errorf("failed to open file %s: %s", vals.Repr(src, vals.NoPretty), err)
}
fm.ports[dst] = &Port{
File: f, Chan: BlackholeChan,
CloseFile: true,
}
case vals.File:
fm.ports[dst] = &Port{
File: src.Inner, Chan: BlackholeChan,
CloseFile: false,
}
case vals.Pipe:
var f *os.File
switch op.mode {
case parse.Read:
f = src.ReadEnd
case parse.Write:
f = src.WriteEnd
default:
return errors.New("can only use < or > with pipes")
}
fm.ports[dst] = &Port{
File: f, Chan: BlackholeChan,
CloseFile: false,
}
default:
srcUnwrap.error("string or file", "%s", vals.Kind(src))
}
}
return nil
}
type seqOp struct{ subops []Op }
func (op seqOp) Invoke(fm *Frame) error {
for _, subop := range op.subops {
err := subop.Exec(fm)
if err != nil {
return err
}
}
return nil
}
type funcOp func(*Frame) error
func (op funcOp) Invoke(fm *Frame) error {
return op(fm)
}