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); }
public override void Compile(MoveInfo treeInfo, ScriptInfo scriptInfo, CompilingInfo compilingInfo) { /* * ================================= * foreach (var in array) * statement; * ================================= * * ================================= * for (i = 0; i < array.size; i++) * { var = array[i]; statement; } * ================================= * * ================================= * foreach (var in array) * { * statement; * } * ================================= * * ================================= * for (i = 0; i < array.size; i++) * { var = array[i]; * statement; * } * ================================= */ Token indexer = new Token(TokenType.Word, "foreachg45e74f_" + compilingInfo.IteratorsCount++); #region Replace "foreach (var in array)" with "for (i = 0; i < array.size; i++)" // "i = 0" BallOfMud indexerInit = new BallOfMud( new List <IElement>() { indexer, Token.Space, Token.Assign, Token.Space, NumberZero }); // "i < array.size" BallOfMud indexerCondition = new BallOfMud( new List <IElement>() { indexer, Token.Space, Token.LogicLess, Token.Space, _array, Token.Ref, KeywordSize }); // "i++" BallOfMud indexerStatement = new BallOfMud( new List <IElement>() { indexer, Token.INC }); // "(i = 0; i < array.size; i++)" BallOfMud foreachGroupReplace = new BallOfMud( new List <IElement> { Token.ParenthesesOpen, indexerInit, Token.SemiColon, Token.Space, indexerCondition, Token.SemiColon, Token.Space, indexerStatement, Token.ParenthesesClose }); int foreachI = this.children.IndexOf(_foreachKeyword); this.children[foreachI] = KeywordFor; int foreachGroupI = this.children.IndexOf(_foreachGroup); this.children[foreachGroupI] = foreachGroupReplace; #endregion #region Add var define & add block // " var = array[i];" BallOfMud varDefine = new BallOfMud( new List <IElement> { Token.Space, _currentVar, Token.Space, Token.Assign, Token.Space, _array, Token.SQBracketOpen, indexer, Token.SQBracketClose, Token.SemiColon }); if (_statement is BlockStatement) // it is already block ... { ScopeGroup blockGroup = (ScopeGroup)_statement.GetChildren().Find(a => a is ScopeGroup); blockGroup.GetContent().Insert(0, varDefine); } else // create block { // "{ var = array[i]; statement; }" BallOfMud blockForInsert = new BallOfMud( new List <IElement> { Token.ScopeOpen, varDefine, 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 }