예제 #1
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            foreach (var nameNode in node.Names)
            {
                var n = nameNode.Name;
                if (n == null)
                {
                    continue;
                }

                // Check current scope for conflicting variable
                var assignedGlobal = false;
                if (_currentScope.TryGetVariable(n, out var conflict))
                {
                    // conflict?
                    switch (conflict.Kind)
                    {
                    case VariableKind.Global:
                        // OK to reassign, as long as kind is the same.
                        // Consider (from Python test grammar)
                        // global a
                        // global a, b
                        assignedGlobal = true;
                        break;

                    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
                var 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);
        }
예제 #2
0
 public virtual void PostWalk(GlobalStatement node)
 {
 }
예제 #3
0
 // GlobalStatement
 public virtual bool Walk(GlobalStatement node)
 {
     return(true);
 }
예제 #4
0
 // GlobalStatement
 public override bool Walk(GlobalStatement node)
 {
     return(Contains(node));
 }
예제 #5
0
 public override void PostWalk(GlobalStatement node)
 {
 }
예제 #6
0
 // GlobalStatement
 public override bool Walk(GlobalStatement node)
 {
     return(false);
 }