Example #1
0
        /// <summary>
        /// Runs command execution
        /// </summary>
        public void Run()
        {
            if (VariableName == null && VariableValue == null)
            {
                throw new CommandExecutionException($"You trying to push nothing");
            }
            if (VariableValue == null && VariableName != null)
            {
                if (double.TryParse(VariableName, out double value))
                {
                    Context.PushStack("var", value);
                    return;
                }
                else
                {
                    var variableFromDefines = Context.PairsList.SingleOrDefault(p => p.Id == VariableName);
                    if (variableFromDefines == null)
                    {
                        throw new CommandExecutionException($"You trying to push {VariableName} but this variable is not defined");
                    }


                    Context.PushStack(variableFromDefines.Id, variableFromDefines.Value);
                    return;
                }
            }
            if (VariableName != null && VariableValue != null)
            {
                object value;

                if (VariableValue.Contains('.'))
                {
                    try
                    {
                        value = Convert.ToDouble(VariableValue);
                    }
                    catch
                    {
                        throw new CommandExecutionException("Wrong second parameter", Context.Pair);
                    }
                }
                else
                {
                    try
                    {
                        value = Convert.ToInt64(VariableValue);
                    }
                    catch
                    {
                        throw new CommandExecutionException("Wrong second parameter", Context.Pair);
                    }
                }

                Context.PushStack(new MutableKeyValuePair <string, object>(VariableName, value));
                return;
            }

            throw new CommandExecutionException($"Nothing was pushed due to incorrect arguments", Context.Pair, Context.Stack);
        }