pkg/eval: Allow "var a:".

This commit is contained in:
Qi Xiao 2021-01-17 01:17:35 +00:00
parent 76c23aa7b0
commit a16cd8dfa6
2 changed files with 9 additions and 1 deletions

View File

@ -90,7 +90,7 @@ func compileVar(cp *compiler, fn *parse.Form) effectOp {
}
name := pn.Value
if strings.Contains(name, NsSuffix) {
if !IsUnqualified(name) {
cp.errorpf(cn, "variable declared in var must be unqualified")
}
sigil, name := SplitSigil(name)
@ -108,6 +108,12 @@ func compileVar(cp *compiler, fn *parse.Form) effectOp {
return nopOp{}
}
// IsUnqualified returns whether name is an unqualified variable name.
func IsUnqualified(name string) bool {
i := strings.IndexByte(name, ':')
return i == -1 || i == len(name)-1
}
// SetForm = 'set' { LHS } '=' { Compound }
func compileSet(cp *compiler, fn *parse.Form) effectOp {
eq := -1

View File

@ -20,6 +20,8 @@ func TestVar(t *testing.T) {
That("var x", "put $x").Puts(nil),
// Declaring one variable whose name needs to be quoted
That("var 'a/b'", "put $'a/b'").Puts(nil),
// Declaring one variable whose name ends in ":".
That("var a:").DoesNothing(),
// Declaring multiple variables
That("var x y", "put $x $y").Puts(nil, nil),
// Declaring one variable with initial value