internal override PythonVariable BindReference(PythonNameBinder binder, string name)
        {
            // First try variables local to this scope
            if (TryGetVariable(name, out var variable) && variable.Kind != VariableKind.Nonlocal)
            {
                if (variable.Kind == VariableKind.Global)
                {
                    AddReferencedGlobal(name);
                }
                return(variable);
            }

            // Try to bind in outer scopes
            for (var parent = Parent; parent != null; parent = parent.Parent)
            {
                if (parent.TryBindOuter(this, name, true, out variable))
                {
                    return(variable);
                }
            }

            return(null);
        }
 internal override PythonVariable BindReference(PythonNameBinder binder, string name) => EnsureVariable(name);
 internal override void FinishBind(PythonNameBinder binder)
 {
 }
 internal override void Bind(PythonNameBinder binder)
 {
     base.Bind(binder);
     Verify(binder);
 }
Example #5
0
        internal virtual void FinishBind(PythonNameBinder binder)
        {
            List <ClosureInfo> closureVariables = null;

            if (_nonLocalVars != null)
            {
                foreach (var variableName in _nonLocalVars)
                {
                    var bound = false;
                    for (var parent = Parent; parent != null; parent = parent.Parent)
                    {
                        if (parent.TryBindOuter(this, variableName.Name, false, out var variable))
                        {
                            bound = !variable.IsGlobal;
                            break;
                        }
                    }

                    if (!bound)
                    {
                        binder.ReportSyntaxError("no binding for nonlocal '{0}' found".FormatUI(variableName.Name), variableName);
                    }
                }
            }

            if (FreeVariables != null && FreeVariables.Count > 0)
            {
                closureVariables = new List <ClosureInfo>();

                foreach (var variable in FreeVariables)
                {
                    closureVariables.Add(new ClosureInfo(variable, !(this is ClassDefinition)));
                }
            }

            if (Variables != null && Variables.Count > 0)
            {
                if (closureVariables == null)
                {
                    closureVariables = new List <ClosureInfo>();
                }

                foreach (var variable in Variables.Values)
                {
                    if (!HasClosureVariable(closureVariables, variable) &&
                        !variable.IsGlobal && (variable.AccessedInNestedScope || ExposesLocalVariable(variable)))
                    {
                        closureVariables.Add(new ClosureInfo(variable, true));
                    }

                    if (variable.Kind == VariableKind.Local)
                    {
                        Debug.Assert(variable.Scope == this);
                    }
                }
            }

            if (closureVariables != null)
            {
                _closureVariables = closureVariables.ToArray();
            }

            // no longer needed
            _references = null;
        }
Example #6
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, string name);
Example #7
0
 public ParameterBinder(PythonNameBinder binder)
 {
     _binder = binder;
 }
Example #8
0
 public DefineBinder(PythonNameBinder binder)
 {
     _binder = binder;
 }
Example #9
0
        internal static void BindAst(PythonLanguageVersion langVersion, PythonAst ast, ErrorSink context, bool bindReferences)
        {
            var binder = new PythonNameBinder(langVersion, ast, context, bindReferences);

            binder.Bind(ast);
        }
Example #10
0
 public DeleteBinder(PythonNameBinder binder)
 {
     _binder = binder;
 }