示例#1
0
        private static Expression InternalCompile(LinkedListNode <Token> first, LinkedListNode <Token> last, int level = 0)
        {
            if (first.Value.Type == TokenType.SBracket && MatchingBracketForward(first) == last)
            {
                return(InternalCompile(first.Next, last.Previous, 0));
            }
            else if (first.Value.Type == TokenType.Negation && first.Next.Value.Type == TokenType.SBracket && MatchingBracketForward(first.Next) == last)
            {
                return(new Not(InternalCompile(first.Next.Next, last.Previous, 0)));
            }
            LinkedListNode <Token> token;
            int brackets = 0;

            switch (level)
            {
            case 0:
                Debug.Assert(level == 0);
                // Equality
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == "=")
                        {
                            if (token.Previous.Value.Type == TokenType.Negation)
                            {
                                return(new XOr(InternalCompile(first, token.Previous.Previous, level), InternalCompile(token.Next, last, level)));
                            }
                            else
                            {
                                return(new Iff(InternalCompile(first, token.Previous, level), InternalCompile(token.Next, last, level)));
                            }
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                Debug.Assert(brackets == 0, "Unbalanced brackets");
                level++;
                goto case 1;

            case 1:
                Debug.Assert(level == 1);
                // Implication
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == ">")
                        {
                            if (token.Previous.Value.Type == TokenType.Negation)
                            {
                                throw new Exception("Implication operator cannot be negated. Tokens: " + ContainerUtils.AsString(first, last));
                            }
                            return(new Implies(InternalCompile(first, token.Previous, level), InternalCompile(token.Next, last, level)));
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 2;

            case 2:
                Debug.Assert(level == 2);
                // Or
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == "+" || token.Value.Value == "|")
                        {
                            if (token.Previous.Value.Type == TokenType.Negation)
                            {
                                return(new NOr(InternalCompile(first, token.Previous.Previous, level), InternalCompile(token.Next, last, level)));
                            }
                            else
                            {
                                return(new Or(InternalCompile(first, token.Previous, level), InternalCompile(token.Next, last, level)));
                            }
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 3;

            case 3:
                Debug.Assert(level == 3);
                // And
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == "*" || token.Value.Value == "&")
                        {
                            if (token.Previous.Value.Type == TokenType.Negation)
                            {
                                return(new NAnd(InternalCompile(first, token.Previous.Previous, level), InternalCompile(token.Next, last, level)));
                            }
                            else
                            {
                                return(new And(InternalCompile(first, token.Previous, level), InternalCompile(token.Next, last, level)));
                            }
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 4;

            case 4:
                Debug.Assert(level == 4);
                // Variables
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Type == TokenType.Identifier)
                        {
                            if (token != first && token.Previous.Value.Type == TokenType.Negation)
                            {
                                return(new Not(new Variable(token.Value.Value)));
                            }
                            else
                            {
                                return(new Variable(token.Value.Value));
                            }
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 5;

            case 5:
                Debug.Assert(level == 5);
                // Literals
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Type == TokenType.Value)
                        {
                            return(new ValueExpression(token.Value.Value == "1"));
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                //level++;
                goto default;

            default:
                throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
            }
        }
示例#2
0
        private static Expression InternalCompile(LinkedListNode <Token> first, LinkedListNode <Token> last, FunctionManager functions, int level = 0)
        {
            if (first.Value.Type == TokenType.SBracket && MatchingBracketForward(first) == last)
            {
                return(InternalCompile(first.Next, last.Previous, functions, 0));
            }
            LinkedListNode <Token> token;
            int brackets = 0;

            switch (level)
            {
            case 0:
                Debug.Assert(level == 0);
                // Additions and subtractions
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == "+")
                        {
                            return(functions.Operator("+").Create(InternalCompile(first, token.Previous, functions, level), InternalCompile(token.Next, last, functions, level)));
                        }
                        if (token.Value.Value == "-")
                        {
                            return(functions.Operator("-").Create(InternalCompile(first, token.Previous, functions, level), InternalCompile(token.Next, last, functions, level)));
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                Debug.Assert(brackets == 0, "Unbalanced brackets");
                level++;
                goto case 1;

            case 1:
                Debug.Assert(level == 1);
                // Multiplications and divisions
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == "*")
                        {
                            return(functions.Operator("*").Create(InternalCompile(first, token.Previous, functions, level), InternalCompile(token.Next, last, functions, level)));
                        }
                        if (token.Value.Value == "/")
                        {
                            return(functions.Operator("/").Create(InternalCompile(first, token.Previous, functions, level), InternalCompile(token.Next, last, functions, level)));
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 2;

            case 2:
                Debug.Assert(level == 2);
                // Powers
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Value == "^")
                        {
                            return(functions.Operator("^").Create(InternalCompile(first, token.Previous, functions, level), InternalCompile(token.Next, last, functions, level)));
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 3;

            case 3:
                Debug.Assert(level == 3);
                // Functions & variables
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Type == TokenType.Identifier)
                        {
                            if (token.Next != null && token.Next.Value.Type == TokenType.SBracket)
                            {
                                return(functions.Function(token.Value.Value).Create(FunctionArgumentsCompile(token.Next.Next, MatchingBracketForward(token.Next), functions)));
                            }
                            else
                            {
                                return(new Variable(token.Value.Value));
                            }
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                level++;
                goto case 4;

            case 4:
                Debug.Assert(level == 4);
                // Literals
                token = last;
                while (token != first.Previous)
                {
                    if (token.Value.Type == TokenType.SBracket)
                    {
                        brackets--;
                    }
                    else if (token.Value.Type == TokenType.EBracket)
                    {
                        brackets++;
                    }
                    else if (brackets == 0)
                    {
                        if (token.Value.Type == TokenType.Value)
                        {
                            return(new ValueExpression(double.Parse(token.Value.Value)));
                        }
                    }
                    else if (brackets <= 0)
                    {
                        throw new Exception("SumTingWong. Tokens: " + ContainerUtils.AsString(first, last));
                    }
                    token = token.Previous;
                }
                if (brackets != 0)
                {
                    throw new Exception("Unbalanced brackets");
                }
                //level++;
                goto default;

            default:
                throw new Exception();
            }
        }