Пример #1
0
        private static void GenerateForStatement(ScriptGenerator generator, MemberSymbol symbol, ForStatement statement) {
            if (statement.Body == null) {
                return;
            }

            ScriptTextWriter writer = generator.Writer;

            writer.WriteTrimmed("for ");
            writer.Write("(");
            if (statement.Initializers != null) {
                ExpressionGenerator.GenerateExpressionList(generator, symbol, statement.Initializers);
            }
            else if (statement.Variables != null) {
                GenerateVariableDeclarations(generator, symbol, statement.Variables);
            }
            writer.WriteTrimmed("; ");
            if (statement.Condition != null) {
                ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
            }
            writer.WriteTrimmed("; ");
            if (statement.Increments != null) {
                ExpressionGenerator.GenerateExpressionList(generator, symbol, statement.Increments);
            }
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;
            GenerateStatement(generator, symbol, statement.Body);
            writer.Indent--;
            writer.Write("}");
            writer.WriteNewLine();
        }
Пример #2
0
        private Statement ProcessForStatement(ForNode node)
        {
            ForStatement statement = new ForStatement();

            _symbolTable.PushScope();

            if (node.Initializer != null) {
                if (node.Initializer is VariableDeclarationNode) {
                    VariableDeclarationStatement initializer = (VariableDeclarationStatement)BuildStatement((StatementNode)node.Initializer);
                    statement.AddInitializer(initializer);
                }
                else {
                    Debug.Assert(node.Initializer is ExpressionListNode);

                    ICollection<Expression> initializers = _expressionBuilder.BuildExpressionList((ExpressionListNode)node.Initializer);
                    foreach (Expression initializer in initializers) {
                        statement.AddInitializer(initializer);
                    }
                }
            }

            if (node.Condition != null) {
                Expression condition = _expressionBuilder.BuildExpression(node.Condition);
                if (condition is MemberExpression) {
                    condition = _expressionBuilder.TransformMemberExpression((MemberExpression)condition);
                }
                statement.AddCondition(condition);
            }

            if (node.Increment != null) {
                Debug.Assert(node.Increment is ExpressionListNode);

                ICollection<Expression> increments = _expressionBuilder.BuildExpressionList((ExpressionListNode)node.Increment);
                foreach (Expression increment in increments) {
                    statement.AddIncrement(increment);
                }
            }

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

            _symbolTable.PopScope();

            return statement;
        }