Ejemplo 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);
        }
Ejemplo n.º 2
0
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable) {
            if (name == "__class__" && _classVariable != null) {
                // 3.x has a cell var called __class__ which can be bound by inner scopes
                variable = _classVariable;
                return true;
            }

            return base.TryBindOuter(from, name, allowGlobals, out variable);
        }
Ejemplo n.º 3
0
 // ImportStatement
 public override bool Walk(ImportStatement node) {
     PythonVariable[] variables = new PythonVariable[node.Names.Count];
     PythonReference[] references = null;
     if (_bindRefs) {
         references = new PythonReference[variables.Length];
     }
     for (int 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, _bindRefs, references);
     return true;
 }
Ejemplo n.º 4
0
 public ClosureInfo(PythonVariable variable, bool accessedInScope)
 {
     Variable        = variable;
     AccessedInScope = accessedInScope;
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
 private void SetInitialized(PythonVariable /*!*/ variable, bool value)
 {
     _bits.Set(variable.Index * 2 + 1, value);
 }
Ejemplo n.º 7
0
 public bool TryGetVariable(string name, out PythonVariable variable) {
     if (_variables != null && name != null) {
         return _variables.TryGetValue(name, out variable);
     } else {
         variable = null;
         return false;
     }
 }
Ejemplo n.º 8
0
        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 (ScopeStatement scope = from.Parent; scope != this; scope = scope.Parent) {
                        scope.AddFreeVariable(variable, false);
                    }

                    AddCellVariable(variable);
                } else if(allowGlobals) {
                    from.AddReferencedGlobal(name);
                }
                return true;
            }
            return false;
        }
Ejemplo n.º 9
0
        internal void AddCellVariable(PythonVariable variable) {
            if (_cellVars == null) {
                _cellVars = new List<string>();
            }

            if (!_cellVars.Contains(variable.Name)) {
                _cellVars.Add(variable.Name);
            }
        }
Ejemplo n.º 10
0
 internal abstract bool ExposesLocalVariable(PythonVariable variable);
Ejemplo n.º 11
0
        internal void AddFreeVariable(PythonVariable variable, bool accessedInScope) {
            if (_freeVars == null) {
                _freeVars = new List<PythonVariable>();
            }

            if (!_freeVars.Contains(variable)) {
                _freeVars.Add(variable);
            }
        }
Ejemplo n.º 12
0
 private bool IsInitialized(PythonVariable /*!*/ variable)
 {
     return(_bits.Get(variable.Index * 2 + 1));
 }
Ejemplo n.º 13
0
 private bool IsAssigned(PythonVariable /*!*/ variable)
 {
     return(_bits.Get(variable.Index * 2));
 }
Ejemplo n.º 14
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            foreach (NameExpression nameNode in node.Names)
            {
                string n = nameNode.Name;
                if (n == null)
                {
                    continue;
                }

                PythonVariable conflict;
                // Check current scope for conflicting variable
                bool assignedGlobal = false;
                if (_currentScope.TryGetVariable(n, out conflict))
                {
                    // conflict?
                    switch (conflict.Kind)
                    {
                    case VariableKind.Global:
                    case VariableKind.Local:
                        assignedGlobal = true;
                        ReportSyntaxWarning(
                            String.Format(
                                System.Globalization.CultureInfo.InvariantCulture,
                                "name '{0}' is assigned to before global declaration",
                                n
                                ),
                            node
                            );
                        break;

                    case VariableKind.Parameter:
                        ReportSyntaxError(
                            String.Format(
                                System.Globalization.CultureInfo.InvariantCulture,
                                "Name '{0}' is a function parameter and declared global",
                                n),
                            node);
                        break;
                    }
                }

                // Check for the name being referenced previously. If it has been, issue warning.
                if (_currentScope.IsReferenced(n) && !assignedGlobal)
                {
                    ReportSyntaxWarning(
                        String.Format(
                            System.Globalization.CultureInfo.InvariantCulture,
                            "name '{0}' is used prior to global declaration",
                            n),
                        node);
                }


                // Create the variable in the global context and mark it as global
                PythonVariable variable = _globalScope.EnsureGlobalVariable(n);
                variable.Kind = VariableKind.Global;

                if (conflict == null)
                {
                    // no previously definied variables, add it to the current scope
                    _currentScope.AddVariable(variable);
                }

                nameNode.AddVariableReference(_globalScope, _bindRefs, Reference(n));
            }
            return(true);
        }
Ejemplo n.º 15
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;
 }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        private static bool HasClosureVariable(List<ClosureInfo> closureVariables, PythonVariable variable) {
            if (closureVariables == null) {
                return false;
            }

            for (int i = 0; i < closureVariables.Count; i++) {
                if (closureVariables[i].Variable == variable) {
                    return true;
                }
            }

            return false;
        }
Ejemplo n.º 18
0
 internal override bool ExposesLocalVariable(PythonVariable variable)
 {
     return(NeedsLocalsDictionary);
 }
Ejemplo n.º 19
0
 internal void AddVariable(PythonVariable variable) {
     EnsureVariables();
     _variables[variable.Name] = variable;
 }
Ejemplo n.º 20
0
        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;
        }
Ejemplo n.º 21
0
 internal PythonVariable/*!*/ CreateVariable(string name, VariableKind kind) {
     EnsureVariables();
     PythonVariable variable;
     _variables[name] = variable = new PythonVariable(name, kind, this);
     return variable;
 }
Ejemplo n.º 22
0
 internal abstract bool ExposesLocalVariable(PythonVariable variable);
Ejemplo n.º 23
0
 public ClosureInfo(PythonVariable variable, bool accessedInScope) {
     Variable = variable;
     AccessedInScope = accessedInScope;
 }
Ejemplo n.º 24
0
 internal void AddVariable(PythonVariable variable)
 {
     EnsureVariables();
     _variables[variable.Name] = variable;
 }
Ejemplo n.º 25
0
 private bool ReadBeforeInitialized(PythonVariable variable) {
     return _readBeforeInitialized.Contains(variable);
 }
Ejemplo n.º 26
0
 // FromImportStatement
 public override bool Walk(FromImportStatement node) {
     if (node.Names.Count != 1 || node.Names[0].Name !="*") {
         PythonVariable[] variables = new PythonVariable[node.Names.Count];
         PythonReference[] references = null;
         if (_bindRefs) {
             references = new PythonReference[node.Names.Count];
         }
         for (int i = 0; i < node.Names.Count; i++) {
             variables[i] = DefineName(node.AsNames[i] != null ? node.AsNames[i].Name : node.Names[i].Name);
             if (references != null) {
                 references[i] = Reference(variables[i].Name);
             }
         }
         node.Variables = variables;
         node.AddVariableReference(_ast, _bindRefs, references);
     } else {
         Debug.Assert(_currentScope != null);
         _currentScope.ContainsImportStar = true;
         _currentScope.NeedsLocalsDictionary = true;
         _currentScope.HasLateBoundVariableSets = true;
     }
     return true;
 }
Ejemplo n.º 27
0
 private void SetAssigned(PythonVariable/*!*/ variable, bool value) {
     _bits.Set(_variableIndices[variable] * 2, value);
 }
Ejemplo n.º 28
0
 internal override bool ExposesLocalVariable(PythonVariable variable) {
     return true;
 }
Ejemplo n.º 29
0
 private void SetInitialized(PythonVariable/*!*/ variable, bool value) {
     _bits.Set(_variableIndices[variable] * 2 + 1, value);
 }
Ejemplo n.º 30
0
 private static int CompareVariables(PythonVariable left, PythonVariable right) {
     return String.Compare(left.Name, right.Name);
 }
Ejemplo n.º 31
0
 internal bool IsAssigned(PythonVariable/*!*/ variable) {
     return _bits.Get(_variableIndices[variable] * 2);
 }
Ejemplo n.º 32
0
 internal override bool ExposesLocalVariable(PythonVariable variable)
 {
     return(true);
 }
Ejemplo n.º 33
0
 internal bool IsInitialized(PythonVariable/*!*/ variable) {
     return _bits.Get(_variableIndices[variable] * 2 + 1);
 }
Ejemplo n.º 34
0
 internal override bool ExposesLocalVariable(PythonVariable variable) {
     return NeedsLocalsDictionary; 
 }
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable)
        {
            if (name == "__class__" && _classVariable != null)
            {
                // 3.x has a cell var called __class__ which can be bound by inner scopes
                variable = _classVariable;
                return(true);
            }

            return(base.TryBindOuter(from, name, allowGlobals, out variable));
        }
Ejemplo n.º 36
0
 public static void AddVariable(this Parameter node, PythonAst ast, bool bindNames, PythonVariable variable)
 {
     if (bindNames)
     {
         ast.SetAttribute(node, Variable, variable);
     }
 }
Ejemplo n.º 37
0
 private bool ReadFromExtractedCode(PythonVariable variable) {
     return _readBeforeInitialized.Contains(variable) &&
         _inputCollector._allReadVariables.Contains(variable);
 }
Ejemplo n.º 38
0
        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 (ScopeStatement scope = from.Parent; scope != this; scope = scope.Parent)
                    {
                        scope.AddFreeVariable(variable, false);
                    }

                    AddCellVariable(variable);
                }
                else if (allowGlobals)
                {
                    from.AddReferencedGlobal(name);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 39
0
 private void SetAssigned(PythonVariable /*!*/ variable, bool value)
 {
     _bits.Set(variable.Index * 2, value);
 }