public static bool Check(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo) { if (parentInfo.Current.IsTT(TokenType.ScopeOpen)) { ScopeGroup.Parse(parentInfo, parsingInfo, scriptInfo); return(true); } else if (parentInfo.Current.IsTT(TokenType.ParenthesesOpen)) { ParenthesesGroup.Parse(parentInfo, parsingInfo, scriptInfo); return(true); } else if (parentInfo.Current.IsTT(TokenType.SQBracketOpen)) { SQBracketGroup.Parse(parentInfo, parsingInfo, scriptInfo); return(true); } return(false); }
public override IElement CreateCopy() { ArrayContentDef e = new ArrayContentDef(); e.AddChildren(this.CopyChildren()); e._isEmpty = _isEmpty; e._contentIntsCount = _contentIntsCount; ScopeGroup thisScope = (ScopeGroup)this.children[0]; ScopeGroup eScope = (ScopeGroup)e.children[0]; foreach (StrIntIndex index in _content.Keys) { IElement value = _content[index]; e._content.Add(index, eScope.GetChildren()[thisScope.GetChildren().IndexOf(value)]); } return(e); }
public override IElement CreateCopy() { ScopeGroup g = new ScopeGroup(this.CopyChildren()); return(g); }
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 }
private static void Parse(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo) { SwitchStatement switchStatement = new SwitchStatement(); MoveInfo moveInfo = new MoveInfo(parentInfo); // expression IElement tryExpGroup = moveInfo.FindNextBlack(SearchDirection.LeftToRight); if (!(tryExpGroup is ParenthesesGroup)) { throw new SyntaxException("Could not find switch expression", parentInfo.GetErrorInfo()); } ParenthesesGroup expGroup = (ParenthesesGroup)tryExpGroup; MoveInfo expGroupInfo = new MoveInfo(expGroup, SearchTree.ContentBlock, 0, parentInfo); Expression exp = Expression.Parse(expGroupInfo, parsingInfo, scriptInfo); if (exp == null || expGroupInfo.FindNextBlack(SearchDirection.LeftToRight) != null) { throw new SyntaxException("Could not parse switch expression", parentInfo.GetErrorInfo()); } // scope group IElement tryScopeGroup = moveInfo.FindNextBlack(SearchDirection.LeftToRight); if (!(tryScopeGroup is ScopeGroup)) { throw new SyntaxException("Could not find switch ScopeGroup", parentInfo.GetErrorInfo()); } ScopeGroup scopeGroup = (ScopeGroup)tryScopeGroup; MoveInfo scopeGroupInfo = new MoveInfo(scopeGroup, SearchTree.ContentBlock, 0, parentInfo); IElement nextCase = scopeGroupInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible); if (nextCase == null) { throw new SyntaxException("Could not find any switch case", parentInfo.GetErrorInfo()); } while (nextCase != null) { if (!( DefaultSwitchStatement.Check(scopeGroupInfo, parsingInfo, scriptInfo, true) || CaseSwitchStatement.Check(scopeGroupInfo, parsingInfo, scriptInfo, true) )) { throw new SyntaxException("Could not parse switch case/default", parentInfo.GetErrorInfo()); } nextCase = scopeGroupInfo.FindNextBlack(SearchDirection.LeftToRight); } // build int startIndex = parentInfo.CurrentIndex; int length = (moveInfo.CurrentIndex + 1) - startIndex; switchStatement.AddChildren(parentInfo.CurrentElements.GetRange(startIndex, length)); parentInfo.Replace(length, switchStatement); }
public static void Parse(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo) { FuncDef function = new FuncDef(parentInfo.Current.CharIndex, parentInfo.Current.CharLength, parentInfo.Current.LineIndex, parsingInfo.SF); // začiatok názvu MoveInfo moveInfo = new MoveInfo(parentInfo); int startIndex = parentInfo.CurrentIndex; // modifier MemberAccess access; AccessModifier modifier = AccessModifier.GetModifier(moveInfo, out access); if (modifier != null) { startIndex = moveInfo.CurrentIndex; } else { moveInfo = new MoveInfo(parentInfo); } // xml XMLBlock xml = XMLBlock.GetXMLSummary(moveInfo); function.xmlBlock = xml; if (xml != null) { startIndex = moveInfo.CurrentIndex; } // začiatok názvu moveInfo = new MoveInfo(parentInfo); IElement nameElem = moveInfo.Current; moveInfo.Move(SearchDirection.LeftToRight); // parametry IElement paramsTry = moveInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible); if (!(paramsTry is ParenthesesGroup)) { throw new SyntaxException("Could not find FuncDef parameters", parentInfo.GetErrorInfo()); } ParenthesesGroup paramsGroup = (ParenthesesGroup)paramsTry; // získaj zoznam parametrov MoveInfo paramsMoveInfo = new MoveInfo(paramsGroup, SearchTree.ContentBlock, 0, moveInfo); List <FuncDefParam> defParams = GetParameterList(paramsMoveInfo, parsingInfo, scriptInfo); // pridaj zoznam parametrov do stromu a posuň sa zaň moveInfo.Move(SearchDirection.LeftToRight); // body IElement bodyTry = moveInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible); if (!(bodyTry is ScopeGroup)) { throw new SyntaxException("Could not find FuncDef body", parentInfo.GetErrorInfo()); } ScopeGroup bodyGroup = (ScopeGroup)bodyTry; List <IElement> bodyGroupChildren = bodyGroup.GetChildren(); moveInfo.Move(SearchDirection.LeftToRight); // skoč za telo // add func to tree int totalLength = moveInfo.CurrentIndex - startIndex; List <IElement> children = parentInfo.CurrentElements.GetRange(startIndex, totalLength); function.AddChildren(children); foreach (FuncDefParam p in defParams) { function.LocalVars.Add(new LocalVarInfo(parsingInfo.SF, p.Name, (int)function.ImportantCharIndex, (int)function.ImportantCharLength, null, p.VarName)); // it must be after function.AddChildren() !! } parentInfo.MoveToIndex(startIndex); parentInfo.Replace(totalLength, function); // set current function parsingInfo.CurrentFunc = function; parsingInfo.FuncDefList.Add(function); // go inside body MoveInfo bodyInfo = new MoveInfo(bodyGroup, SearchTree.ContentBlock, 0, parentInfo); Statement.ParseStatementList(bodyInfo, parsingInfo, scriptInfo); // info string name = nameElem.ToString(); /*if (scriptInfo.) * if (scriptInfo.Functions.FindIndex(a => a.Name.EqualCode(name)) != -1) * { * ErrorManager.Semantic("Function '" + name + "' already defined", new ErrorInfo(parentInfo.ErrorInfo)); * return; * }*/ FuncInfo funcInfo = GetInfo(name, access, defParams, xml, parsingInfo, function); scriptInfo.AddFunction(funcInfo); function.funcInfo = funcInfo; }
private static ArrayContentDef Parse(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo) { ArrayContentDef arrayDef = new ArrayContentDef(); ScopeGroup group = (ScopeGroup)parentInfo.Current; MoveInfo moveInfo = new MoveInfo(group, SearchTree.ContentBlock, 0, parentInfo); IElement tryNext = moveInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible); // only for strIndex MoveInfo strIndexerInfo = new MoveInfo(moveInfo); IElement tryAssign = strIndexerInfo.FindNextBlack(SearchDirection.LeftToRight); if (tryNext == null) // { } { arrayDef._isEmpty = true; } while (!arrayDef._isEmpty) { if (tryNext == null) { throw new SyntaxException("Could not find next element in ArrayContentDef", parentInfo.GetErrorInfo()); } else if (tryNext is ScopeGroup) // { {...} } { ArrayContentDef contentDef = ArrayContentDef.Parse(moveInfo, parsingInfo, scriptInfo); arrayDef._content.Add(new StrIntIndex(arrayDef._contentIntsCount++), contentDef); tryNext = moveInfo.FindNextBlack(SearchDirection.LeftToRight); } else if (tryNext.IsTT(TokenType.Word) && tryAssign != null && tryAssign.IsTT(TokenType.Assign)) // { Name = "MyName" } { string strIndex = tryNext.ToString(); IElement strValue = null; IElement strValueTry = strIndexerInfo.FindNextBlack(SearchDirection.LeftToRight); // move behind "=" if (strValueTry == null) { throw new SyntaxException("Could not find value for strIndex in ArrayContentDef", parentInfo.GetErrorInfo()); } if (strValueTry is ScopeGroup) // { Name = {...} } { strValue = ArrayContentDef.Parse(strIndexerInfo, parsingInfo, scriptInfo); } else { strValue = Expression.Parse(strIndexerInfo, parsingInfo, scriptInfo); if (strValue == null) { throw new SyntaxException("Could not parse expression for strIndex in ArrayContentDef", parentInfo.GetErrorInfo()); } } StrIntIndex newIndex = new StrIntIndex(strIndex); StrIntIndex createdIndex = arrayDef._content.Keys.FirstOrDefault(a => a == newIndex); // index may have been already defined in this def.. if (createdIndex != null) { scriptInfo.SF.Errors.Add(new SemanticError("ArrayContentDef already contains key '" + strIndex + "'", new ErrorInfo(moveInfo.GetErrorInfo()))); } else { arrayDef._content.Add(newIndex, strValue); } tryNext = strIndexerInfo.FindNextBlack(SearchDirection.LeftToRight); moveInfo = strIndexerInfo; } else // { 1, "dawd", self GetGuid() } { Expression simpleExp = Expression.Parse(moveInfo, parsingInfo, scriptInfo); if (simpleExp == null) { throw new SyntaxException("Could not parse expression in ArrayContentDef", parentInfo.GetErrorInfo()); } arrayDef._content.Add(new StrIntIndex(arrayDef._contentIntsCount++), simpleExp); tryNext = moveInfo.FindNextBlack(SearchDirection.LeftToRight); } if (tryNext == null) // end of def { break; } else if (tryNext.IsTT(TokenType.Comma)) // new elem... { tryNext = moveInfo.FindNextBlack(SearchDirection.LeftToRight); // only for strIndex strIndexerInfo = new MoveInfo(moveInfo); tryAssign = strIndexerInfo.FindNextBlack(SearchDirection.LeftToRight); continue; } else // WTF?! { throw new SyntaxException("Unexpected token '" + tryNext.ToString() + "' in ArrayContentDef", parentInfo.GetErrorInfo()); } } arrayDef.AddChildren(group); parentInfo.Replace(1, arrayDef); return(arrayDef); }