diff options
author | alandonovan <adonovan@google.com> | 2018-03-13 10:59:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-13 10:59:24 -0400 |
commit | a1b28d86a00778631c18367411127e0870e33446 (patch) | |
tree | 66ab321f857ff36e59bf97e41fa5b2fdaf523cac /syntax/syntax.go | |
parent | 3705afa472e466b8b061cce44b47c9ddc6db696d (diff) | |
download | starlark-go-a1b28d86a00778631c18367411127e0870e33446.tar.gz |
Remove predeclared names from the module globals dict (#86)
* Remove predeclared names from the module globals dict
Previously, the Skylark spec and Go implementation had a notion
of "predeclared globals", which are module globals that have a value
before execution begins, such as the 'glob' function in
the Bazel build language. This is undesirable because it causes
every module to appear to define 'glob', and it makes this name
accessible to other modules through the load statement.
This CL changes the spec and implementation to match the Bazel behavior:
predeclared names like glob are no longer part of the module's globals dictionary,
but are treated similar to universal names such as None and len.
From the Skylark program's perspective, there is no difference
between per-module predeclared names and universal
predeclared names, and the spec now uses only the term "predeclared".
However, the resolver distinguishes them as an optimization as this allows
the evaluator to use separate dictionaries to represent the
universal and the per-module portions.
API CHANGE: The ExecFile function has changed, and so must all its callers.
Previously, it accepted the globals dictionary as a parameter,
populated with predeclared names, and execution would add more
names to this dictionary. Now, it accepts the predeclared
dictionary (which it does not modify), and returns the globals
dictionary.
Also: the resolver now assigns an index to each global,
allowing the evaluator to use a flat representation for
globals similar to that for locals.
Fixes #75
(See also Google internal issue b/74460309)
Diffstat (limited to 'syntax/syntax.go')
-rw-r--r-- | syntax/syntax.go | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/syntax/syntax.go b/syntax/syntax.go index 3440b64..3480c66 100644 --- a/syntax/syntax.go +++ b/syntax/syntax.go @@ -71,7 +71,8 @@ type File struct { Stmts []Stmt // set by resolver: - Locals []*Ident // this file's (comprehension-)local variables + Locals []*Ident // this file's (comprehension-)local variables + Globals []*Ident // this file's global variables } func (x *File) Span() (start, end Position) { @@ -259,8 +260,8 @@ type Ident struct { // set by resolver: - Scope uint8 // one of resolve.{Undefined,Local,Free,Global,Builtin} - Index int // index into enclosing {DefStmt,File}.Locals (if scope==Local) or DefStmt.FreeVars (if scope==Free) + Scope uint8 // see type resolve.Scope + Index int // index into enclosing {DefStmt,File}.Locals (if scope==Local) or DefStmt.FreeVars (if scope==Free) or File.Globals (if scope==Global) } func (x *Ident) Span() (start, end Position) { |