Example #1
0
 public void Remove(VariableItem v)
 {
     if (v.Name != null && _variableList.Contains(v.Name))
     {
         _variableList.Remove(v.Name);
     }
 }
Example #2
0
        public VariableItem Create(object v, string name, string statementId)
        {
            var newvar = new VariableItem(v, name);

            Add(newvar, statementId);
            return(newvar);
        }
Example #3
0
        public void Add(VariableItem v, string statementId)
        {
            v.StatementId = statementId;

            if (v.Name.Contains("@"))
            {
                if (GLOBALS.Variables.Contains(v.Name))
                {
                    GLOBALS.Variables[v.Name] = v;
                }
                else
                {
                    GLOBALS.Variables.Add(v.Name, v);
                }
            }

            if (_variableList.Contains(v.Name))
            {
                _variableList[v.Name] = v;
            }
            else
            {
                _variableList.Add(v.Name, v);
            }
        }
Example #4
0
 protected bool Equals(VariableItem other)
 {
     return(string.Equals(StatementId, other.StatementId) &&
            string.Equals(Name, other.Name) &&
            Equals(Variable, other.Variable));
 }
Example #5
0
        public VariableItem Execute(List <VariableItem> vars)
        {
            if (vars != null && vars.Count > 0)
            {
                foreach (var variableItem in vars)
                {
                    _variables.Add(variableItem, _currentFunc.Id.ToString());
                }
            }

            var lastItem        = new VariableItem("false");
            var managerialWords = new List <string>()
            {
                "if ", "elseif ", "while ", "for "
            };

            _statementList = new List <string> {
                _currentFunc.Id.ToString()
            };
            var commandList = _commands.GetCommandNameList();

            for (var pos = 0; pos < _currentFunc.Code.Count; pos++)
            {
                int?newPos;
                if ((newPos = IsJumpTo(pos)) != null)
                {
                    pos = (int)newPos; continue;
                }

                var codeItem = _currentFunc.Code[pos];
                ExecuteCodeEvent(_currentFunc.Name, codeItem);

                // remove comment string
                if (codeItem.Contains("#"))
                {
                    codeItem = codeItem.Substring(codeItem.IndexOf("#"), codeItem.Length - codeItem.IndexOf("#")).Trim();
                    if (codeItem.Length < 1)
                    {
                        continue;
                    }
                }


                // Detect managed words
                if (managerialWords.Any(codeItem.Contains))
                {
                    /* Execute jumping operators */
                    CalculateJumpOper(pos, ref _statementList);
                    if ((newPos = IsJumpTo(pos)) != null)
                    {
                        pos = (int)newPos;
                    }
                    continue;
                }


                // Code simplification
                lastItem = Simplification(pos);



                // Remove last statement id
                if (codeItem.Contains("end"))
                {
                    _variables.ClearByStatementId(_statementList[_statementList.Count - 1]);
                    _statementList.Remove(_statementList[_statementList.Count - 1]);
                }
            }

            return(lastItem);
        }
Example #6
0
        private VariableItem SimplifyExpression(List <string> exp)
        {
            var cmdList = exp;

            int index = 0;

            index = 0;
            while (index < cmdList.Count)
            {
                if (!cmdList.Any(t => t.Equals("-") || t.Equals("+") || t.Equals("*") || t.Equals("/")))
                {
                    break;
                }

                var i = index;
                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 = Guid.NewGuid().ToString().Replace("-", ""));
                    _variables.Add(rez, _statementList.Last());
                    cmdList.RemoveAll(j => j.Length < 1);
                    index = 0;
                }
                else
                {
                    index++;
                }
            }

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


            index = 0;
            while (index < cmdList.Count)
            {
                if (!cmdList.Any(t => t.Equals(">>") || t.Equals("<<")))
                {
                    break;
                }

                var i = index;
                if (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);
                        throw new Exception("ERROR :)");
                        break;

                    case "<<":
                        rez = o1 + o2;
                        break;

                    default:
                        throw new Exception("MAGIC!");
                    }
                    cmdList[i - 1] = "";
                    cmdList[i + 1] = "";
                    cmdList[i]     = (rez.Name = Guid.NewGuid().ToString().Replace("-", ""));
                    _variables.Add(rez, _statementList.Last());
                    cmdList.RemoveAll(j => j.Length < 1);
                    index = 0;
                }
                else
                {
                    index++;
                }
            }

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


            index = 0;
            while (index < cmdList.Count)
            {
                if (!cmdList.Any(t => t.Equals(">") || t.Equals("<") || t.Equals(">=") ||
                                 t.Equals("<=") || t.Equals("==") || t.Equals("!=")))
                {
                    break;
                }

                var i = index;
                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 = Guid.NewGuid().ToString().Replace("-", ""));
                    _variables.Add(rez, _statementList.Last());
                    cmdList.RemoveAll(j => j.Length < 1);
                    index = 0;
                }
                else
                {
                    index++;
                }
            }

            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 = Guid.NewGuid().ToString().Replace("-", ""));
                    _variables.Add(rez, _statementList.Last());
                    cmdList.RemoveAll(j => j.Length < 1);
                    y = 0;
                }
                else
                {
                    y++;
                }
            }

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

            index = 0;
            while (index < cmdList.Count)
            {
                if (!cmdList.Any(l => l.Equals("=")))
                {
                    break;
                }

                var i = index;
                if (cmdList[i].Equals("="))
                {
                    VariableItem rez;

                    if (_variables.Exist(cmdList[i - 1]))
                    {
                        var o1 = _variables.GetVariable(cmdList[i - 1]);
                        var o2 = _variables.GetVariable(cmdList[i + 1]);
                        rez = new VariableItem(o2, o1.Name);
                        _variables.Remove(o2);
                        _variables.Remove(o1);
                    }
                    else
                    {
                        var o2 = _variables.GetVariable(cmdList[i + 1]);
                        o2.Name = cmdList[i - 1];
                        rez     = o2;
                        //_variables.Remove(o2);
                    }

                    cmdList[i - 1] = "";
                    cmdList[i + 1] = "";
                    cmdList[i]     = rez.Name;
                    _variables.Add(rez, _statementList.Last());
                    cmdList.RemoveAll(j => j.Length < 1);
                }
                else
                {
                    index++;
                }
            }

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


            if (cmdList.Count == 1)
            {
                var ret = _variables.GetVariable(cmdList[0]);
                _variables.Add(ret, _statementList.Last());
                return(ret);
            }
            else
            {
                throw new Exception("BUG!");
            }
        }