-
Notifications
You must be signed in to change notification settings - Fork 77
/
op_defer.go
57 lines (44 loc) · 1.45 KB
/
op_defer.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
package spruce
import (
"fmt"
"strings"
"github.com/starkandwayne/goutils/tree"
. "github.com/geofffranks/spruce/log"
)
// DeferOperator sheds the "defer" command off of (( defer args args args )) and
// leaves (( args args args ))
type DeferOperator struct{}
// Setup doesn't do anything for Defer. We're a pretty lightweight operator.
func (DeferOperator) Setup() error {
return nil
}
// Phase gives back Param phase in this case, because we don't want any
// following phases to pick up the operator post-deference
func (DeferOperator) Phase() OperatorPhase {
return EvalPhase
}
// Dependencies returns an empty slice - defer produces no deps at all.
func (DeferOperator) Dependencies(_ *Evaluator, _ []*Expr, _ []*tree.Cursor, _ []*tree.Cursor) []*tree.Cursor {
return nil
}
// Run chops off "defer" and leaves the args in double parens. Need to
// reconstruct the operator string
func (DeferOperator) Run(_ *Evaluator, args []*Expr) (*Response, error) {
DEBUG("Running defer operator...")
if len(args) == 0 {
return nil, fmt.Errorf("Defer has no arguments - what are you deferring?")
}
components := []string{"(("} //Join these with spaces at the end
for _, arg := range args {
components = append(components, arg.String())
}
components = append(components, "))")
DEBUG("Returning from defer operator")
return &Response{
Type: Replace,
Value: strings.Join(components, " "),
}, nil
}
func init() {
RegisterOp("defer", DeferOperator{})
}