Esempio n. 1
0
        // ImportStatement
        public override bool Walk(ImportStatement node)
        {
            var variables = new PythonVariable[node.Names.Count];

            PythonReference[] references = null;
            if (BindReferences)
            {
                references = new PythonReference[variables.Length];
            }
            for (var i = 0; i < node.Names.Count; i++)
            {
                string name;

                if (node.AsNames[i] != null)
                {
                    name = node.AsNames[i].Name;
                }
                else if (node.Names[i].Names.Count > 0)
                {
                    name = node.Names[i].Names[0].Name;
                }
                else
                {
                    name = null;
                }

                if (name != null)
                {
                    variables[i] = DefineName(name);
                    if (references != null)
                    {
                        references[i] = Reference(name);
                    }
                }
            }
            node.Variables = variables;
            node.AddVariableReference(_ast, BindReferences, references);
            return(true);
        }
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable)
        {
            if (allowGlobals)
            {
                // Unbound variable
                from.AddReferencedGlobal(name);

                if (from.HasLateBoundVariableSets)
                {
                    // If the context contains unqualified exec, new locals can be introduced
                    // Therefore we need to turn this into a fully late-bound lookup which
                    // happens when we don't have a PythonVariable.
                    variable = null;
                    return(false);
                }
                else
                {
                    // Create a global variable to bind to.
                    variable = EnsureGlobalVariable(name);
                    return(true);
                }
            }
            variable = null;
            return(false);
        }
 internal override bool ExposesLocalVariable(PythonVariable variable) => true;
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable)
        {
            // Functions expose their locals to direct access
            ContainsNestedFreeVariables = true;
            if (TryGetVariable(name, out variable))
            {
                variable.AccessedInNestedScope = true;

                if (variable.Kind == VariableKind.Local || variable.Kind == VariableKind.Parameter)
                {
                    from.AddFreeVariable(variable, true);

                    for (var scope = from.Parent; scope != this; scope = scope.Parent)
                    {
                        scope.AddFreeVariable(variable, false);
                    }

                    AddCellVariable(variable);
                }
                else if (allowGlobals)
                {
                    from.AddReferencedGlobal(name);
                }
                return(true);
            }
            return(false);
        }
 internal override bool ExposesLocalVariable(PythonVariable variable) => NeedsLocalsDictionary;
Esempio n. 6
0
 public ClosureInfo(PythonVariable variable, bool accessedInScope)
 {
     Variable        = variable;
     AccessedInScope = accessedInScope;
 }
Esempio n. 7
0
 internal void AddVariable(PythonVariable variable)
 {
     EnsureVariables();
     Variables[variable.Name] = variable;
 }
Esempio n. 8
0
 internal virtual bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable)
 {
     // Hide scope contents by default (only functions expose their locals)
     variable = null;
     return(false);
 }
Esempio n. 9
0
 internal abstract bool ExposesLocalVariable(PythonVariable variable);