Beispiel #1
0
        public override object Process(TreeFunctional treeFunction)
        {
            MakeReversePolishNotation();

            foreach (var token in reversePolishNotation)
            {
                switch (token.type)
                {
                case TokenType.NUMERIC_CONST:
                case TokenType.VARIABLE:
                    stack.Push(token);
                    break;

                case TokenType.PLUS:
                    stack.Push(new TokenNumeric()
                    {
                        data = (double)((stack.Pop() as TokenNumeric).data) + (double)((stack.Pop() as TokenNumeric).data)
                    });
                    break;

                case TokenType.MINUS:
                    var right = (double)((stack.Pop() as TokenNumeric).data);
                    var left  = (double)((stack.Pop() as TokenNumeric).data);
                    stack.Push(new TokenNumeric()
                    {
                        data = left - right
                    });
                    break;

                case TokenType.MULTIPLICATION:
                    stack.Push(new TokenNumeric()
                    {
                        data = (double)((stack.Pop() as TokenNumeric).data) * (double)((stack.Pop() as TokenNumeric).data)
                    });
                    break;

                case TokenType.DIVIDE:
                    right = (double)((stack.Pop() as TokenNumeric).data);
                    left  = (double)((stack.Pop() as TokenNumeric).data);
                    stack.Push(new TokenNumeric()
                    {
                        data = left / right
                    });
                    break;

                default:
                    throw new Exception("something bad in get priority!");
                }
            }
            object data = (stack.Pop() as TokenNumeric).data;

            if (tokenToChange != null && treeFunction != null)
            {
                treeFunction.UpdateVar(tokenToChange.name, data, VariableType.NUMERIC);
            }

            return(data);
        }
Beispiel #2
0
        void FindMain()
        {
            int start = FindFunction("main");

            while (this.text[++start] != '{')
            {
                ;
            }
            int end = start;

            this.mainTree = new TreeFunctional("main", TokenType.FUNCTION, start, EndBracket(start, "main"));
        }
Beispiel #3
0
 public void UpdateValueInInfixNotation(TreeFunctional tree)
 {
     this.RemoveReverseNotations();
     foreach (var token in this.infixNotation)
     {
         if (token.type == TokenType.VARIABLE)
         {
             var tokenVar = (token as TokenVariable);
             tokenVar.data = tree.GetVar(tokenVar.name).data;
         }
     }
 }
Beispiel #4
0
        public override object Process(TreeFunctional treeFunction)
        {
            string str = "";

            foreach (var token in strings)
            {
                str += (string)token.data;
            }
            if (treeFunction != null)
            {
                treeFunction.UpdateVar(tokenToChange.name, str, VariableType.STRING);
            }
            return(str);
        }
Beispiel #5
0
        Token Process(TreeFunctional tree)
        {
            TokenLogic tokenHead = tree.head as TokenLogic;

            for (int i = tokenHead.startPos + 1; i < tokenHead.endPos;)
            {
                Token currentToken = GetNextToken(ref i, tree);
                if (currentToken == null)
                {
                    break;
                }
                switch (currentToken.type)
                {
                case TokenType.VARIABLE:
                    ProcessVariable(tree, ref i, ref currentToken);
                    break;

                case TokenType.IF:
                    var token = ProcessIfElseStatement(tree, ref i, ref currentToken);
                    if (token != null)
                    {
                        return(token);
                    }
                    break;

                case TokenType.FOR:
                    ProcessFor(tree, ref i, ref currentToken);
                    break;

                case TokenType.FUNCTION:
                    ProcessFunction(tree, ref i, ref currentToken);
                    break;

                case TokenType.RETURN:
                    currentToken = GetNextToken(ref i, tree);
                    if (currentToken is TokenLogic)
                    {
                        return(ProcessFunction(tree, ref i, ref currentToken));
                    }
                    return(currentToken);
                }
                if (tree.next != null)
                {
                    tree.next.Process(tree);
                }
                tree.next = null;
            }
            return(null);
        }
Beispiel #6
0
 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>();
 }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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");
                }
            }
        }
Beispiel #9
0
        private Token ProcessIfElseStatement(TreeFunctional tree, ref int i, ref Token currentToken)
        {
            TreeIf ifTree = new TreeIf(currentToken as TokenIfElse, tree);

            // process statement

            currentToken = GetNextToken(ref i, tree);
            int deep;

            if (currentToken.type == TokenType.ARITHMETIC_BRACKET_OPEN)
            {
                deep = 1;
                ifTree.PutTokenInStatement(currentToken, null, null, i);
            }
            else
            {
                throw new Exception("Expected open bracket in if statement line: " + i.ToString());
            }

            while (deep != 0)
            {
                currentToken = GetNextToken(ref i, tree);
                if (currentToken.type == TokenType.ARITHMETIC_BRACKET_OPEN)
                {
                    deep++;
                }
                else
                if (currentToken.type == TokenType.ARITHMETIC_BRACKET_CLOSE)
                {
                    deep--;
                }
                if (currentToken.type == TokenType.VARIABLE || currentToken.type == TokenType.NUMERIC_CONST)
                {
                    var tokenOperation = GetNextToken(ref i, tree);
                    var rightVariable  = GetNextToken(ref i, tree);
                    ifTree.PutTokenInStatement(currentToken, tokenOperation, rightVariable, i);
                }
                else
                {
                    ifTree.PutTokenInStatement(currentToken, null, null, i);
                }
            }
            --i;
            while (this.text[++i] != '{')
            {
                ;
            }
            (ifTree.head as TokenIfElse).startPos = i;
            i = (ifTree.head as TokenIfElse).endPos + 1;

            if (ifTree.ProcessStatement())
            {
                var retToken = Process(ifTree);
                if (retToken != null)
                {
                    return(retToken);
                }
            }
            else
            {
                int tmp = i;
                currentToken = GetNextToken(ref tmp, tree);
                if (currentToken != null && currentToken.type == TokenType.ELSE)
                {
                    --i;
                    while (this.text[++i] != '{')
                    {
                        ;
                    }
                    (currentToken as TokenIfElse).startPos = i;
                    ifTree.head = currentToken;
                    var retToken = Process(ifTree);
                    if (retToken != null)
                    {
                        return(retToken);
                    }
                    i = (ifTree.head as TokenIfElse).endPos + 1;
                }
            }
            return(null);
        }
Beispiel #10
0
        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;
        }
Beispiel #11
0
        private Token ProcessFunction(TreeFunctional tree, ref int i, ref Token currentToken)
        {
            TokenFunction funcToken = currentToken as TokenFunction;

            currentToken = GetNextToken(ref i, tree);

            if (BuiltInFunction.IsBuiltIn(funcToken.name))
            {
                if (currentToken.type != TokenType.ARITHMETIC_BRACKET_OPEN)
                {
                    throw new Exception("Expected open bracket after calling fuction!");
                }
                List <TokenNumeric> paramets = new List <TokenNumeric>();
                int currentPos = i;
                currentToken = GetNextToken(ref i, tree);

                while (currentToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE && currentToken.type != TokenType.END_OP)
                {
                    if (currentToken is TokenLogic)
                    {
                        currentToken = ProcessFunction(tree, ref i, ref currentToken);
                    }
                    Token nextToken = GetNextToken(ref i, tree);

                    if (nextToken.type != TokenType.COMA && nextToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE)
                    {
                        i = currentPos;
                        ProcessVariable(tree, ref i, ref currentToken, false);

                        (currentToken as TokenNumeric).data = tree.next.Process(null);
                        tree.next = null;
                    }

                    if (currentToken.type != TokenType.COMA && currentToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE)
                    {
                        paramets.Add(currentToken as TokenNumeric);
                    }
                    currentPos = i;
                    if (nextToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE)
                    {
                        currentToken = GetNextToken(ref i, tree);
                    }
                    else
                    {
                        currentToken = nextToken;
                    }
                }
                return(BuiltInFunction.ProcessFunction(funcToken.name, paramets));
            }
            TreeFunctional funcTree = new TreeFunctional(funcToken.name, TokenType.FUNCTION, funcToken.startPos, funcToken.endPos);

            var tokenInDefinition = GetNextToken(ref funcToken.startPos, null);

            if (tokenInDefinition.type != TokenType.ARITHMETIC_BRACKET_OPEN)
            {
                throw new Exception("Expected open bracket in callin function" + funcToken.name);
            }
            currentToken      = GetNextToken(ref i, tree);
            tokenInDefinition = GetNextToken(ref funcToken.startPos, null);
            while (currentToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE && tokenInDefinition.type != TokenType.ARITHMETIC_BRACKET_CLOSE)
            {
                if (currentToken.type == TokenType.NUMERIC_CONST)
                {
                    currentToken = (currentToken as TokenConst).ConvertToTokenVariable((tokenInDefinition as TokenVariable).name);
                }
                else
                {
                    (currentToken as TokenVariable).name = (tokenInDefinition as TokenVariable).name;
                }
                funcTree.PutVariableinStack(currentToken as TokenVariable);

                currentToken      = GetNextToken(ref i, tree);
                tokenInDefinition = GetNextToken(ref funcToken.startPos, null);
                if (currentToken.type == TokenType.COMA && tokenInDefinition.type == TokenType.COMA)
                {
                    currentToken      = GetNextToken(ref i, tree);
                    tokenInDefinition = GetNextToken(ref funcToken.startPos, null);
                }
                else
                if (currentToken.type != TokenType.ARITHMETIC_BRACKET_CLOSE && tokenInDefinition.type != TokenType.ARITHMETIC_BRACKET_CLOSE)
                {
                    throw new Exception("Expect coma operator in functions paramets");
                }
            }
            while (this.text[(funcTree.head as TokenLogic).startPos++] != '{')
            {
                ;
            }

            return(this.Process(funcTree));
        }
Beispiel #12
0
 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;
 }
Beispiel #13
0
 abstract public object Process(TreeFunctional treeFunction);