Skip to content

Commit

Permalink
expand: join NameRef with Kind in Variable
Browse files Browse the repository at this point in the history
Nameref variables always contain strings, so it's more appropriate to
describe them with a value kind instead of a boolean attribute applying
to all value kinds.

This also means we can fit Kind in the space left by the removed
boolean, recovering about a third of the extra weight added to Variable
by the previous commit.

name   old time/op    new time/op    delta
Run-8    1.45ms ± 1%    1.45ms ± 1%    ~     (p=0.937 n=6+6)

name   old alloc/op   new alloc/op   delta
Run-8    81.5kB ± 0%    80.8kB ± 0%  -0.88%  (p=0.002 n=6+6)

name   old allocs/op  new allocs/op  delta
Run-8       882 ± 0%       881 ± 0%    ~     (p=0.182 n=6+6)
  • Loading branch information
mvdan committed Dec 22, 2018
1 parent 1bf6411 commit 1a53a43
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions expand/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ type WriteEnviron interface {
Set(name string, vr Variable) error
}

type ValueKind int
type ValueKind uint8

const (
Unset ValueKind = iota
String
NameRef
Indexed
Associative
)
Expand All @@ -68,11 +69,10 @@ type Variable struct {
Local bool
Exported bool
ReadOnly bool
NameRef bool // if true, Value must be string

Kind ValueKind

Str string // Used when Kind is String.
Str string // Used when Kind is String or NameRef.
List []string // Used when Kind is Indexed.
Map map[string]string // Used when Kind is Associative.
}
Expand Down Expand Up @@ -109,7 +109,7 @@ const maxNameRefDepth = 100
func (v Variable) Resolve(env Environ) (string, Variable) {
name := ""
for i := 0; i < maxNameRefDepth; i++ {
if !v.NameRef {
if v.Kind != NameRef {
return name, v
}
name = v.Str // keep name for the next iteration
Expand Down
2 changes: 1 addition & 1 deletion expand/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (cfg *Config) paramExp(pe *syntax.ParamExp) (string, error) {
var strs []string
if pe.Names != 0 {
strs = cfg.namesByPrefix(pe.Param.Value)
} else if orig.NameRef {
} else if orig.Kind == NameRef {
strs = append(strs, orig.Str)
} else if vr.Kind == Indexed {
for i, e := range vr.List {
Expand Down
8 changes: 3 additions & 5 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,13 +883,13 @@ func (r *Runner) cmd(ctx context.Context, cm syntax.Command) {
case "readonly":
modes = append(modes, "-r")
case "nameref":
modes = append(modes, "-n")
valType = "-n"
}
for _, opt := range x.Opts {
switch s := r.literal(opt); s {
case "-x", "-r", "-n":
case "-x", "-r":
modes = append(modes, s)
case "-a", "-A":
case "-a", "-A", "-n":
valType = s
case "-g":
global = true
Expand Down Expand Up @@ -919,8 +919,6 @@ func (r *Runner) cmd(ctx context.Context, cm syntax.Command) {
vr.Exported = true
case "-r":
vr.ReadOnly = true
case "-n":
vr.NameRef = true
}
}
r.setVar(name, as.Index, vr)
Expand Down
3 changes: 2 additions & 1 deletion interp/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"golang.org/x/crypto/ssh/terminal"

"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/syntax"
)

Expand Down Expand Up @@ -175,7 +176,7 @@ func (r *Runner) unTest(ctx context.Context, op syntax.UnTestOperator, x string)
case syntax.TsVarSet:
return r.lookupVar(x).IsSet()
case syntax.TsRefVar:
return r.lookupVar(x).NameRef
return r.lookupVar(x).Kind == expand.NameRef
case syntax.TsNot:
return x == ""
default:
Expand Down
8 changes: 6 additions & 2 deletions interp/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ func (r *Runner) setVar(name string, index syntax.ArithmExpr, vr expand.Variable
if name2, var2 := cur.Resolve(r.Env); name2 != "" {
name = name2
cur = var2
vr.NameRef = false
cur.NameRef = false
}

if vr.Kind == expand.String && index == nil {
Expand Down Expand Up @@ -246,6 +244,9 @@ func (r *Runner) assignVal(as *syntax.Assign, valType string) expand.Variable {
s := r.literal(as.Value)
if !as.Append || !prev.IsSet() {
prev.Kind = expand.String
if valType == "-n" {
prev.Kind = expand.NameRef
}
prev.Str = s
return prev
}
Expand All @@ -265,6 +266,9 @@ func (r *Runner) assignVal(as *syntax.Assign, valType string) expand.Variable {
if as.Array == nil {
// don't return the zero value, as that's an unset variable
prev.Kind = expand.String
if valType == "-n" {
prev.Kind = expand.NameRef
}
prev.Str = ""
return prev
}
Expand Down

0 comments on commit 1a53a43

Please sign in to comment.