Exemplo n.º 1
0
        private Statement ProcessSwitchStatement(SwitchNode node)
        {
            Expression condition = _expressionBuilder.BuildExpression(node.Condition);
            if (condition is MemberExpression) {
                condition = _expressionBuilder.TransformMemberExpression((MemberExpression)condition);
            }

            SwitchStatement statement = new SwitchStatement(condition);

            foreach (SwitchSectionNode switchSectionNode in node.Cases) {
                SwitchGroup group = new SwitchGroup();

                foreach (StatementNode caseNode in switchSectionNode.Labels) {
                    if (caseNode is CaseLabelNode) {
                        CaseLabelNode labelNode = (CaseLabelNode)caseNode;
                        Expression labelValue = _expressionBuilder.BuildExpression(labelNode.Value);
                        if (labelValue is MemberExpression) {
                            labelValue = _expressionBuilder.TransformMemberExpression((MemberExpression)labelValue);
                        }

                        group.AddCase(labelValue);
                    }
                    else {
                        Debug.Assert(caseNode is DefaultLabelNode);
                        group.AddDefaultCase();
                    }
                }

                foreach (StatementNode statementNode in switchSectionNode.Statements) {
                    Statement childStatement = BuildStatement(statementNode);
                    if (childStatement != null) {
                        group.AddStatement(childStatement);
                    }
                }

                statement.AddSwitchGroup(group);
            }

            return statement;
        }
Exemplo n.º 2
0
        private static void GenerateSwitchStatement(ScriptGenerator generator, MemberSymbol symbol, SwitchStatement statement)
        {
            ScriptTextWriter writer = generator.Writer;

            writer.WriteTrimmed("switch ");
            writer.Write("(");
            ExpressionGenerator.GenerateExpression(generator, symbol, statement.Condition);
            writer.WriteTrimmed(") ");
            writer.Write("{");
            writer.WriteNewLine();
            writer.Indent++;
            foreach (SwitchGroup group in statement.Groups) {
                if (group.Cases != null) {
                    foreach (Expression caseExpression in group.Cases) {
                        writer.Write("case ");
                        ExpressionGenerator.GenerateExpression(generator, symbol, caseExpression);
                        writer.Write(":");
                        writer.WriteNewLine();
                    }
                }

                if (group.IncludeDefault) {
                    writer.Write("default:");
                    writer.WriteNewLine();
                }

                writer.Indent++;
                foreach (Statement childStatement in group.Statements) {
                    GenerateStatement(generator, symbol, childStatement);
                }
                writer.Indent--;
            }
            writer.Indent--;
            writer.Write("}");
            writer.WriteNewLine();
        }