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; }
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"); } } }
/// <summary> /// if i < 2 is true expected token with type NUMERIC_CONST and data is 1 /// </summary> /// <param name="token"></param> public void PutTokenInStatement(Token token, Token tokenOfOperation, Token rightOperator, int line) { double leftData = 0; if (token.type == TokenType.VARIABLE || token.type == TokenType.NUMERIC_CONST) { leftData = (double)(token as TokenNumeric).data; } switch (token.type) { case TokenType.ARITHMETIC_BRACKET_OPEN: this.arichmeticStatementTree.PutToken(new Token() { type = TokenType.ARITHMETIC_BRACKET_OPEN }); break; case TokenType.ARITHMETIC_BRACKET_CLOSE: this.arichmeticStatementTree.PutToken(new Token() { type = TokenType.ARITHMETIC_BRACKET_CLOSE }); break; case TokenType.VARIABLE: case TokenType.NUMERIC_CONST: var tokenCondition = tokenOfOperation; Token nextToken = rightOperator; double rightData = (double)(nextToken as TokenNumeric).data; double resultSmallStation = 0; switch (tokenCondition.type) { case TokenType.MORE: if (leftData > rightData) { resultSmallStation = 1; } else { resultSmallStation = 0; } goto putInStatement; case TokenType.LESS: if (leftData < rightData) { resultSmallStation = 1; } else { resultSmallStation = 0; } goto putInStatement; case TokenType.EQUAL: if (leftData == rightData) { resultSmallStation = 1; } else { resultSmallStation = 0; } goto putInStatement; case TokenType.NOT_EQUAL: if (leftData != rightData) { resultSmallStation = 1; } else { resultSmallStation = 0; } goto putInStatement; putInStatement: arichmeticStatementTree.PutToken(new TokenNumeric() { type = TokenType.NUMERIC_CONST, data = resultSmallStation }); break; } break; case TokenType.OR: arichmeticStatementTree.PutToken(new Token() { type = TokenType.PLUS }); break; case TokenType.AND: arichmeticStatementTree.PutToken(new Token() { type = TokenType.MULTIPLICATION }); break; default: throw new Exception("Something went bad in if condition Line:" + line.ToString()); } }