Esempio n. 1
0
        public static Queue <Symbol> ToPostfixQueue(Queue <Symbol> infix)
        {
            Stack <Symbol> stack   = new Stack <Symbol>();
            Queue <Symbol> postfix = new Queue <Symbol>();
            Symbol         temp_symbol;

            foreach (Symbol symbol in infix)
            {
                if (symbol is ValueSymbol)
                {
                    postfix.Enqueue(symbol);
                }
                else if (GroupSymbol.IsLeft(symbol))
                {
                    stack.Push(symbol);
                }
                else if (GroupSymbol.IsRight(symbol))
                {
                    while (stack.Count > 0)
                    {
                        temp_symbol = stack.Pop();
                        if (GroupSymbol.IsLeft(temp_symbol))
                        {
                            break;
                        }

                        postfix.Enqueue(temp_symbol);
                    }
                }
                else
                {
                    if (stack.Count > 0)
                    {
                        temp_symbol = stack.Pop();
                        while (stack.Count != 0 && OperationSymbol.Compare(temp_symbol, symbol))
                        {
                            postfix.Enqueue(temp_symbol);
                            temp_symbol = stack.Pop();
                        }

                        if (OperationSymbol.Compare(temp_symbol, symbol))
                        {
                            postfix.Enqueue(temp_symbol);
                        }
                        else
                        {
                            stack.Push(temp_symbol);
                        }
                    }

                    stack.Push(symbol);
                }
            }

            while (stack.Count > 0)
            {
                postfix.Enqueue(stack.Pop());
            }

            return(postfix);
        }
Esempio n. 2
0
        public static Queue <Symbol> ToInfixQueue(string expression)
        {
            if (tokenizer_regex == null)
            {
                Queue <string> skipped = new Queue <string>();
                StringBuilder  expr    = new StringBuilder();
                expr.Append("([\\,");

                foreach (OperatorSymbol op in OperatorSymbol.Operators)
                {
                    if (op.Name.Length > 1)
                    {
                        skipped.Enqueue(op.Name);
                        continue;
                    }

                    expr.Append("\\");
                    expr.Append(op.Name);
                }

                foreach (GroupSymbol gp in GroupSymbol.GroupSymbols)
                {
                    expr.Append("\\");
                    expr.Append(gp.Name);
                }

                expr.Append("]{1}");

                while (skipped.Count > 0)
                {
                    expr.Append("|");
                    expr.Append(skipped.Dequeue());
                }

                expr.Append(")");

                tokenizer_regex = new Regex(expr.ToString(), RegexOptions.IgnorePatternWhitespace);
            }

            string []     tokens      = tokenizer_regex.Split(expression);
            List <Symbol> symbol_list = new List <Symbol>();

            for (int i = 0; i < tokens.Length; i++)
            {
                string token      = tokens[i].Trim();
                string next_token = null;

                if (token == String.Empty)
                {
                    continue;
                }

                if (i < tokens.Length - 1)
                {
                    next_token = tokens[i + 1].Trim();
                }

                Symbol symbol = Symbol.FromString(token, next_token);
                symbol_list.Add(symbol);

                // I'm too tired to fix this at the stack/execution level right now;
                // injecting void at the parsing level for zero-arg functions
                if (symbol_list.Count >= 3 && GroupSymbol.IsRight(symbol) &&
                    GroupSymbol.IsLeft(symbol_list[symbol_list.Count - 2]) &&
                    symbol_list[symbol_list.Count - 3] is FunctionSymbol)
                {
                    symbol_list.Insert(symbol_list.Count - 1, new VoidSymbol());
                }
            }

            Queue <Symbol> queue = new Queue <Symbol>();

            foreach (Symbol symbol in symbol_list)
            {
                queue.Enqueue(symbol);
            }

            return(queue);
        }
Esempio n. 3
0
        public static bool IsRight(Symbol symbol)
        {
            GroupSymbol group = symbol as GroupSymbol;

            return(group != null && group.Direction == GroupSymbol.GroupDirection.Right);
        }