Example #1
0
        public TreeFunctional(string name, TokenType type, int startPos, int endPos)
        {
            this.name = name;
            Token token;

            switch (type)
            {
            case TokenType.FUNCTION:
                token = new TokenFunction(null, startPos, endPos);
                break;

            case TokenType.IF:
                token = new TokenIfElse(startPos, endPos, TokenType.IF);
                break;

            default:
                token = null;
                break;
            }

            this.head          = token;
            this.next          = null;
            this.stackVariable = new Dictionary <string, TokenVariable>();
        }
Example #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));
        }