public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(valueType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of applying negation to {arg} of type {valueType.Name} as type {returnType.Name}");
            }

            object value;

            if (valueType == typeof(int))
            {
                value = -arg.GetAs <int>(context);
            }
            else
            {
                value = -arg.GetAs <double>(context);
            }

            if (returnType.IsAssignableFrom(valueType))
            {
                return((T)value);
            }

            return((T)Convert.ChangeType(value, returnType));
        }
Esempio n. 2
0
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(getType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of User.{userMethod} of type {getType.Name} as type {returnType.Name}");
            }

            switch (userMethod)
            {
            case UserMethod.GetInt:
                return((T)Convert.ChangeType(PlayerData.GetInt(keyArg.GetAs <string>(context), defaultArg?.GetAs <int>(context) ?? 0), typeof(T)));

            case UserMethod.GetDouble:
                return((T)Convert.ChangeType(PlayerData.GetDouble(keyArg.GetAs <string>(context), defaultArg?.GetAs <double>(context) ?? 0.0), typeof(T)));

            case UserMethod.GetBool:
                return((T)Convert.ChangeType(PlayerData.GetBool(keyArg.GetAs <string>(context), defaultArg?.GetAs <bool>(context) ?? false), typeof(T)));

            case UserMethod.GetString:
                return((T)Convert.ChangeType(PlayerData.GetString(keyArg.GetAs <string>(context), defaultArg?.GetAs <string>(context) ?? ""), typeof(T)));

            default:
                throw new Exception($"Unexpected UserMethod: {userMethod}");
            }
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(getType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of Indexing with type {getType.Name} as type {returnType.Name}");
            }

            object index = indexArg.GetAs <object>(context);

            if (!indexerType.IsAssignableFrom(indexArg.GetValueType()))
            {
                index = Convert.ChangeType(index, indexerType);
            }

            object value = indexGetter.Invoke(valueArg.GetAs <object>(context), new object[] { index });

            if (returnType.IsAssignableFrom(getType))
            {
                return((T)value);
            }

            return((T)Convert.ChangeType(value, returnType));
        }
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            switch (operatorType)
            {
            case Operator.AndEquals:
                if (!assignee.GetAs <bool>(context))
                {
                    //Short Circuiting
                    //No need to set anything
                    break;
                }
                assignee.SetAs(context, value.GetAs <bool>(context));
                break;

            case Operator.OrEquals:
                if (assignee.GetAs <bool>(context))
                {
                    //Short Circuiting
                    //No need to set anything
                    break;
                }
                assignee.SetAs(context, value.GetAs <bool>(context));
                break;

            default:
                throw new ArgumentException($"Unexpected Operator: {operatorType}");
            }

            return(FlowState.Nominal);
        }
Esempio n. 5
0
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            if (valueType == typeof(string))
            {
                PlayerData.SetString(keyArg.GetAs <string>(context), valueArg.GetAs <string>(context));
            }
            else if (valueType == typeof(bool))
            {
                PlayerData.SetBool(keyArg.GetAs <string>(context), valueArg.GetAs <bool>(context));
            }
            else if (valueType == typeof(double))
            {
                PlayerData.SetDouble(keyArg.GetAs <string>(context), valueArg.GetAs <double>(context));
            }
            else if (valueType == typeof(int))
            {
                PlayerData.SetInt(keyArg.GetAs <string>(context), valueArg.GetAs <int>(context));
            }
            else if (valueType == typeof(IList))
            {
                PlayerData.SetJsonValue(
                    key: keyArg.GetAs <string>(context),
                    value: GetUserListFunction.SerializeList(valueArg.GetAs <IList>(context)));
            }
            else
            {
                throw new Exception($"Unexpected ValueType for SetValue: {valueType.Name}");
            }

            return(FlowState.Nominal);
        }
Esempio n. 6
0
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(valueType))
            {
                throw new ScriptRuntimeException($"Tried to implicitly cast the results of {this} to type {returnType}");
            }

            if (valueType == typeof(int))
            {
                //Integer type
                switch (mathMethod)
                {
                case MathMethod.Min: return((T)Convert.ChangeType(Math.Min(arg1.GetAs <int>(context), arg2.GetAs <int>(context)), returnType));

                case MathMethod.Max: return((T)Convert.ChangeType(Math.Max(arg1.GetAs <int>(context), arg2.GetAs <int>(context)), returnType));

                default: throw new ArgumentException($"Unexpected MathMethod: {mathMethod}");
                }
            }

            //Double type
            switch (mathMethod)
            {
            case MathMethod.Min: return((T)Convert.ChangeType(Math.Min(arg1.GetAs <double>(context), arg2.GetAs <double>(context)), returnType));

            case MathMethod.Max: return((T)Convert.ChangeType(Math.Max(arg1.GetAs <double>(context), arg2.GetAs <double>(context)), returnType));

            default: throw new ArgumentException($"Unexpected MathMethod: {mathMethod}");
            }
        }
        public T GetAs <T>(RuntimeContext context)
        {
            if (valueType == typeof(int))
            {
                return(IntOperator <T>(arg1.GetAs <int>(context), arg2.GetAs <int>(context), operatorType, valueType));
            }

            return(DoubleOperator <T>(arg1.GetAs <double>(context), arg2.GetAs <double>(context), operatorType, valueType));
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(typeof(TResult)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of Indexing with type {typeof(TResult).Name} as type {returnType.Name}");
            }

            TResult result = getOperation(value.GetAs <TInput>(context));

            return((T)Convert.ChangeType(result, typeof(T)));
        }
Esempio n. 9
0
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            FlowState state;

            bool continuing = true;

            while (continuing && continueExpression.GetAs <bool>(context))
            {
                bodyContext = new ScopeRuntimeContext(context);

                state = loopBody.Execute(bodyContext);

                switch (state)
                {
                case FlowState.Nominal:
                case FlowState.LoopContinue:
                    //Do nothing
                    break;

                case FlowState.LoopBreak:
                    continuing = false;
                    break;

                case FlowState.Return:
                    return(state);

                default:
                    throw new Exception($"Unexpected FlowState: {state}");
                }
            }

            return(FlowState.Nominal);
        }
        public T GetAs <T>(RuntimeContext context)
        {
            if (!typeof(T).AssignableFromType(typeof(bool)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of applying {operatorType} as type {typeof(T).Name}");
            }

            switch (operatorType)
            {
            case Operator.And: return((T)(object)(arg1.GetAs <bool>(context) && arg2.GetAs <bool>(context)));

            case Operator.Or: return((T)(object)(arg1.GetAs <bool>(context) || arg2.GetAs <bool>(context)));

            default: throw new ArgumentException($"Unexpected Operator {operatorType}");
            }
        }
        public T GetAs <T>(RuntimeContext context)
        {
            if (!typeof(T).AssignableFromType(typeof(bool)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve value of User.HasData as type {typeof(T).Name}");
            }

            return((T)(object)PlayerData.HasKey(keyArg.GetAs <string>(context)));
        }
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            if (assigneeType == valueType)
            {
                assignee.Set(context, value.GetAs <object>(context));
            }
            else if (assigneeType.AssignableFromType(valueType))
            {
                assignee.Set(context, Convert.ChangeType(value.GetAs <object>(context), assigneeType));
            }
            else
            {
                throw new ScriptRuntimeException(
                          $"Unable to assign {assignee} of type {assigneeType.Name} the value {value} of type {value.GetValueType().Name}");
            }

            return(FlowState.Nominal);
        }
Esempio n. 13
0
        public T GetAs <T>(RuntimeContext context)
        {
            if (!typeof(T).AssignableFromType(typeof(bool)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of IsNaN function at type {typeof(T).Name}");
            }

            return((T)(object)double.IsNaN(arg.GetAs <double>(context)));
        }
Esempio n. 14
0
        public T GetAs <T>(RuntimeContext context)
        {
            //Check Value Type
            if (!typeof(T).AssignableFromType(typeof(bool)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of Not operator at type {typeof(T).Name}");
            }

            return((T)(object)!arg.GetAs <bool>(context));
        }
Esempio n. 15
0
        public T GetAs <T>(RuntimeContext context)
        {
            if (!typeof(T).AssignableFromType(typeof(bool)))
            {
                throw new ScriptRuntimeException(
                          $"Return value of {operatorType} is a boolean, but it was accessed as {typeof(T).Name}");
            }

            if (argType == typeof(double) || argType == typeof(int))
            {
                switch (operatorType)
                {
                case Operator.IsEqualTo: return((T)(object)(arg1.GetAs <double>(context) == arg2.GetAs <double>(context)));

                case Operator.IsNotEqualTo: return((T)(object)(arg1.GetAs <double>(context) != arg2.GetAs <double>(context)));

                default: throw new ArgumentException($"Unexpected Operator: {operatorType}");
                }
            }
            else if (argType == typeof(bool))
            {
                switch (operatorType)
                {
                case Operator.IsEqualTo: return((T)(object)(arg1.GetAs <bool>(context) == arg2.GetAs <bool>(context)));

                case Operator.IsNotEqualTo: return((T)(object)(arg1.GetAs <bool>(context) != arg2.GetAs <bool>(context)));

                default: throw new ArgumentException($"Unexpected Operator: {operatorType}");
                }
            }
            else if (argType == typeof(string))
            {
                switch (operatorType)
                {
                case Operator.IsEqualTo: return((T)(object)(arg1.GetAs <string>(context) == arg2.GetAs <string>(context)));

                case Operator.IsNotEqualTo: return((T)(object)(arg1.GetAs <string>(context) != arg2.GetAs <string>(context)));

                default: throw new ArgumentException($"Unexpected Operator: {operatorType}");
                }
            }
            else
            {
                switch (operatorType)
                {
                case Operator.IsEqualTo: return((T)(object)(arg1.GetAs <object>(context) == arg2.GetAs <object>(context)));

                case Operator.IsNotEqualTo: return((T)(object)(arg1.GetAs <object>(context) != arg2.GetAs <object>(context)));

                default: throw new ArgumentException($"Unexpected Operator: {operatorType}");
                }
            }
        }
Esempio n. 16
0
 public override FlowState Execute(ScopeRuntimeContext context)
 {
     if (condition.GetAs <bool>(context))
     {
         return(trueBlock?.Execute(new ScopeRuntimeContext(context)) ?? FlowState.Nominal);
     }
     else
     {
         return(falseBlock?.Execute(new ScopeRuntimeContext(context)) ?? FlowState.Nominal);
     }
 }
        public T GetAs <T>(RuntimeContext context)
        {
            if (!typeof(T).AssignableFromType(typeof(bool)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of applying {operatorType} as type {typeof(T).Name}");
            }

            switch (operatorType)
            {
            case Operator.IsGreaterThan: return((T)(object)(arg1.GetAs <double>(context) > arg2.GetAs <double>(context)));

            case Operator.IsGreaterThanOrEqualTo: return((T)(object)(arg1.GetAs <double>(context) >= arg2.GetAs <double>(context)));

            case Operator.IsLessThan: return((T)(object)(arg1.GetAs <double>(context) < arg2.GetAs <double>(context)));

            case Operator.IsLessThanOrEqualTo: return((T)(object)(arg1.GetAs <double>(context) <= arg2.GetAs <double>(context)));

            default: throw new ArgumentException($"Unexpected Operator: {operatorType}");
            }
        }
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            loopContext = new ScopeRuntimeContext(context);
            FlowState state = declarationStatement?.Execute(loopContext) ?? FlowState.Nominal;

            switch (state)
            {
            case FlowState.Nominal:
                //Continue
                break;

            case FlowState.LoopContinue:
            case FlowState.LoopBreak:
            case FlowState.Return:
            default:
                throw new Exception($"Unexpected FlowState: {state}");
            }

            bool continuing = true;

            foreach (object item in containerExpression.GetAs <IEnumerable>(loopContext))
            {
                loopVariable.Set(loopContext, item);
                bodyContext = new ScopeRuntimeContext(loopContext);

                state = loopBody.Execute(bodyContext);

                switch (state)
                {
                case FlowState.Nominal:
                case FlowState.LoopContinue:
                    //Do Nothing
                    break;

                case FlowState.LoopBreak:
                    continuing = false;
                    break;

                case FlowState.Return:
                    return(state);

                default:
                    throw new Exception($"Unexpected FlowState: {state}");
                }

                if (!continuing)
                {
                    break;
                }
            }

            return(FlowState.Nominal);
        }
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            if (returnType == typeof(void))
            {
                context.PushReturnValue(null);
            }
            else
            {
                if (returnType == returnValue.GetValueType())
                {
                    context.PushReturnValue(returnValue.GetAs <object>(context));
                }
                else
                {
                    context.PushReturnValue(
                        Convert.ChangeType(returnValue.GetAs <object>(context), returnType));
                }
            }

            return(FlowState.Return);
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(valueType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of applying Clamp to {value} as type {returnType.Name}");
            }

            if (returnType == typeof(int))
            {
                return((T)(object)GeneralMath.Clamp(
                           value: value.GetAs <int>(context),
                           min: lowerbound.GetAs <int>(context),
                           max: upperbound.GetAs <int>(context)));
            }

            return((T)(object)GeneralMath.Clamp(
                       value: value.GetAs <double>(context),
                       min: lowerbound.GetAs <double>(context),
                       max: upperbound.GetAs <double>(context)));
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(valueType))
            {
                throw new ScriptRuntimeException($"Tried to implicitly cast the results of {this} to type {returnType.Name} instead of argument type {valueType.Name}");
            }

            bool cond = condition.GetAs <bool>(context);

            return((T)Convert.ChangeType(cond ? arg1.GetAs <object>(context) : arg2.GetAs <object>(context), typeof(T)));
        }
Esempio n. 22
0
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            if (assigneeType == typeof(int))
            {
                assignee.SetAs(context, assignee.GetAs <int>(context) + value.GetAs <int>(context));
            }
            else if (assigneeType == typeof(double))
            {
                assignee.SetAs(context, assignee.GetAs <double>(context) + value.GetAs <double>(context));
            }
            else if (assigneeType == typeof(string))
            {
                assignee.SetAs(context, assignee.GetAs <string>(context) + GetStringValue(value, context));
            }
            else
            {
                throw new ScriptRuntimeException(
                          $"Incompatible types for operator {Operator.PlusEquals}: {assignee} of type {assigneeType.Name} and {value} of type {value.GetValueType().Name}");
            }

            return(FlowState.Nominal);
        }
Esempio n. 23
0
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            if (context.VariableExists(identifier))
            {
                //You cannot declare a local variable to shadow an existing global
                throw new ScriptRuntimeException($"Variable already declared in this context: {identifier}");
            }

            object defaultValue = initializer.GetAs <object>(context);

            context.DeclareVariable(identifier, valueType, defaultValue);

            return(FlowState.Nominal);
        }
        private static string GetStringValue(IValueGetter arg, RuntimeContext context)
        {
            Type argType = arg.GetValueType();

            if (argType == typeof(string))
            {
                return(arg.GetAs <string>(context));
            }
            else if (argType == typeof(double))
            {
                return(arg.GetAs <double>(context).ToString());
            }
            else if (argType == typeof(int))
            {
                return(arg.GetAs <int>(context).ToString());
            }
            else if (argType == typeof(bool))
            {
                return(arg.GetAs <bool>(context).ToString());
            }

            throw new ScriptRuntimeException($"Unsupported type for Stringification: type {argType.Name}");
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(valueType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of applying {operatorType} as type {returnType.Name}");
            }

            if (returnType == typeof(int))
            {
                return((T)(object)(int)arg.GetAs <double>(context));
            }

            switch (operatorType)
            {
            case Operator.CastDouble: return((T)(object)arg.GetAs <double>(context));

            case Operator.CastInteger: return((T)(object)(int)Math.Floor(arg.GetAs <double>(context)));

            default: throw new ArgumentException($"Unexpected Operator: {operatorType}");
            }
        }
Esempio n. 26
0
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(outputType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of Indexing with type {outputType.Name} as type {returnType.Name}");
            }

            object result = operation(value.GetAs <object>(context));

            if (!returnType.IsAssignableFrom(outputType))
            {
                result = Convert.ChangeType(result, returnType);
            }

            return((T)result);
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (!returnType.AssignableFromType(getType))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of User.GetList as type {returnType.Name}");
            }

            string key = keyArg.GetAs <string>(context);

            if (PlayerData.HasKey(key))
            {
                return((T)DeserializeList(PlayerData.GetJsonValue(key)));
            }
            else
            {
                return((T)(defaultArg?.GetAs <object>(context) ?? Activator.CreateInstance(getType)));
            }
        }
Esempio n. 28
0
        public override FlowState Execute(ScopeRuntimeContext context)
        {
            loopContext = new ScopeRuntimeContext(context);
            FlowState state = initializationStatement?.Execute(loopContext) ?? FlowState.Nominal;

            switch (state)
            {
            case FlowState.Nominal:
                //Continue
                break;

            case FlowState.LoopContinue:
            case FlowState.LoopBreak:
            case FlowState.Return:
            default:
                throw new Exception($"Unexpected FlowState: {state}");
            }

            bool continuing = true;

            while (continuing && continueExpression.GetAs <bool>(loopContext))
            {
                bodyContext = new ScopeRuntimeContext(loopContext);

                state = loopBody.Execute(bodyContext);

                switch (state)
                {
                case FlowState.Nominal:
                case FlowState.LoopContinue:
                    //Do Nothing
                    break;

                case FlowState.LoopBreak:
                    continuing = false;
                    break;

                case FlowState.Return:
                    return(state);

                default:
                    throw new Exception($"Unexpected FlowState: {state}");
                }

                //Don't run incrementStatement if we are breaking out
                if (continuing)
                {
                    state = incrementStatement?.Execute(loopContext) ?? FlowState.Nominal;

                    switch (state)
                    {
                    case FlowState.Nominal:
                        //Do Nothing
                        break;

                    case FlowState.Return:
                    case FlowState.LoopContinue:
                    case FlowState.LoopBreak:
                    default:
                        throw new Exception($"Unexpected FlowState: {state}");
                    }
                }
            }

            return(FlowState.Nominal);
        }
        public T GetAs <T>(RuntimeContext context)
        {
            Type returnType = typeof(T);

            if (returnType == typeof(object))
            {
                returnType = valueType;
            }

            if (!(returnType == typeof(int) || returnType == typeof(double)))
            {
                throw new ScriptRuntimeException($"Tried to retrieve result of applying Math.{mathMethod} to {arg} of type {valueType.Name} as type {returnType.Name}");
            }

            if (returnType == typeof(int) && valueType != typeof(int))
            {
                throw new ScriptRuntimeException($"Tried to implicitly cast the results of {this} to type {returnType.Name}");
            }

            if (returnType == typeof(int))
            {
                switch (mathMethod)
                {
                case MathMethod.Floor: return((T)(object)(int)Math.Floor(arg.GetAs <double>(context)));

                case MathMethod.Ceiling: return((T)(object)(int)Math.Ceiling(arg.GetAs <double>(context)));

                case MathMethod.Round: return((T)(object)(int)Math.Round(arg.GetAs <double>(context)));

                case MathMethod.Abs: return((T)(object)Math.Abs(arg.GetAs <int>(context)));

                case MathMethod.Sign: return((T)(object)Math.Sign(arg.GetAs <double>(context)));

                case MathMethod.Ln:
                case MathMethod.Log10:
                case MathMethod.Exp:
                case MathMethod.Sqrt:
                case MathMethod.Sin:
                case MathMethod.Cos:
                case MathMethod.Tan:
                case MathMethod.Asin:
                case MathMethod.Acos:
                case MathMethod.Atan:
                case MathMethod.Sinh:
                case MathMethod.Cosh:
                case MathMethod.Tanh:
                    throw new ScriptRuntimeException($"Math.{mathMethod} function can't return int");

                default: throw new ArgumentException($"Unexpected MathMethod: {mathMethod}");
                }
            }

            switch (mathMethod)
            {
            case MathMethod.Floor: return((T)(object)Math.Floor(arg.GetAs <double>(context)));

            case MathMethod.Ceiling: return((T)(object)Math.Ceiling(arg.GetAs <double>(context)));

            case MathMethod.Round: return((T)(object)Math.Round(arg.GetAs <double>(context)));

            case MathMethod.Abs: return((T)(object)Math.Abs(arg.GetAs <double>(context)));

            case MathMethod.Sign: return((T)(object)(double)Math.Sign(arg.GetAs <double>(context)));

            case MathMethod.Ln: return((T)(object)Math.Log(arg.GetAs <double>(context)));

            case MathMethod.Log10: return((T)(object)Math.Log10(arg.GetAs <double>(context)));

            case MathMethod.Sqrt: return((T)(object)Math.Sqrt(arg.GetAs <double>(context)));

            case MathMethod.Exp: return((T)(object)Math.Exp(arg.GetAs <double>(context)));

            case MathMethod.Sin: return((T)(object)Math.Sin(arg.GetAs <double>(context)));

            case MathMethod.Cos: return((T)(object)Math.Cos(arg.GetAs <double>(context)));

            case MathMethod.Tan: return((T)(object)Math.Tan(arg.GetAs <double>(context)));

            case MathMethod.Asin: return((T)(object)Math.Asin(arg.GetAs <double>(context)));

            case MathMethod.Acos: return((T)(object)Math.Acos(arg.GetAs <double>(context)));

            case MathMethod.Atan: return((T)(object)Math.Atan(arg.GetAs <double>(context)));

            case MathMethod.Sinh: return((T)(object)Math.Sinh(arg.GetAs <double>(context)));

            case MathMethod.Cosh: return((T)(object)Math.Cosh(arg.GetAs <double>(context)));

            case MathMethod.Tanh: return((T)(object)Math.Tanh(arg.GetAs <double>(context)));

            default: throw new ArgumentException($"Unexpected MathMethod: {mathMethod}");
            }
        }
Esempio n. 30
0
 FlowState IExecutable.Execute(ScopeRuntimeContext context)
 {
     operation.Invoke(value.GetAs <TInput>(context));
     return(FlowState.Nominal);
 }