2017-12-18 09:32:04 +08:00
|
|
|
package eval
|
2017-05-30 06:39:40 +08:00
|
|
|
|
|
|
|
import (
|
2017-12-18 09:23:28 +08:00
|
|
|
"errors"
|
2017-05-30 06:39:40 +08:00
|
|
|
"strings"
|
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
"github.com/elves/elvish/eval/types"
|
2017-05-30 06:39:40 +08:00
|
|
|
"github.com/elves/elvish/parse"
|
|
|
|
"github.com/elves/elvish/util"
|
|
|
|
)
|
|
|
|
|
2017-12-18 09:23:28 +08:00
|
|
|
var ErrImpure = errors.New("expression is impure")
|
|
|
|
|
2017-12-18 09:32:04 +08:00
|
|
|
func PurelyEvalCompound(cn *parse.Compound) (string, error) {
|
|
|
|
return (*Evaler)(nil).PurelyEvalCompound(cn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *Evaler) PurelyEvalCompound(cn *parse.Compound) (string, error) {
|
|
|
|
return ev.PurelyEvalPartialCompound(cn, nil)
|
2017-12-18 09:23:28 +08:00
|
|
|
}
|
|
|
|
|
2017-12-18 09:32:04 +08:00
|
|
|
func (ev *Evaler) PurelyEvalPartialCompound(cn *parse.Compound, upto *parse.Indexing) (string, error) {
|
2017-05-30 06:39:40 +08:00
|
|
|
tilde := false
|
|
|
|
head := ""
|
|
|
|
for _, in := range cn.Indexings {
|
|
|
|
if len(in.Indicies) > 0 {
|
2017-12-18 09:23:28 +08:00
|
|
|
return "", ErrImpure
|
2017-05-30 06:39:40 +08:00
|
|
|
}
|
|
|
|
switch in.Head.Type {
|
|
|
|
case parse.Tilde:
|
|
|
|
tilde = true
|
|
|
|
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
|
|
|
|
head += in.Head.Value
|
2017-07-20 18:45:00 +08:00
|
|
|
case parse.Variable:
|
|
|
|
if ev == nil {
|
2017-12-18 09:23:28 +08:00
|
|
|
return "", ErrImpure
|
2017-07-20 18:45:00 +08:00
|
|
|
}
|
2017-12-18 09:32:04 +08:00
|
|
|
v := ev.PurelyEvalPrimary(in.Head)
|
2018-01-02 09:34:09 +08:00
|
|
|
if s, ok := v.(types.String); ok {
|
2017-07-20 18:45:00 +08:00
|
|
|
head += string(s)
|
|
|
|
} else {
|
2017-12-18 09:23:28 +08:00
|
|
|
return "", ErrImpure
|
2017-07-20 18:45:00 +08:00
|
|
|
}
|
2017-05-30 06:39:40 +08:00
|
|
|
default:
|
2017-12-18 09:23:28 +08:00
|
|
|
return "", ErrImpure
|
2017-05-30 06:39:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if in == upto {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tilde {
|
|
|
|
i := strings.Index(head, "/")
|
|
|
|
if i == -1 {
|
|
|
|
i = len(head)
|
|
|
|
}
|
|
|
|
uname := head[:i]
|
|
|
|
home, err := util.GetHome(uname)
|
|
|
|
if err != nil {
|
2017-12-18 09:23:28 +08:00
|
|
|
return "", err
|
2017-05-30 06:39:40 +08:00
|
|
|
}
|
|
|
|
head = home + head[i:]
|
|
|
|
}
|
2017-12-18 09:23:28 +08:00
|
|
|
return head, nil
|
2017-05-30 06:39:40 +08:00
|
|
|
}
|
2017-07-20 18:45:00 +08:00
|
|
|
|
|
|
|
// PurelyEvalPrimary evaluates a primary node without causing any side effects.
|
|
|
|
// If this cannot be done, it returns nil.
|
|
|
|
//
|
|
|
|
// Currently, only string literals and variables with no @ can be evaluated.
|
2018-01-01 04:31:45 +08:00
|
|
|
func (ev *Evaler) PurelyEvalPrimary(pn *parse.Primary) types.Value {
|
2017-07-20 18:45:00 +08:00
|
|
|
switch pn.Type {
|
|
|
|
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
|
2018-01-02 09:34:09 +08:00
|
|
|
return types.String(pn.Value)
|
2017-07-20 18:45:00 +08:00
|
|
|
case parse.Variable:
|
2017-12-18 09:32:04 +08:00
|
|
|
explode, ns, name := ParseVariable(pn.Value)
|
2017-07-20 18:45:00 +08:00
|
|
|
if explode {
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-28 03:59:42 +08:00
|
|
|
ec := NewTopFrame(ev, "[pure eval]", "", nil)
|
2017-07-20 18:45:00 +08:00
|
|
|
variable := ec.ResolveVar(ns, name)
|
2017-10-01 10:47:25 +08:00
|
|
|
if variable != nil {
|
|
|
|
return variable.Get()
|
|
|
|
}
|
2017-07-20 18:45:00 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|