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; }
public TreeIf(TokenIfElse ifToken, 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 = ifToken; this.arichmeticStatementTree = new ArichmetichTree(null); this.stackVariableLocal = new Dictionary <string, TokenVariable>(); }
private void ProcessVariable(TreeFunctional tree, ref int i, ref Token currentToken, bool withAssign = true) { Token nextToken = GetNextToken(ref i, tree); if (nextToken.type == TokenType.ASSIGN || !withAssign) { if (withAssign) { nextToken = GetNextToken(ref i, tree); } var type = nextToken.type; if (type == TokenType.FUNCTION) { nextToken = ProcessFunction(tree, ref i, ref nextToken); if (nextToken == null) { throw new Exception("Expected return value in function, Line:" + i); } (currentToken as TokenVariable).data = (nextToken as TokenVariable).data; (currentToken as TokenVariable).varType = (nextToken as TokenVariable).varType; nextToken = GetNextToken(ref i, tree); type = nextToken.type; } if (type == TokenType.NUMERIC_CONST || type == TokenType.ARITHMETIC_BRACKET_OPEN || (type == TokenType.VARIABLE && (nextToken as TokenVariable).varType != VariableType.STRING)) { ArichmetichTree arTree = new ArichmetichTree(currentToken as TokenVariable); tree.next = arTree; int numBracket = nextToken.type == TokenType.ARITHMETIC_BRACKET_OPEN ? 1 : 0; while (nextToken.type != TokenType.END_OP && nextToken.type != TokenType.COMA && (nextToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE || numBracket == 0)) { arTree.PutToken(nextToken); nextToken = GetNextToken(ref i, tree); switch (nextToken.type) { case TokenType.ARITHMETIC_BRACKET_OPEN: numBracket++; break; case TokenType.ARITHMETIC_BRACKET_CLOSE: numBracket--; break; case TokenType.FUNCTION: nextToken = ProcessFunction(tree, ref i, ref nextToken); if (nextToken == null) { throw new Exception("Expected return value in function, Line:" + i); } break; } } } else if (type == TokenType.VARIABLE && (type == TokenType.VARIABLE && (nextToken as TokenVariable).varType != VariableType.NUMERIC)) { StringTree strTree = new StringTree(currentToken as TokenVariable); tree.next = strTree; while (nextToken.type != TokenType.END_OP && nextToken.type != TokenType.COMA && nextToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE) { strTree.AddToken(nextToken as TokenVariable); nextToken = GetNextToken(ref i, tree); if (nextToken.type == TokenType.END_OP || nextToken.type == TokenType.ARITHMETIC_BRACKET_CLOSE) { break; } if (nextToken.type != TokenType.PLUS) { throw new Exception("Expect \'+\' in line:" + i.ToString()); } nextToken = GetNextToken(ref i, tree); if (nextToken.type == TokenType.FUNCTION) { nextToken = ProcessFunction(tree, ref i, ref nextToken); if (nextToken == null) { throw new Exception("Expected return value in function, Line:" + i); } } } } else if (type == TokenType.END_OP) { return; } else { throw new Exception("Somethin bad in Process"); } } }
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; }