Beispiel #1
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(
                            "name '{0}' is assigned to before global declaration".FormatUI(n),
                            node
                            );
                        break;

                    case VariableKind.Parameter:
                        ReportSyntaxError(
                            "Name '{0}' is a function parameter and declared global".FormatUI(n),
                            node);
                        break;
                    }
                }

                // Check for the name being referenced previously. If it has been, issue warning.
                if (_currentScope.IsReferenced(n) && !assignedGlobal)
                {
                    ReportSyntaxWarning(
                        "name '{0}' is used prior to global declaration".FormatUI(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, BindReferences, Reference(n));
            }
            return(true);
        }
Beispiel #2
0
        // ClassDefinition
        public override bool Walk(ClassDefinition node)
        {
            if (node.Name != null)
            {
                node.Variable = DefineName(node.Name);
                node.AddVariableReference(GlobalScope, BindReferences, Reference(node.Name));
            }

            // Base references are in the outer context
            foreach (var b in node.Bases)
            {
                b.Expression.Walk(this);
            }

            // process the decorators in the outer context
            foreach (var dec in (node.Decorators?.Decorators).MaybeEnumerate().ExcludeDefault())
            {
                dec.Walk(this);
            }

            PushScope(node);
            node.ModuleNameVariable = GlobalScope.EnsureGlobalVariable("__name__");

            // define the __doc__ and the __module__
            if (node.Body.Documentation != null)
            {
                node.DocVariable = DefineName("__doc__");
            }
            node.ModVariable = DefineName("__module__");
            if (LanguageVersion.Is3x())
            {
                node.ClassVariable = DefineName("__class__");
            }

            // Walk the body
            node.Body.Walk(this);
            return(false);
        }
Beispiel #3
0
        // FunctionDefinition
        public override bool Walk(FunctionDefinition node)
        {
            GlobalScope.EnsureGlobalVariable("__name__");

            // Name is defined in the enclosing context
            if (!node.IsLambda)
            {
                node.Variable = DefineName(node.Name);
                node.AddVariableReference(GlobalScope, BindReferences, Reference(node.Name));
            }

            // process the default arg values and annotations in the outer
            // context
            foreach (var p in node.Parameters)
            {
                p.DefaultValue?.Walk(this);
                p.Annotation?.Walk(this);
            }
            // process the decorators in the outer context
            foreach (var dec in (node.Decorators?.Decorators).MaybeEnumerate().ExcludeDefault())
            {
                dec.Walk(this);
            }

            // process the return annotation in the outer context
            node.ReturnAnnotation?.Walk(this);
            PushScope(node);

            foreach (var p in node.Parameters)
            {
                p.Walk(_parameter);
            }

            node.Body?.Walk(this);
            return(false);
        }