Exemplo n.º 1
0
 static string CreateVariableEntry(Variable var, string name, bool isLocal = false)
 {
     try
     {
         string value       = var.AsString(true, true, 16);
         string localGlobal = isLocal ? "0" : "1";
         string varData     = name + ":" + localGlobal + ":" +
                              Constants.TypeToString(var.Type).ToLower() + ":" + value;
         return(varData.Trim());
     }
     catch (Exception exc)
     {
         // TODO: Clean up not used objects.
         bool removed = isLocal ? PopLocalVariable(name) : RemoveGlobal(name);
         Console.WriteLine("Object {0} is probably dead ({1}): {2}. Removing it.", name, removed, exc);
         return(null);
     }
 }
Exemplo n.º 2
0
        static void MergeStrings(Variable leftCell, Variable rightCell)
        {
            switch (leftCell.Action)
            {
            case "+":
                leftCell.String = leftCell.AsString() + rightCell.AsString();
                break;

            case "<":
                string arg1 = leftCell.AsString();
                string arg2 = rightCell.AsString();
                leftCell.Value = Convert.ToDouble(string.Compare(arg1, arg2) < 0);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) > 0);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) <= 0);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) >= 0);
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) == 0);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) != 0);
                break;

            case ")":
                break;

            default:
                throw new ArgumentException("Can't perform action [" +
                                            leftCell.Action + "] on strings");
            }
        }
Exemplo n.º 3
0
        // Merge Strings
        // Сливане на Strings
        private static void MergeStrings(Variable leftCell, Variable rightCell)
        {
            switch (leftCell.Action)
            {
            case "+":
                leftCell.String += rightCell.AsString();
                break;

            case "<":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.String, rightCell.String) < 0);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.String, rightCell.String) > 0);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.String, rightCell.String) <= 0);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.String, rightCell.String) >= 0);
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.String, rightCell.String) == 0);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.String, rightCell.String) != 0);
                break;

            default:

                throw new ArgumentException("Can't perform action [" +
                                            leftCell.Action + "] on strings");
            }
        }
Exemplo n.º 4
0
        async Task <string> DebugScript()
        {
            if (string.IsNullOrWhiteSpace(m_script))
            {
                return(null);
            }

            m_debugging = new ParsingScript(m_script, 0, m_char2Line);
            m_debugging.OriginalScript = m_script;

            Variable result = Variable.EmptyInstance;

            while (m_debugging.Pointer < m_script.Length)
            {
                result = await ProcessNext();
            }

            return(result.AsString());
        }
Exemplo n.º 5
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY);

            Utils.CheckNotEmpty(script, varName, m_name);

            // 2. Get the current value of the variable.
            ParserFunction func         = ParserFunction.GetFunction(varName);
            Variable       currentValue = func.GetValue(script);

            // 3. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.AsString();

            Variable newValue = new Variable(arg.ToLower());

            return(newValue);
        }
Exemplo n.º 6
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string substring;

            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.NEXT_ARG_ARRAY);

            Utils.CheckNotEmpty(script, varName, m_name);

            // 2. Get the current value of the variable.
            ParserFunction func         = ParserFunction.GetFunction(varName);
            Variable       currentValue = func.GetValue(script);

            // 3. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.AsString();
            // 4. Get the initial index of the substring.
            Variable init = Utils.GetItem(script);

            Utils.CheckNonNegativeInt(init);

            // 5. Get the length of the substring if available.
            bool lengthAvailable = Utils.SeparatorExists(script);

            if (lengthAvailable)
            {
                Variable length = Utils.GetItem(script);
                Utils.CheckPosInt(length);
                if (init.Value + length.Value > arg.Length)
                {
                    throw new ArgumentException("The total substring length is larger than [" +
                                                arg + "]");
                }
                substring = arg.Substring((int)init.Value, (int)length.Value);
            }
            else
            {
                substring = arg.Substring((int)init.Value);
            }
            Variable newValue = new Variable(substring);

            return(newValue);
        }
Exemplo n.º 7
0
        protected override Variable Evaluate(ParsingScript script)
        {
            bool            isList = false;
            List <Variable> args   = Utils.GetArgs(script,
                                                   Constants.START_ARG, Constants.END_ARG, out isList);

            Utils.CheckArgs(args.Count, 3, m_name);

            string   varName  = Utils.GetSafeString(args, 0);
            Variable lines    = Utils.GetSafeVariable(args, 1);
            int      fromLine = Utils.GetSafeInt(args, 2);
            string   hash2    = Utils.GetSafeString(args, 3);
            string   sepStr   = Utils.GetSafeString(args, 4, "\t");

            if (sepStr == "\\t")
            {
                sepStr = "\t";
            }
            char[] sep = sepStr.ToCharArray();

            var      function = ParserFunction.GetFunction(varName);
            Variable mapVar   = function != null?function.GetValue(script) :
                                    new Variable(Variable.VarType.ARRAY);

            for (int counter = fromLine; counter < lines.Tuple.Count; counter++)
            {
                Variable lineVar = lines.Tuple[counter];
                Variable toAdd   = new Variable(counter - fromLine);
                string   line    = lineVar.AsString();
                var      tokens  = line.Split(sep);
                string   hash    = tokens[0];
                mapVar.AddVariableToHash(hash, toAdd);
                if (!string.IsNullOrWhiteSpace(hash2) &&
                    !hash2.Equals(hash, StringComparison.OrdinalIgnoreCase))
                {
                    mapVar.AddVariableToHash(hash2, toAdd);
                }
            }

            ParserFunction.AddGlobalOrLocalVariable(varName,
                                                    new GetVarFunction(mapVar));
            return(Variable.EmptyInstance);
        }
Exemplo n.º 8
0
        public static double GetSafeDouble(List <Variable> args, int index, double defaultValue = 0.0)
        {
            if (args.Count <= index)
            {
                return(defaultValue);
            }

            Variable numberVar = args[index];

            if (numberVar.Type != Variable.VarType.NUMBER)
            {
                double num;
                if (!CanConvertToDouble(numberVar.String, out num))
                {
                    throw new ArgumentException("Expected a double instead of [" + numberVar.AsString() + "]");
                }
                return(num);
            }
            return(numberVar.AsDouble());
        }
Exemplo n.º 9
0
        public static Variable ExtractArrayElement(Variable array,
                                                   List <Variable> indices)
        {
            Variable currLevel = array;

            for (int i = 0; i < indices.Count; i++)
            {
                Variable index      = indices [i];
                int      arrayIndex = currLevel.GetArrayIndex(index);

                int tupleSize = currLevel.Tuple != null ? currLevel.Tuple.Count : 0;
                if (arrayIndex < 0 || arrayIndex >= tupleSize)
                {
                    throw new ArgumentException("Unknown index [" + index.AsString() +
                                                "] for tuple of size " + tupleSize);
                }
                currLevel = currLevel.Tuple [arrayIndex];
            }
            return(currLevel);
        }
Exemplo n.º 10
0
        public static int GetSafeInt(List <Variable> args, int index, int defaultValue = 0)
        {
            if (args.Count <= index)
            {
                return(defaultValue);
            }
            Variable numberVar = args[index];

            if (numberVar.Type != Variable.VarType.NUMBER)
            {
                int num;
                if (!Int32.TryParse(numberVar.String, NumberStyles.Number,
                                    CultureInfo.InvariantCulture, out num))
                {
                    throw new ArgumentException("Expected an integer instead of [" + numberVar.AsString() + "]");
                }
                return(num);
            }
            return(numberVar.AsInt());
        }
Exemplo n.º 11
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string methodName = Utils.GetItem(script).AsString();

            Utils.CheckNotEmpty(script, methodName, m_name);
            script.MoveForwardIf(Constants.NEXT_ARG);

            string paramName = Utils.GetToken(script, Constants.NEXT_ARG_ARRAY);

            Utils.CheckNotEmpty(script, paramName, m_name);
            script.MoveForwardIf(Constants.NEXT_ARG);

            Variable paramValueVar = Utils.GetItem(script);
            string   paramValue    = paramValueVar.AsString();

            var result = Statics.InvokeCall(typeof(Statics),
                                            methodName, paramName, paramValue);

            return(result);
        }
Exemplo n.º 12
0
        public void Sort()
        {
            if (Tuple == null || Tuple.Count <= 1)
            {
                return;
            }

            List <double> numbers = new List <double>();
            List <string> strings = new List <string>();

            for (int i = 0; i < Tuple.Count; i++)
            {
                Variable arg = Tuple[i];
                if (arg.Tuple != null)
                {
                    arg.Sort();
                }
                else if (arg.Type == VarType.NUMBER)
                {
                    numbers.Add(arg.AsDouble());
                }
                else
                {
                    strings.Add(arg.AsString());
                }
            }
            List <Variable> newTuple = new List <Variable>(Tuple.Count);

            numbers.Sort();
            strings.Sort();

            for (int i = 0; i < numbers.Count; i++)
            {
                newTuple.Add(new Variable(numbers[i]));
            }
            for (int i = 0; i < strings.Count; i++)
            {
                newTuple.Add(new Variable(strings[i]));
            }
            Tuple = newTuple;
        }
Exemplo n.º 13
0
        protected override Variable Evaluate(ParsingScript script)
        {
            bool            isList = false;
            List <Variable> args   = Utils.GetArgs(script,
                                                   Constants.START_ARG, Constants.END_ARG, out isList);

            Utils.CheckArgs(args.Count, 3, m_name);

            string   varName  = Utils.GetSafeString(args, 0);
            Variable lines    = Utils.GetSafeVariable(args, 1);
            int      fromLine = Utils.GetSafeInt(args, 2);
            string   sepStr   = Utils.GetSafeString(args, 3, "\t");

            if (sepStr == "\\t")
            {
                sepStr = "\t";
            }
            char[] sep = sepStr.ToCharArray();

            var      function     = ParserFunction.GetFunction(varName);
            Variable allTokensVar = new Variable(Variable.VarType.ARRAY);

            for (int counter = fromLine; counter < lines.Tuple.Count; counter++)
            {
                Variable lineVar   = lines.Tuple[counter];
                Variable toAdd     = new Variable(counter - fromLine);
                string   line      = lineVar.AsString();
                var      tokens    = line.Split(sep);
                Variable tokensVar = new Variable(Variable.VarType.ARRAY);
                foreach (string token in tokens)
                {
                    tokensVar.Tuple.Add(new Variable(token));
                }
                allTokensVar.Tuple.Add(tokensVar);
            }

            ParserFunction.AddGlobalOrLocalVariable(varName,
                                                    new GetVarFunction(allTokensVar));

            return(Variable.EmptyInstance);
        }
Exemplo n.º 14
0
        public virtual string AsString(bool isList   = true,
                                       bool sameLine = true)
        {
            if (Type == VarType.NUMBER)
            {
                return(Value.ToString());
            }
            if (Type == VarType.STRING)
            {
                return(m_string == null ? "" : m_string);
            }
            if (Type == VarType.NONE || m_tuple == null)
            {
                return(string.Empty);
            }

            StringBuilder sb = new StringBuilder();

            if (isList)
            {
                sb.Append(Constants.START_GROUP.ToString() +
                          (sameLine ? "" : Environment.NewLine));
            }
            for (int i = 0; i < m_tuple.Count; i++)
            {
                Variable arg = m_tuple[i];
                sb.Append(arg.AsString(isList, sameLine));
                if (i != m_tuple.Count - 1)
                {
                    sb.Append(sameLine ? " " : Environment.NewLine);
                }
            }
            if (isList)
            {
                sb.Append(Constants.END_GROUP.ToString() +
                          (sameLine ? " " : Environment.NewLine));
            }

            return(sb.ToString());
        }
Exemplo n.º 15
0
        private static void ProcessScript(string script, string filename = "")
        {
            s_PrintingCompleted = false;
            string   errorMsg = null;
            Variable result   = null;

            try
            {
                result = System.Threading.Tasks.Task.Run(() => Interpreter.Instance.Process(script, filename)).Result;
                //result = Interpreter.Instance.Process(script);
            }
            catch (Exception exc)
            {
                errorMsg = exc.InnerException != null ? exc.InnerException.Message : exc.Message;
                ParserFunction.InvalidateStacksAfterLevel(0);
            }

            if (!s_PrintingCompleted)
            {
                string output = Interpreter.Instance.Output;
                if (!string.IsNullOrWhiteSpace(output))
                {
                    Console.WriteLine(output);
                }
                else if (result != null)
                {
                    output = result.AsString(false, false);
                    if (!string.IsNullOrWhiteSpace(output))
                    {
                        Console.WriteLine(output);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                Utils.PrintColor(errorMsg + Environment.NewLine, ConsoleColor.Red);
                errorMsg = string.Empty;
            }
        }
Exemplo n.º 16
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.NEXT_ARG_ARRAY);

            Utils.CheckNotEmpty(script, varName, m_name);

            // 2. Get the current value of the variable.
            ParserFunction func         = ParserFunction.GetFunction(varName);
            Variable       currentValue = func.GetValue(script);

            // 3. Get the value to be looked for.
            Variable searchValue = Utils.GetItem(script);

            // 4. Apply the corresponding C# function.
            string basePart = currentValue.AsString();
            string search   = searchValue.AsString();

            int result = basePart.IndexOf(search);

            return(new Variable(result));
        }
Exemplo n.º 17
0
        public static double GetSafeDouble(List <Variable> args, int index, double defaultValue = 0.0)
        {
            if (args.Count <= index)
            {
                return(defaultValue);
            }

            Variable numberVar = args[index];

            if (numberVar.Type != Variable.VarType.NUMBER)
            {
                double num;
                if (!Double.TryParse(numberVar.String, NumberStyles.Number |
                                     NumberStyles.AllowExponent |
                                     NumberStyles.Float,
                                     CultureInfo.InvariantCulture, out num))
                {
                    throw new ArgumentException("Expected a double instead of [" + numberVar.AsString() + "]");
                }
                return(num);
            }
            return(numberVar.AsDouble());
        }
Exemplo n.º 18
0
        private static int ExtendArrayHelper(Variable parent, Variable indexVar)
        {
            parent.SetAsArray();

            int arrayIndex = parent.GetArrayIndex(indexVar);

            if (arrayIndex < 0)
            {
                // This is not a "normal index" but a new string for the dictionary.
                string hash = indexVar.AsString();
                arrayIndex = parent.SetHashVariable(hash, Variable.NewEmpty());
                return(arrayIndex);
            }

            if (parent.Tuple.Count <= arrayIndex)
            {
                for (int i = parent.Tuple.Count; i <= arrayIndex; i++)
                {
                    parent.Tuple.Add(Variable.NewEmpty());
                }
            }
            return(arrayIndex);
        }
Exemplo n.º 19
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.END_ARG_ARRAY);

            Utils.CheckNotEnd(script, m_name);

            List <Variable> arrayIndices = Utils.GetArrayIndices(ref varName);

            // 2. Get the current value of the variable.
            ParserFunction func = ParserFunction.GetFunction(varName);

            Utils.CheckNotNull(varName, func);
            Variable currentValue = func.GetValue(script);
            Variable element      = currentValue;

            // 2b. Special case for an array.
            if (arrayIndices.Count > 0)// array element
            {
                element = Utils.ExtractArrayElement(currentValue, arrayIndices);
                script.MoveForwardIf(Constants.END_ARRAY);
            }

            // 3. Take either the length of the underlying tuple or
            // string part if it is defined,
            // or the numerical part converted to a string otherwise.
            int size = element.Type == Variable.VarType.ARRAY ?
                       element.Tuple.Count :
                       element.AsString().Length;

            script.MoveForwardIf(Constants.END_ARG, Constants.SPACE);

            Variable newValue = new Variable(size);

            return(newValue);
        }
Exemplo n.º 20
0
        public int GetArrayIndex(Variable indexVar)
        {
            if (this.Type != VarType.ARRAY)
            {
                return(-1);
            }

            if (indexVar.Type == VarType.NUMBER)
            {
                Utils.CheckNonNegativeInt(indexVar);
                return((int)indexVar.Value);
            }

            string hash = indexVar.AsString();
            int    ptr  = m_tuple.Count;

            if (m_dictionary.TryGetValue(hash, out ptr) &&
                ptr < m_tuple.Count)
            {
                return(ptr);
            }

            return(-1);
        }
Exemplo n.º 21
0
        string ProcessRepl(string repl)
        {
            ReplMode = true;

            Dictionary <int, int> char2Line;
            string        script     = Utils.ConvertToScript(repl, out char2Line);
            ParsingScript tempScript = new ParsingScript(script, 0, char2Line);

            tempScript.OriginalScript = repl;
            tempScript.Debugger       = this;

            Variable result = null;

            try
            {
                while (tempScript.Pointer < script.Length)
                {
                    result = DebuggerUtils.Execute(tempScript);
                    tempScript.GoToNextStatement();
                }
            }
            catch (Exception exc)
            {
                return("Exception thrown: " + exc.Message);
            }
            finally
            {
                ReplMode = false;
            }

            string stringRes = Output + "\n";

            stringRes += result == null ? "" : result.AsString();

            return(stringRes);
        }
Exemplo n.º 22
0
        public virtual string AsString(bool isList   = true,
                                       bool sameLine = true,
                                       int maxCount  = -1)
        {
            if (Type == VarType.NUMBER)
            {
                return(Value.ToString());
            }
            if (Type == VarType.STRING)
            {
                return(m_string == null ? "" : m_string);
            }
            if (Type == VarType.OBJECT)
            {
                return(ObjectToString());
            }

            StringBuilder sb = new StringBuilder();

            if (Type == VarType.ENUM)
            {
                sb.Append(Constants.START_GROUP.ToString() + " ");
                foreach (string key in m_propertyMap.Keys)
                {
                    sb.Append(key + " ");
                }
                sb.Append(Constants.END_GROUP.ToString());
                return(sb.ToString());
            }

            if (Type == VarType.NONE || m_tuple == null)
            {
                return(string.Empty);
            }

            if (isList)
            {
                sb.Append(Constants.START_GROUP.ToString() +
                          (sameLine ? "" : Environment.NewLine));
            }

            int count = maxCount < 0 ? m_tuple.Count : Math.Min(maxCount, m_tuple.Count);
            int i     = 0;

            if (m_dictionary.Count > 0)
            {
                count = maxCount < 0 ? m_dictionary.Count : Math.Min(maxCount, m_dictionary.Count);
                foreach (KeyValuePair <string, int> entry in m_dictionary)
                {
                    if (entry.Value >= 0 || entry.Value < m_tuple.Count)
                    {
                        string value   = m_tuple[entry.Value].AsString(isList, sameLine, maxCount);
                        string realKey = entry.Key;
                        m_keyMappings.TryGetValue(entry.Key.ToLower(), out realKey);

                        sb.Append("\"" + realKey + "\" : " + value);
                        if (i++ < count - 1)
                        {
                            sb.Append(sameLine ? ", " : Environment.NewLine);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                for (; i < count; i++)
                {
                    Variable arg = m_tuple[i];
                    sb.Append(arg.AsString(isList, sameLine, maxCount));
                    if (i != count - 1)
                    {
                        sb.Append(sameLine ? ", " : Environment.NewLine);
                    }
                }
            }
            if (count < m_tuple.Count)
            {
                sb.Append(" ...");
            }
            if (isList)
            {
                sb.Append(Constants.END_GROUP.ToString() +
                          (sameLine ? "" : Environment.NewLine));
            }

            return(sb.ToString());
        }
Exemplo n.º 23
0
        static void MergeStrings(Variable leftCell, Variable rightCell, ParsingScript script)
        {
            switch (leftCell.Action)
            {
            case "+":
                leftCell.String = leftCell.AsString() + rightCell.AsString();
                break;

            case "<":
                string arg1 = leftCell.AsString();
                string arg2 = rightCell.AsString();
                leftCell.Value = Convert.ToDouble(string.Compare(arg1, arg2) < 0);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) > 0);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) <= 0);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) >= 0);
                break;

            case "===":
                leftCell.Value = Convert.ToDouble(
                    (leftCell.Type == rightCell.Type &&
                     leftCell.AsString() == rightCell.AsString()) ||
                    (leftCell.Type == Variable.VarType.UNDEFINED &&
                     rightCell.AsString() == Constants.UNDEFINED) ||
                    (rightCell.Type == Variable.VarType.UNDEFINED &&
                     leftCell.AsString() == Constants.UNDEFINED));
                break;

            case "!==":
                leftCell.Value = Convert.ToDouble(
                    leftCell.Type != rightCell.Type ||
                    leftCell.AsString() != rightCell.AsString());
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) == 0);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(
                    string.Compare(leftCell.AsString(), rightCell.AsString()) != 0);
                break;

            case ":":
                leftCell.SetHashVariable(leftCell.AsString(), rightCell);
                break;

            case ")":
                break;

            default:
                Utils.ThrowErrorMsg("Can't process operation [" + leftCell.Action + "] on strings.",
                                    script, leftCell.Action);
                break;
            }
        }
Exemplo n.º 24
0
 public Variable Translate(Variable aVariable)
 {
     return(new Variable(m_name + "_" + m_color + "_" + aVariable.AsString()));
 }
Exemplo n.º 25
0
        private static void MergeNumbers(Variable leftCell, Variable rightCell)
        {
            if (rightCell.Type != Variable.VarType.NUMBER)
            {
                rightCell.Value = rightCell.AsDouble();
            }
            switch (leftCell.Action)
            {
            case "%":
                leftCell.Value %= rightCell.Value;
                break;

            case "*":
                leftCell.Value *= rightCell.Value;
                break;

            case "/":
                if (rightCell.Value == 0.0)
                {
                    throw new ArgumentException("Division by zero");
                }
                leftCell.Value /= rightCell.Value;
                break;

            case "+":
                if (rightCell.Type != Variable.VarType.NUMBER)
                {
                    leftCell.String = leftCell.AsString() + rightCell.String;
                }
                else
                {
                    leftCell.Value += rightCell.Value;
                }
                break;

            case "-":
                leftCell.Value -= rightCell.Value;
                break;

            case "<":
                leftCell.Value = Convert.ToDouble(leftCell.Value < rightCell.Value);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(leftCell.Value > rightCell.Value);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(leftCell.Value <= rightCell.Value);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(leftCell.Value >= rightCell.Value);
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(leftCell.Value == rightCell.Value);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(leftCell.Value != rightCell.Value);
                break;

            case "&":
                leftCell.Value = (int)leftCell.Value & (int)rightCell.Value;
                break;

            case "^":
                leftCell.Value = (int)leftCell.Value ^ (int)rightCell.Value;
                break;

            case "|":
                leftCell.Value = (int)leftCell.Value | (int)rightCell.Value;
                break;

            case "&&":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) && Convert.ToBoolean(rightCell.Value));
                break;

            case "||":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) || Convert.ToBoolean(rightCell.Value));
                break;
            }
        }
Exemplo n.º 26
0
        private static void MergeNumbers(Variable leftCell, Variable rightCell, ParsingScript script)
        {
            if (rightCell.Type != Variable.VarType.NUMBER)
            {
                rightCell.Value = rightCell.AsDouble();
            }
            switch (leftCell.Action)
            {
            case "%":
                leftCell.Value %= rightCell.Value;
                break;

            case "*":
                leftCell.Value *= rightCell.Value;
                break;

            case "/":
                if (rightCell.Value == 0.0)
                {
                    throw new ArgumentException("Division by zero");
                }
                leftCell.Value /= rightCell.Value;
                break;

            case "+":
                if (rightCell.Type != Variable.VarType.NUMBER)
                {
                    leftCell.String = leftCell.AsString() + rightCell.String;
                }
                else
                {
                    leftCell.Value += rightCell.Value;
                }
                break;

            case "-":
                leftCell.Value -= rightCell.Value;
                break;

            case "<":
                leftCell.Value = Convert.ToDouble(leftCell.Value < rightCell.Value);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(leftCell.Value > rightCell.Value);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(leftCell.Value <= rightCell.Value);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(leftCell.Value >= rightCell.Value);
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(leftCell.Value == rightCell.Value);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(leftCell.Value != rightCell.Value);
                break;

            case "&":
                leftCell.Value = (int)leftCell.Value & (int)rightCell.Value;
                break;

            case "^":
                leftCell.Value = (int)leftCell.Value ^ (int)rightCell.Value;
                break;

            case "|":
                leftCell.Value = (int)leftCell.Value | (int)rightCell.Value;
                break;

            case "&&":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) && Convert.ToBoolean(rightCell.Value));
                break;

            case "||":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) || Convert.ToBoolean(rightCell.Value));
                break;

            case "**":
                leftCell.Value = Math.Pow(leftCell.Value, rightCell.Value);
                break;

            case ")":
                Utils.ThrowErrorMsg("Can't process last token [" + rightCell.Value + "] in the expression.",
                                    script, script.Current.ToString());
                break;

            default:
                Utils.ThrowErrorMsg("Can't process operation [" + leftCell.Action + "] in the expression.",
                                    script, leftCell.Action);
                break;
            }
        }
Exemplo n.º 27
0
        public static string GetString(string paramName, ParsingScript script = null)
        {
            Variable result = GetVar(paramName, script);

            return(result.AsString());
        }
Exemplo n.º 28
0
        Variable GetCoreProperty(string propName, ParsingScript script = null)
        {
            Variable result = Variable.EmptyInstance;

            if (m_propertyMap.TryGetValue(propName, out result) ||
                m_propertyMap.TryGetValue(GetRealName(propName), out result))
            {
                return(result);
            }
            else if (propName.Equals(Constants.OBJECT_PROPERTIES, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(GetProperties()));
            }
            else if (propName.Equals(Constants.OBJECT_TYPE, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable((int)Type));
            }
            else if (propName.Equals(Constants.SIZE, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(GetSize()));
            }
            else if (propName.Equals(Constants.UPPER, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(AsString().ToUpper()));
            }
            else if (propName.Equals(Constants.LOWER, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(AsString().ToLower()));
            }
            else if (propName.Equals(Constants.STRING, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(AsString()));
            }
            else if (propName.Equals(Constants.FIRST, StringComparison.OrdinalIgnoreCase))
            {
                if (Tuple != null && Tuple.Count > 0)
                {
                    return(Tuple[0]);
                }
                return(AsString().Length > 0 ? new Variable("" + AsString()[0]) : Variable.EmptyInstance);
            }
            else if (propName.Equals(Constants.LAST, StringComparison.OrdinalIgnoreCase))
            {
                if (Tuple != null && Tuple.Count > 0)
                {
                    return(Tuple.Last <Variable>());
                }
                return(AsString().Length > 0 ? new Variable("" + AsString().Last <char>()) : Variable.EmptyInstance);
            }
            else if (script != null && propName.Equals(Constants.INDEX_OF, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);

                string           search    = Utils.GetSafeString(args, 0);
                int              startFrom = Utils.GetSafeInt(args, 1, 0);
                string           param     = Utils.GetSafeString(args, 2, "no_case");
                StringComparison comp      = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                             StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().IndexOf(search, startFrom, comp)));
            }
            else if (script != null && propName.Equals(Constants.SUBSTRING, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);

                int startFrom = Utils.GetSafeInt(args, 0, 0);
                int length    = Utils.GetSafeInt(args, 1, AsString().Length);
                length = Math.Min(length, AsString().Length - startFrom);

                return(new Variable(AsString().Substring(startFrom, length)));
            }
            else if (script != null && propName.Equals(Constants.REVERSE, StringComparison.OrdinalIgnoreCase))
            {
                script.GetFunctionArgs();
                if (Tuple != null)
                {
                    Tuple.Reverse();
                }
                else if (Type == VarType.STRING)
                {
                    char[] charArray = AsString().ToCharArray();
                    Array.Reverse(charArray);
                    String = new string(charArray);
                }

                return(this);
            }
            else if (script != null && propName.Equals(Constants.SORT, StringComparison.OrdinalIgnoreCase))
            {
                script.GetFunctionArgs();
                Sort();

                return(this);
            }
            else if (script != null && propName.Equals(Constants.SPLIT, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args   = script.GetFunctionArgs();
                string          sep    = Utils.GetSafeString(args, 0, " ");
                var             option = Utils.GetSafeString(args, 1);

                return(TokenizeFunction.Tokenize(AsString(), sep, option));
            }
            else if (script != null && propName.Equals(Constants.JOIN, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                string          sep  = Utils.GetSafeString(args, 0, " ");
                if (Tuple == null)
                {
                    return(new Variable(AsString()));
                }

                var join = string.Join(sep, Tuple);
                return(new Variable(join));
            }
            else if (script != null && propName.Equals(Constants.ADD, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                Variable var = Utils.GetSafeVariable(args, 0);
                if (Tuple != null)
                {
                    Tuple.Add(var);
                }
                else if (Type == VarType.NUMBER)
                {
                    Value += var.AsDouble();
                }
                else
                {
                    String += var.AsString();
                }
                return(var);
            }
            else if (script != null && propName.Equals(Constants.AT, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                int at = Utils.GetSafeInt(args, 0);

                if (Tuple != null && Tuple.Count > 0)
                {
                    return(Tuple.Count > at ? Tuple[at] : Variable.EmptyInstance);
                }
                string str = AsString();
                return(str.Length > at ? new Variable("" + str[at]) : Variable.EmptyInstance);
            }
            else if (script != null && propName.Equals(Constants.REPLACE, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 2, propName);
                string oldVal = Utils.GetSafeString(args, 0);
                string newVal = Utils.GetSafeString(args, 1);

                return(new Variable(AsString().Replace(oldVal, newVal)));
            }
            else if (propName.Equals(Constants.EMPTY_WHITE, StringComparison.OrdinalIgnoreCase))
            {
                bool isEmpty = string.IsNullOrWhiteSpace(AsString());
                return(new Variable(isEmpty));
            }
            else if (script != null && propName.Equals(Constants.REPLACE_TRIM, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 2, propName);
                string currentValue = AsString();

                for (int i = 0; i < args.Count; i += 2)
                {
                    string oldVal = Utils.GetSafeString(args, i);
                    string newVal = Utils.GetSafeString(args, i + 1);
                    currentValue = currentValue.Replace(oldVal, newVal);
                }

                return(new Variable(currentValue.Trim()));
            }
            else if (script != null && propName.Equals(Constants.CONTAINS, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                bool contains = false;
                if (Type == Variable.VarType.ARRAY)
                {
                    string lower = val.ToLower();
                    contains = m_dictionary != null && m_dictionary.ContainsKey(lower);
                    if (!contains && Tuple != null)
                    {
                        foreach (var item in Tuple)
                        {
                            if (item.AsString().Equals(val, comp))
                            {
                                contains = true;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    contains = val != "" && AsString().IndexOf(val, comp) >= 0;
                }
                return(new Variable(contains));
            }
            else if (script != null && propName.Equals(Constants.EQUALS, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().Equals(val, comp)));
            }
            else if (script != null && propName.Equals(Constants.STARTS_WITH, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().StartsWith(val, comp)));
            }
            else if (script != null && propName.Equals(Constants.ENDS_WITH, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().EndsWith(val, comp)));
            }
            else if (script != null && propName.Equals(Constants.TRIM, StringComparison.OrdinalIgnoreCase))
            {
                script.GetFunctionArgs();
                return(new Variable(AsString().Trim()));
            }
            else if (propName.Equals(Constants.KEYS, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> results = GetAllKeys();
                return(new Variable(results));
            }

            return(result);
        }
Exemplo n.º 29
0
        public static async Task <string> GetString(string paramName, ParsingScript script = null)
        {
            Variable result = await GetVar(paramName, script);

            return(result.AsString());
        }