public TreeFor(TokenFor forToken, TokenVariable forVar, ArichmetichTree updateForVar, TreeFunctional tree) : base() { if (tree.head.type == TokenType.IF || tree.head.type == TokenType.FOR) { var tmp = tree as TreeIf; this.stackVariable = tmp.stackVariable; foreach (var item in tmp.stackVariableLocal) { this.stackVariable.Add(item.Key, item.Value); } } else { this.stackVariable = tree.stackVariable; } this.head = forToken; this.forVar = forVar; this.updateForVar = updateForVar; }
private void ProcessFor(TreeFunctional tree, ref int i, ref Token currentToken) { TokenFor forToken = currentToken as TokenFor; currentToken = GetNextToken(ref i, tree); if (currentToken.type != TokenType.ARITHMETIC_BRACKET_OPEN) { throw new Exception("Expected open bracket after for, Line: " + i.ToString()); } currentToken = GetNextToken(ref i, tree); TokenVariable forVar = null; if (currentToken.type == TokenType.VARIABLE) { forVar = currentToken as TokenVariable; } int endPos = i; if (forVar != null) { while (this.text[++endPos] != ';') { ; } TreeFunctional treeForInitForVar = new TreeFunctional(null, TokenType.FUNCTION, i - forVar.name.Length - 1, endPos); Process(treeForInitForVar); forVar.data = treeForInitForVar.stackVariable[forVar.name].data; i = endPos + 1; } List <Token> forStatement = new List <Token>(); currentToken = GetNextToken(ref i, tree); while (currentToken.type != TokenType.END_OP) { forStatement.Add(currentToken); currentToken = GetNextToken(ref i, tree); } currentToken = GetNextToken(ref i, tree); ArichmetichTree updateVarTree = null; if (currentToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE) { if (currentToken.type != TokenType.VARIABLE) { throw new Exception("Expect var in update var posution in for, Line: " + i.ToString()); } updateVarTree = new ArichmetichTree(currentToken as TokenVariable); currentToken = GetNextToken(ref i, tree); if (currentToken.type != TokenType.ASSIGN) { throw new Exception("Expect assign in update pos in for, Line: " + i.ToString()); } currentToken = GetNextToken(ref i, tree); while (currentToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE) { updateVarTree.PutToken(currentToken); currentToken = GetNextToken(ref i, tree); } } TreeFor forTree = new TreeFor(forToken, forVar, updateVarTree, tree); while (forTree.ProcessStatement(forStatement, i)) { Process(forTree); forTree.ProcessUpdateForVar(); } i = (forTree.head as TokenLogic).endPos + 1; }