2016-01-23 01:05:15 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2016-01-23 01:05:15 +08:00
|
|
|
)
|
|
|
|
|
2016-01-23 07:56:09 +08:00
|
|
|
func onePrimary(cn *parse.Compound) *parse.Primary {
|
2016-02-03 23:36:12 +08:00
|
|
|
if len(cn.Indexings) == 1 && len(cn.Indexings[0].Indicies) == 0 {
|
|
|
|
return cn.Indexings[0].Head
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
2016-01-23 07:56:09 +08:00
|
|
|
return nil
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
|
|
|
|
2016-01-23 07:56:09 +08:00
|
|
|
func oneString(cn *parse.Compound) (string, bool) {
|
|
|
|
pn := onePrimary(cn)
|
|
|
|
if pn != nil {
|
|
|
|
switch pn.Type {
|
|
|
|
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
|
|
|
|
return pn.Value, true
|
|
|
|
}
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
2016-01-23 07:56:09 +08:00
|
|
|
return "", false
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// mustString musts that a Compound contains exactly one Primary of type
|
|
|
|
// Variable.
|
2016-01-23 07:56:09 +08:00
|
|
|
func mustString(cp *compiler, cn *parse.Compound, msg string) string {
|
|
|
|
s, ok := oneString(cn)
|
|
|
|
if !ok {
|
2020-03-29 04:34:52 +08:00
|
|
|
cp.errorpf(cn, msg)
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
2016-01-23 07:56:09 +08:00
|
|
|
return s
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|