Example #1
0
        private void GetIndexesAndValues(ArrayContentDef arrayDef, ref Dictionary <Queue <StrIntIndex>, IElement> indexesForValues, Queue <StrIntIndex> lastQueue)
        {
            Queue <StrIntIndex> indexesQueue;

            foreach (StrIntIndex curI in arrayDef._content.Keys)
            {
                if (lastQueue != null)
                {
                    indexesQueue = new Queue <StrIntIndex>(lastQueue);
                }
                else
                {
                    indexesQueue = new Queue <StrIntIndex>();
                }

                indexesQueue.Enqueue(curI);

                IElement curElem = arrayDef._content[curI];

                // empty array {} | []
                if ((curElem is ArrayContentDef && ((ArrayContentDef)curElem)._isEmpty) ||
                    (curElem is Expression && ((Expression)curElem).GetChildren()[0] is ArrayDef))
                {
                    IElement emptyArray = new BallOfMud(new List <IElement> {
                        Token.SQBracketOpen, Token.SQBracketClose
                    });
                    indexesForValues.Add(indexesQueue, emptyArray);
                }
                else if (curElem is ArrayContentDef) // { {...} }
                {
                    GetIndexesAndValues((ArrayContentDef)curElem, ref indexesForValues, indexesQueue);
                }
                else if (curElem is Expression) // {"5"}
                {
                    indexesForValues.Add(indexesQueue, curElem);
                }
                else
                {
                    throw new InvalidOperationException("Ehm...WTF?!...check Parse");
                }
            }
        }
Example #2
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);
        }
Example #3
0
        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
        }
Example #4
0
        public IElement GetCompiledStatements(VarName varName)
        {
            BallOfMud statements = new BallOfMud();

            statements.AddChildren(new List <IElement> {
                Token.ScopeOpen, Token.Space
            });

            Dictionary <Queue <StrIntIndex>, IElement> indexesForValues = new Dictionary <Queue <StrIntIndex>, IElement>();

            GetIndexesAndValues(this, ref indexesForValues, null);

            foreach (Queue <StrIntIndex> queue in indexesForValues.Keys)
            {
                IElement  value     = indexesForValues[queue];
                BallOfMud statement = new BallOfMud(new List <IElement> {
                    varName
                });
                foreach (StrIntIndex curIndex in queue)
                {
                    Token index = null;
                    if (curIndex.Index is String)
                    {
                        index = new Token(TokenType.String, (string)curIndex.Index);
                    }
                    else
                    {
                        index = new Token(TokenType.Number, (int)curIndex.Index);
                    }

                    statement.AddChildren(new List <IElement> {
                        Token.SQBracketOpen, index, Token.SQBracketClose
                    });
                }
                statement.AddChildren(new List <IElement> {
                    Token.Space, Token.Assign, Token.Space, value, Token.SemiColon, Token.Space
                });
                statements.AddChildren(statement);
            }

            statements.AddChildren(Token.ScopeClose);
            return(statements);

            /*
             * numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
             *  numbers[0] = 0;
             *  numbers[1] = 1;
             *  etc...
             *
             *  vectors = { {0, 0, 0}, {5, 10, 2}, {487, 516, 487486} };
             *  vector[0][0] = 0;
             *  vector[0][1] = 0;
             *  vector[0][2] = 0;
             *  vector[1][0] = 5;
             *  vector[1][1] = 10;
             *  vector[1][2] = 2;
             *  vector[2][0] = 487;
             *  vector[2][1] = 516;
             *  vector[2][2] = 487486;
             *
             *  menus = { { {"Item0_0_0", "Item0_0_1"}, {"Item0_1_0", "Item0_1_1"} }, { {"Item1_0_0", "Item1_0_1"}, {"Item1_1_0", "Item1_1_1"} } };
             *  menus[0][0][0] = "Item0_0_0";
             *  menus[0][0][1] = "Item0_0_1";
             *  menus[0][1][0] = "Item0_1_0";
             *  menus[0][1][1] = "Item0_1_1";
             *  menus[1][0][0] = "Item1_0_0";
             *  menus[1][0][1] = "Item1_0_1";
             *  menus[1][1][0] = "Item1_1_0";
             *  menus[1][1][1] = "Item1_1_1";
             *
             *  personWithoutHobbys = {Name = "Peter", Age = 17, Hobbys = {}};
             *  personWithoutHobbys = {Name = "Peter", Age = 17, Hobbys = []};
             *  personWithoutHobbys["Name"] = "Peter";
             *  personWithoutHobbys["Age"] = 17;
             *  personWithoutHobbys["Hobbys"] = [];
             *
             *  person = {Name = "Peter", Age = 17, Hobbys = {"PC", "Skiing"} };
             *  person["Name"] = "Peter";
             *  person["Age"] = 17;
             *  person["Hobbys"][0] = "PC";
             *  person["Hobbys"][1] = "Skiing";
             *
             *  persons = { {Name = "Peter", Age = 17, Hobbys = {"PC", "Skiing"} }, {Name = "Andrea", Age = 21, Hobbys = {"Diving", "Cooking"} } };
             *  persons[0]["Name"] = "Peter";
             *  persons[0]["Age"] = 17;
             *  persons[0]["Hobbys"][0] = "PC";
             *  persons[0]["Hobbys"][1] = "Skiing";
             *  persons[1]["Name"] = "Andrea";
             *  persons[1]["Age"] = 21;
             *  persons[1]["Hobbys"][0] = "Diving";
             *  persons[1]["Hobbys"][1] = "Cooking";
             */
        }