Esempio n. 1
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);
        }
Esempio n. 2
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));
        }