Exemple #1
0
        private Statement ProcessForeachStatement(ForeachNode node)
        {
            TypeSymbol type = _symbolSet.ResolveType(node.Type, _symbolTable, _memberContext);

            Debug.Assert(type != null);

            bool dictionaryContainer = (type.Name == "DictionaryEntry") || (type.Name == "KeyValuePair`2");

            Expression collectionExpression = _expressionBuilder.BuildExpression(node.Container);

            if (collectionExpression is MemberExpression)
            {
                collectionExpression = _expressionBuilder.TransformMemberExpression((MemberExpression)collectionExpression);
            }

            ForInStatement statement;

            if (dictionaryContainer)
            {
                VariableSymbol dictionaryVariable = null;

                if (collectionExpression.Type != ExpressionType.Local)
                {
                    string dictionaryVariableName = _symbolTable.CreateSymbolName("dict");
                    dictionaryVariable = new VariableSymbol(dictionaryVariableName, _memberContext,
                                                            collectionExpression.EvaluatedType);
                }

                statement = new ForInStatement(collectionExpression, dictionaryVariable);

                string         keyVariableName = _symbolTable.CreateSymbolName("key");
                VariableSymbol keyVariable     = new VariableSymbol(keyVariableName, _memberContext,
                                                                    _symbolSet.ResolveIntrinsicType(IntrinsicType.String));
                statement.SetLoopVariable(keyVariable);
            }
            else
            {
                statement = new ForInStatement(collectionExpression);

                string         enumeratorVariableName = _symbolTable.CreateSymbolName("enum");
                VariableSymbol enumVariable           = new VariableSymbol(enumeratorVariableName, _memberContext,
                                                                           _symbolSet.ResolveIntrinsicType(IntrinsicType.IEnumerator));
                statement.SetLoopVariable(enumVariable);
            }

            _symbolTable.PushScope();

            VariableSymbol itemVariable = new VariableSymbol(node.Name.Name, _memberContext, type);

            _symbolTable.AddSymbol(itemVariable);
            statement.SetItemVariable(itemVariable);

            Statement body = BuildStatement((StatementNode)node.Body);

            statement.AddBody(body);

            _symbolTable.PopScope();

            return(statement);
        }
Exemple #2
0
        private void BindGlobalVariable(VariableDeclarationStatementSyntax variableDeclarationStatementSyntax)
        {
            var declaration = variableDeclarationStatementSyntax.Declaration;

            foreach (var declarator in declaration.Variables)
            {
                var variableType = _symbolSet.ResolveType(declaration.Type, null, null);

                foreach (var arrayRankSpecifier in declarator.ArrayRankSpecifiers)
                {
                    variableType = new ArraySymbol(variableType);
                }

                var symbol = new GlobalVariableSymbol(declarator, variableType);
                _bindingResult.AddSymbol(declarator, symbol);

                _symbolSet.AddGlobal(symbol);
            }
        }