Exemplo n.º 1
0
 protected bool Equals(VariableItem other)
 {
     return(string.Equals(StatementId, other.StatementId) &&
            string.Equals(Name, other.Name) &&
            Type == other.Type &&
            Equals(Variable, other.Variable));
 }
Exemplo n.º 2
0
        private VariableItem SimplifyExpression(List <string> exp)
        {
            var cmdList = exp;


            for (int i = 0; i < cmdList.Count; i++)
            {
                if (cmdList[i].Equals("-") || cmdList[i].Equals("+") ||
                    cmdList[i].Equals("*") || cmdList[i].Equals("/"))
                {
                    var          o1 = _variables.GetVariable(cmdList[i - 1]);
                    var          o2 = _variables.GetVariable(cmdList[i + 1]);
                    VariableItem rez;

                    switch (cmdList[i])
                    {
                    case "+": rez = o1 + o2; break;

                    case "-": rez = o1 - o2; break;

                    case "*": rez = o1 * o2; break;

                    case "/": rez = o1 / o2; break;

                    default: throw new Exception("MAGIC!");
                    }
                    cmdList[i - 1] = "";
                    cmdList[i + 1] = "";
                    cmdList[i]     = rez.Name;
                    _variables.Add(rez);
                    cmdList.RemoveAll(j => j.Length < 1);
                }
            }

            cmdList.RemoveAll(i => i.Length < 1);



            for (int i = 0; i < cmdList.Count; i++)
            {
                if (cmdList[i].Equals(">") || cmdList[i].Equals(">=") ||
                    cmdList[i].Equals("<") || cmdList[i].Equals("<=") ||
                    cmdList[i].Equals("==") || cmdList[i].Equals("!="))
                {
                    var          o1 = _variables.GetVariable(cmdList[i - 1]);
                    var          o2 = _variables.GetVariable(cmdList[i + 1]);
                    VariableItem rez;

                    switch (cmdList[i])
                    {
                    case ">": rez = new VariableItem(o1 > o2); break;

                    case ">=": rez = new VariableItem(o1 >= o2); break;

                    case "<": rez = new VariableItem(o1 < o2); break;

                    case "<=": rez = new VariableItem(o1 <= o2); break;

                    case "==": rez = new VariableItem(o1 == o2); break;

                    case "!=": rez = new VariableItem(o1 != o2); break;

                    default: throw new Exception("MAGIC!");
                    }
                    cmdList[i - 1] = "";
                    cmdList[i + 1] = "";
                    cmdList[i]     = rez.Name;
                    _variables.Add(rez);
                    cmdList.RemoveAll(j => j.Length < 1);
                }
            }

            cmdList.RemoveAll(i => i.Length < 1);

            int y = 0;

            while (true)
            {
                if (!cmdList.Any(i => i.Equals("and", StringComparison.OrdinalIgnoreCase) ||
                                 i.Equals("or", StringComparison.OrdinalIgnoreCase)))
                {
                    break;
                }

                if (cmdList[y].Equals("and", StringComparison.OrdinalIgnoreCase) ||
                    cmdList[y].Equals("or", StringComparison.OrdinalIgnoreCase))
                {
                    var          o1 = _variables.GetVariable(cmdList[y - 1]);
                    var          o2 = _variables.GetVariable(cmdList[y + 1]);
                    VariableItem rez;

                    switch (cmdList[y])
                    {
                    case "and":
                        rez = new VariableItem(o1 & o2);
                        break;

                    case "or":
                        rez = new VariableItem(o1 | o2);
                        break;

                    default:
                        throw new Exception("MAGIC!");
                    }

                    cmdList[y - 1] = "";
                    cmdList[y + 1] = "";
                    cmdList[y]     = rez.Name;
                    _variables.Add(rez);
                    cmdList.RemoveAll(j => j.Length < 1);
                    y = 0;
                }
                else
                {
                    y++;
                }
            }

            cmdList.RemoveAll(i => i.Length < 1);

            if (cmdList.Count == 1)
            {
                return(_variables.GetVariable(cmdList[0]));
            }
            else
            {
                throw new Exception("BUG!");
            }
        }
Exemplo n.º 3
0
 public void Add(VariableItem v)
 {
     _variableList.Add(v.Name, v);
 }