Example #1
0
        public override void Compile(MoveInfo treeInfo, ScriptInfo scriptInfo, CompilingInfo compilingInfo)
        {
            /*
             * _statement
             * !BlockStatement
             * =================================
             * do
             *  statement;
             * while (exp);
             * =================================
             *
             * _statement
             * BlockStatement
             * ScopeGroup
             *
             * =================================
             * var = true; while (var || exp)
             * { var = false; statement; }
             *
             * =================================
             *
             * =================================
             * do
             * {
             *  statement;
             * }
             * while (exp);
             * =================================
             *
             * =================================
             * var = true; while (var || exp)
             * { var = false;
             *  statement;
             * }
             *
             * =================================
             */

            Token      firstVar = new Token(TokenType.Word, "doWhileJHS8G8AW9_" + compilingInfo.IteratorsCount++);
            Expression exp      = (Expression)_expParentGroup.GetContent().Find(a => a is Expression);

            // "doWhileControlVar = true;"
            BallOfMud firstVarInit = new BallOfMud(
                new List <IElement>()
            {
                firstVar,
                Token.Space,
                Token.Assign,
                Token.Space,
                Token.True,
                Token.SemiColon
            });

            // "doWhileControlVar = false;"
            BallOfMud firstVarCancel = new BallOfMud(
                new List <IElement>()
            {
                firstVar,
                Token.Space,
                Token.Assign,
                Token.Space,
                Token.False,
                Token.SemiColon
            });

            BallOfMud blockVarInsert = new BallOfMud(
                new List <IElement> {
                Token.Space, firstVarCancel
            });

            #region Replace "do" with "doWhileControlVar = true; while (doWhileControlVar || exp)"
            // "(doWhileControlVar || exp)"
            BallOfMud newExpression = new BallOfMud(
                new List <IElement> {
                Token.ParenthesesOpen,
                firstVar,
                Token.Space,
                Token.LogicOR,
                Token.Space,
                exp,
                Token.ParenthesesClose
            });

            // "doWhileControlVar = true; while (doWhileControlVar || exp)"
            BallOfMud doReplace = new BallOfMud(
                new List <IElement> {
                firstVarInit,
                Token.Space,
                WhileKeyword,
                Token.Space,
                newExpression
            });

            int doIndex = this.children.IndexOf(_doKeyword);
            this.children[doIndex] = doReplace;
            #endregion

            #region Add fisrtVarCancel & add block
            if (_statement is BlockStatement) // it is already block ...
            {
                ScopeGroup blockGroup = (ScopeGroup)_statement.GetChildren().Find(a => a is ScopeGroup);
                blockGroup.GetContent().Insert(0, blockVarInsert);
            }
            else // create block
            {
                // "{ doWhileControlVar = false; statement; }"
                BallOfMud blockForInsert = new BallOfMud(
                    new List <IElement> {
                    Token.ScopeOpen,
                    Token.Space,
                    firstVarCancel,
                    Token.Space,
                    _statement,
                    Token.Space,
                    Token.ScopeClose
                });

                int statementI  = this.children.IndexOf(_statement);
                int tryTabIndex = statementI - 1;
                if (tryTabIndex >= 0 &&
                    this.children[tryTabIndex].IsTT(TokenType.WhiteSpace) &&
                    this.children[tryTabIndex].ToString() == "\t")
                {
                    this.children.RemoveAt(tryTabIndex);
                    statementI--;
                }
                this.children[statementI] = blockForInsert;
            }
            #endregion

            // delete "while (exp);"
            int whileKeywordI = this.children.IndexOf(_whileKeyword);
            this.children.RemoveRange(whileKeywordI, this.children.Count - whileKeywordI);
        }