public ArichmetichTree(TokenVariable tokenToChange) { this.tokenToChange = tokenToChange; infixNotation = new List <Token>(); reversePolishNotation = new List <Token>(); stack = new Stack <Token>(); }
public StringTree(TokenVariable tokenToChange) { if (tokenToChange.varType == VariableType.NUMERIC) { throw new Exception("Something bad in constructor in String tree!"); } this.tokenToChange = tokenToChange; strings = new List <TokenVariable>(); }
public void AddToken(TokenVariable token) { if (token.varType == VariableType.STRING) { strings.Add(token); } else { throw new Exception("Something bad in constructor in String tree!"); } }
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 Token GetNextToken(ref int pos, TreeFunctional tree) { char tmp = text[pos]; while (this.text[pos] == ' ' || this.text[pos] == '\n' || this.text[pos] == '\t') { ++pos; } if (char.IsLetter(this.text[pos])) // is var { int start = pos; while (char.IsLetter(this.text[++pos]) || this.text[pos] == '_') { ; } string nameVar = this.text.Substring(start, pos - start); if (BuiltInFunction.IsBuiltIn(nameVar)) { --pos; while (this.text[++pos] != '(') { ; } return(new TokenFunction(nameVar, start, pos)); } switch (nameVar) { case "if": return(new TokenIfElse(pos, EndBracket(pos, "if"), TokenType.IF)); case "else": return(new TokenIfElse(pos, EndBracket(pos, "else"), TokenType.ELSE)); case "for": start = pos; while (this.text[++start] != '{') { ; } return(new TokenFor(start, EndBracket(start, "for"), TokenType.FOR)); case "AND": return(new Token() { type = TokenType.AND }); case "OR": return(new Token() { type = TokenType.OR }); case "MORE": return(new Token() { type = TokenType.MORE }); case "LESS": return(new Token() { type = TokenType.LESS }); case "EQ": return(new Token() { type = TokenType.EQUAL }); case "NEQ": return(new Token() { type = TokenType.NOT_EQUAL }); case "return": return(new Token() { type = TokenType.RETURN }); } int checkBarcket = pos - 1; while (this.text[++checkBarcket] == ' ') { ; } if (this.text[checkBarcket] == '(') { start = FindFunction(nameVar); return(new TokenFunction(nameVar, start, EndBracket(start, nameVar))); } if (tree == null) { TokenVariable var = new TokenVariable(nameVar); return(var); } else { if (tree.VariableExist(nameVar)) { return(tree.GetVar(nameVar)); } else { TokenVariable var = new TokenVariable(nameVar); tree.PutVariableinStack(var); return(var); } } } if (text[pos] == '=') { ++pos; return(new Token() { type = TokenType.ASSIGN }); } if (text[pos] == '+') { ++pos; return(new Token() { type = TokenType.PLUS }); } if (text[pos] == '-') { ++pos; return(new Token() { type = TokenType.MINUS }); } if (text[pos] == '*') { ++pos; return(new Token() { type = TokenType.MULTIPLICATION }); } if (text[pos] == '/') { ++pos; return(new Token() { type = TokenType.PLUS }); } if (text[pos] == '(') { ++pos; return(new Token() { type = TokenType.ARITHMETIC_BRACKET_OPEN }); } if (text[pos] == ')') { ++pos; return(new Token() { type = TokenType.ARITHMETIC_BRACKET_CLOSE }); } if (text[pos] == ',') { ++pos; return(new Token() { type = TokenType.COMA }); } if (text[pos] == '\"') { int start = pos; char c; while ((c = text[++pos]) != '\"') { if (c == '\n') { throw new Exception("Not close string in line: " + pos.ToString()); } } string str = this.text.Substring(start + 1, pos - start - 1); ++pos; return(new TokenVariable(null) { type = TokenType.VARIABLE, varType = VariableType.STRING, data = str }); } if (char.IsDigit(this.text[pos])) // is Const { int start = pos; while (char.IsDigit(this.text[++pos])) { ; } string str = this.text.Substring(start, pos - start); double value = Convert.ToDouble(str); return(new TokenConst() { type = TokenType.NUMERIC_CONST, data = value }); } if (this.text[pos] == ';') { ++pos; return(new Token() { type = TokenType.END_OP }); } return(null); }
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; }
public override void PutVariableinStack(TokenVariable var) { stackVariableLocal.Add(var.name, var); }
public virtual void PutVariableinStack(TokenVariable var) { stackVariable.Add(var.name, var); }