Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            node.Parent = _currentScope;

            foreach (string n in node.Names)
            {
                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);
                }
            }
            return(true);
        }