Exemple #1
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack      = 0;
            var caseLabels = new List <LabelOperand>(Branches.Count);

            var             caseEnd      = context.MakeLabel("caseEnd");
            LabelOperand    caseDefault  = null;
            BlockExpression defaultBlock = null;

            for (var i = 0; i < Branches.Count; i++)
            {
                var label = context.MakeLabel("caseBranch_" + i);
                caseLabels.Add(label);

                var conditions = Branches[i].Conditions;
                if (conditions.Any(c => c == null))
                {
                    caseDefault = label;

                    if (conditions.Count == 1)
                    {
                        defaultBlock = Branches[i].Block;
                    }
                }
            }

            var emptyDefault = caseDefault == null;

            if (emptyDefault)
            {
                caseDefault = context.MakeLabel("caseDefault");
            }

            context.PushScope();

            context.Statement(Expression);
            stack += Expression.Compile(context);

            var flattenedBranches = FlattenBranches(Branches, caseLabels, caseDefault);

            BuildTables(flattenedBranches, caseDefault, out var tables, out var rest);

            foreach (var table in tables)
            {
                var start  = table.Entries[0].Value;
                var labels = table.Entries.Select(e => e.Label).ToList();

                stack += context.Dup();
                stack += context.JumpTable(start, labels);
            }

            foreach (var entry in rest)
            {
                stack += context.Dup();
                stack += entry.Condition.Compile(context);
                stack += context.BinaryOperation(TokenType.EqualTo);
                stack += context.JumpTrue(entry.Label);
            }

            stack += context.Jump(caseDefault);

            context.PushLoop(null, caseEnd);

            for (var i = 0; i < Branches.Count; i++)
            {
                var branchStack = stack;
                var branch      = Branches[i];

                branchStack += context.Bind(caseLabels[i]);
                branchStack += context.Drop();
                branchStack += branch.Block.Compile(context);
                branchStack += context.Jump(caseEnd);

                CheckStack(branchStack, 0);
            }

            // only bind if we need a default block
            if (emptyDefault || defaultBlock != null)
            {
                stack += context.Bind(caseDefault);
            }

            // always drop the switch value
            stack += context.Drop();

            if (defaultBlock != null)
            {
                stack += defaultBlock.Compile(context);
            }

            context.PopLoop();

            stack += context.Bind(caseEnd);

            context.PopScope();

            CheckStack(stack, 0);
            return(stack);
        }