예제 #1
0
        /// <summary>
        /// Gets the translated code for the grammar structure.
        /// </summary>
        /// <returns>The translated code for the grammar structure.</returns>
        public IEnumerable <Block> Translate(TranslationContext context)
        {
            // Create new scope
            Scope newScope = new Scope(context.CurrentScope);

            // Create translation context
            TranslationContext newContext = new TranslationContext(newScope, context);

            // Create counter variable
            StackValue counter = new StackValue(Variable, VarType, false);

            newScope.StackValues.Add(counter);

            // Create main loop contents
            List <Block> loopContents = new List <Block>();

            foreach (IStatement statement in Statements)
            {
                loopContents.AddRange(statement.Translate(newContext));
            }
            loopContents.Add(counter.CreateVariableIncrement(Step.Translate(newContext)));

            // Create output
            object startTranslated = Start.Balance().Translate(context);

            List <Block> output = new List <Block>();

            output.AddRange(counter.CreateDeclaration(startTranslated));
            output.Add(new Block(BlockSpecs.Repeat,
                                 new CompoundExpression(
                                     CompoundOperator.Divide,
                                     new CompoundExpression(CompoundOperator.Minus, End,
                                                            startTranslated is Block
                            ? new LookupExpression(counter, FileName, ErrorToken)
                            : Start, FileName, ErrorToken),
                                     Step,
                                     FileName,
                                     ErrorToken
                                     ).Translate(newContext), loopContents.ToArray()));
            output.AddRange(newScope.CreateCleanUp());

            return(output);
        }