예제 #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;
        }
예제 #2
0
        private static void GenerateForInStatement(ScriptGenerator generator, MemberSymbol symbol, ForInStatement statement)
        {
            ScriptTextWriter writer = generator.Writer;

            if (statement.IsDictionaryEnumeration) {
                writer.Write("var ");
                writer.Write(statement.DictionaryVariable.GeneratedName);
                writer.WriteTrimmed(" = ");
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.CollectionExpression);
                writer.Write(";");
                writer.WriteNewLine();

                writer.WriteTrimmed("for ");
                writer.Write("(var ");
                writer.Write(statement.LoopVariable.GeneratedName);
                writer.Write(" in ");
                writer.Write(statement.DictionaryVariable.GeneratedName);
                writer.WriteTrimmed(") ");
                writer.Write("{");
                writer.WriteNewLine();
                writer.Indent++;
                writer.Write("var ");
                writer.Write(statement.ItemVariable.GeneratedName);
                writer.WriteTrimmed(" = ");
                writer.WriteTrimmed("{ ");
                writer.WriteTrimmed("key: ");
                writer.Write(statement.LoopVariable.GeneratedName);
                writer.WriteTrimmed(", ");
                writer.WriteTrimmed("value: ");
                writer.Write(statement.DictionaryVariable.GeneratedName);
                writer.Write("[");
                writer.Write(statement.LoopVariable.GeneratedName);
                writer.Write("]");
                writer.WriteTrimmed(" };");
                writer.WriteNewLine();
                GenerateStatement(generator, symbol, statement.Body);
                writer.Indent--;
                writer.Write("}");
                writer.WriteNewLine();
            }
            else {
                writer.Write("var ");
                writer.Write(statement.LoopVariable.GeneratedName);
                writer.WriteTrimmed(" = ");

                writer.Write("ss.IEnumerator.getEnumerator(");
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.CollectionExpression);
                writer.Write(");");

                writer.WriteNewLine();

                writer.WriteTrimmed("while ");
                writer.Write("(");
                writer.Write(statement.LoopVariable.GeneratedName);
                writer.WriteTrimmed(".moveNext()) ");
                writer.Write("{");
                writer.WriteNewLine();
                writer.Indent++;

                writer.Write("var ");
                writer.Write(statement.ItemVariable.GeneratedName);
                writer.WriteTrimmed(" = ");
                writer.Write(statement.LoopVariable.GeneratedName);
                writer.Write(".current;");
                writer.WriteNewLine();

                GenerateStatement(generator, symbol, statement.Body);

                writer.Indent--;
                writer.Write("}");
                writer.WriteNewLine();
            }
        }