Exemplo n.º 1
0
 /// <inheritdoc />
 public void Set(MoPath key, IMoValue value)
 {
     if (int.TryParse(key.Value, out int index))
     {
         this[index] = value;
     }
 }
Exemplo n.º 2
0
        /// <inheritdoc />
        public virtual IMoValue Get(MoPath key, MoParams parameters)
        {
            if (key.HasChildren)
            {
                string main = key.Value;

                if (!string.IsNullOrWhiteSpace(main))
                {
                    IMoValue value = null;

                    if (!Map.TryGetValue(main, out value))
                    {
                        return(DoubleValue.Zero);
                    }

                    if (value is IMoStruct moStruct)
                    {
                        return(moStruct.Get(key.Next, parameters));
                    }

                    return(value);
                }
            }

            if (Map.TryGetValue(key.Value, out var v))
            {
                return(v);
            }

            return(DoubleValue.Zero);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment)
        {
            IMoValue array = Array.Evaluate(scope, environment);

            if (array is VariableStruct vs)
            {
                MoScope subScope = new MoScope(scope.Runtime);

                foreach (IMoValue value in vs.Map.Values)
                {
                    subScope.IsContinue = false;
                    subScope.IsBreak    = false;

                    Variable.Assign(
                        subScope, environment, value is VariableStruct vss ? vss.Map.FirstOrDefault().Value : value);

                    Body.Evaluate(subScope, environment);

                    if (subScope.ReturnValue != null)
                    {
                        return(subScope.ReturnValue);
                    }
                    else if (subScope.IsBreak)
                    {
                        break;
                    }
                }
            }

            return(DoubleValue.Zero);
        }
Exemplo n.º 4
0
        public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment)
        {
            IMoValue result = DoubleValue.Zero;

            // MoScope scope = new MoScope(this);

            foreach (IExpression expression in Parameters)
            {
                if (expression == null)
                {
                    continue;
                }

                try
                {
                    result = expression.Evaluate(scope, environment);

                    if (scope.ReturnValue != null)
                    {
                        result = scope.ReturnValue;

                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw new MoLangRuntimeException(
                              expression, "An error occured while evaluating the expression", ex);
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public virtual void Set(MoPath key, IMoValue value)
        {
            if (!key.HasChildren)
            {
                Map[key.Value] = value;

                return;
            }

            string main = key.Value;

            if (!string.IsNullOrWhiteSpace(main))
            {
                if (!Map.TryGetValue(main, out var container))
                {
                    if (!key.HasChildren)
                    {
                        throw new MoLangRuntimeException($"Variable was not a struct: {key}", null);
                    }

                    Map.TryAdd(main, container = CreateNew());
                }

                if (container is IMoStruct moStruct)
                {
                    moStruct.Set(key.Next, value);
                }
                else
                {
                    throw new MoLangRuntimeException($"Variable was not a struct: {key}", null);
                }
            }
        }
Exemplo n.º 6
0
        /// <inheritdoc />
        public override void Set(object instance, IMoValue value)
        {
            var propType = _propertyInfo.PropertyType;

            if (propType == typeof(double))
            {
                _propertyInfo.SetValue(instance, value.AsDouble());
                return;
            }
            else if (propType == typeof(float))
            {
                _propertyInfo.SetValue(instance, value.AsFloat());
                return;
            }
            else if (propType == typeof(bool))
            {
                _propertyInfo.SetValue(instance, value.AsBool());
                return;
            }
            else if (propType == typeof(string))
            {
                _propertyInfo.SetValue(instance, value.AsString());
                return;
            }

            _propertyInfo.SetValue(instance, value);
            InvokeChanged();
        }
Exemplo n.º 7
0
 /// <inheritdoc />
 public bool Equals(IMoValue b)
 {
     if (b is StringValue stringValue)
     {
         return(string.Equals(Value, stringValue.Value));                // stringValue.Value.
     }
     return(false);
 }
Exemplo n.º 8
0
        /// <inheritdoc />
        public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment)
        {
            IMoValue eval = Parameters[0].Evaluate(scope, environment);

            scope.ReturnValue = eval;

            return(eval);
        }
Exemplo n.º 9
0
        /// <inheritdoc />
        public bool Equals(IMoValue b)
        {
            if (_value == b.AsDouble())
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 10
0
        /// <inheritdoc />
        public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment)
        {
            IMoValue evalLeft = Left.Evaluate(scope, environment);

            //IMoValue value = environment.GetValue(new MoPath(evalLeft.AsString()));

            if (evalLeft == null || !evalLeft.AsBool())
            {
                return(Right.Evaluate(scope, environment));
            }
            else
            {
                return(evalLeft);
            }
        }
Exemplo n.º 11
0
        /// <inheritdoc />
        public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment)
        {
            //List<IExpression> p = Args.ToList();
            MoPath name = Name;            /* Name is NameExpression expression ? expression.Name :
                                            * new MoPath(Name.Evaluate(scope, environment).ToString());*/

            IMoValue[] arguments = new IMoValue[Parameters.Length];

            for (int i = 0; i < arguments.Length; i++)
            {
                arguments[i] = Parameters[i].Evaluate(scope, environment);
            }

            return(environment.GetValue(name, new MoParams(arguments)));
        }
Exemplo n.º 12
0
        public void SetValue(MoPath name, IMoValue value)
        {
            if (!Structs.TryGetValue(name.Value, out var v))
            {
                throw new MoLangRuntimeException($"Invalid path: {name.Path}", null);
            }

            try
            {
                v.Set(name.Next, value);
            }
            catch (Exception ex)
            {
                throw new MoLangRuntimeException($"Cannot set value on struct: {name}", ex);
            }
        }
Exemplo n.º 13
0
		///  <summary>
		/// 		Evaluates the expressions provided and returns the resulting value (if any) or <see cref="DoubleValue.Zero"/>
		///  </summary>
		///  <param name="expression">The expression to evaluate</param>
		///  <param name="context">The context to use</param>
		///  <returns>
		/// 		The value returned by the expression (if any) or <see cref="DoubleValue.Zero"/>
		///  </returns>
		public IMoValue Execute(IExpression expression, IDictionary<string, IMoValue> context)
		{
			if (expression == null)
				return DoubleValue.Zero;

			if (Environment.Structs.TryGetValue("context", out IMoStruct cont) && cont is ContextStruct contextStruct
			                                                                   && context != null)
			{
				contextStruct.Container = context;
			}

			IMoValue result = null;
			MoScope scope = new MoScope(this);

			result = expression.Evaluate(scope, Environment);

			Environment.Structs["temp"].Clear();

			return result ?? DoubleValue.Zero;
		}
Exemplo n.º 14
0
        /// <summary>
        ///		Get the typed value of a parameter at specified <paramref name="index"/>
        /// </summary>
        /// <param name="index">The index of the </param>
        /// <typeparam name="T">The expected type</typeparam>
        /// <returns>
        ///		The parameter passed at <paramref name="index"/>
        /// </returns>
        /// <exception cref="MoLangRuntimeException"></exception>
        public T Get <T>(int index)
        {
            IMoValue obj = _parameters[index];

            if (obj == null)
            {
                throw new MoLangRuntimeException(
                          $"MoParams: Expected parameter type of {typeof(T).Name} got null", null);
            }

            if (obj?.GetType() == typeof(T))
            {
                return((T)obj);
            }
            else
            {
                throw new MoLangRuntimeException(
                          "MoParams: Expected parameter type of " + typeof(T).Name + ", " + obj.GetType().Name + " given.",
                          null);
            }
        }
Exemplo n.º 15
0
        /// <inheritdoc />
        public void Set(MoPath key, IMoValue value)
        {
            if (!key.HasChildren)
            {
                if (_propertyCache.Properties.TryGetValue(key.Value, out var accessor))
                {
                    if (!accessor.CanWrite)
                    {
                        throw new MoLangRuntimeException("Cannot write to ReadOnly property!", null);
                    }

                    accessor.Set(_instance, value);

                    return;
                }

                throw new MoLangRuntimeException($"Variable was not a struct: {key}", null);
            }

            string main = key.Value;

            if (!string.IsNullOrWhiteSpace(main))
            {
                if (!_propertyCache.Properties.TryGetValue(main, out var accessor))
                {
                    throw new MoLangRuntimeException($"Could not access property: {key}", null);
                }

                var container = accessor.Get(_instance);

                if (container is IMoStruct moStruct)
                {
                    moStruct.Set(key.Next, value);
                }
                else
                {
                    throw new MoLangRuntimeException($"Variable was not a struct: {key}", null);
                }
            }
        }
Exemplo n.º 16
0
        private bool ExecuteGet(MoPath property, string main, MoParams parameters, out IMoValue returnValue)
        {
            if (_propertyCache.Properties.TryGetValue(main, out var accessor))
            {
                if (!accessor.CanRead)
                {
                    throw new MoLangRuntimeException($"Cannot read from property '{property.ToString()}'", null);
                }

                returnValue = accessor.Get(_instance);
                return(true);
            }

            if (_propertyCache.Functions.TryGetValue(main, out var f))
            {
                returnValue = f.Invoke(_instance, parameters);
                return(true);
            }

            returnValue = DoubleValue.Zero;
            return(false);
        }
Exemplo n.º 17
0
        /// <inheritdoc />
        public override void Assign(MoScope scope, MoLangEnvironment environment, IMoValue value)
        {
            var index = (int)Index.Evaluate(scope, environment).AsDouble();

            MoPath path;

            if (Array is NameExpression nameExpression)
            {
                var p = nameExpression.Name;
                path = p;
            }
            else
            {
                var eval = Array.Evaluate(scope, environment);
                path = new MoPath($"{eval.AsString()}");
            }

            var array = environment.GetValue(path);

            if (array is ArrayStruct asArray)
            {
                asArray[index] = value;
            }
        }
Exemplo n.º 18
0
 /// <inheritdoc />
 public override void Set(MoPath key, IMoValue value)
 {
     throw new NotSupportedException("Read-only context");
 }
Exemplo n.º 19
0
 /// <inheritdoc />
 public override void Assign(MoScope scope, MoLangEnvironment environment, IMoValue value)
 {
     environment.SetValue(Name, value);
 }
Exemplo n.º 20
0
 public BooleanExpression(bool value) : base()
 {
     _value = value ? DoubleValue.One : DoubleValue.Zero;
 }
Exemplo n.º 21
0
 /// <inheritdoc />
 public virtual void Assign(MoScope scope, MoLangEnvironment environment, IMoValue value)
 {
     throw new Exception("Cannot assign a value to " + this.GetType());
 }
Exemplo n.º 22
0
 public NumberExpression(IMoValue value) : base()
 {
     _value = value;
 }
Exemplo n.º 23
0
 /// <inheritdoc />
 public NumberExpression(double value) : base()
 {
     _value = new DoubleValue(value);
 }
Exemplo n.º 24
0
 /// <inheritdoc />
 public void Set(MoPath key, IMoValue value)
 {
     throw new NotSupportedException("Cannot set a value in a query struct.");
 }
Exemplo n.º 25
0
 /// <inheritdoc />
 public override void Set(object instance, IMoValue value)
 {
     _fieldInfo.SetValue(instance, value);
     InvokeChanged();
 }
Exemplo n.º 26
0
 /// <inheritdoc />
 public bool Equals(IMoValue b)
 {
     return(Equals((object)b));
 }
Exemplo n.º 27
0
 public abstract void Set(object instance, IMoValue value);
Exemplo n.º 28
0
        private static void ProcessMethods(IReflect type,
                                           IDictionary <string, Func <object, MoParams, IMoValue> > functions)
        {
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (var method in methods)
            {
                var functionAttribute = method.GetCustomAttribute <MoFunctionAttribute>();

                if (functionAttribute == null)
                {
                    continue;
                }

                foreach (var name in functionAttribute.Name)
                {
                    if (functions.ContainsKey(name))
                    {
                        Debug.WriteLine($"Duplicate function \'{name}\' in {type.ToString()}");

                        continue;
                    }

                    IMoValue ExecuteMolangFunction(object instance, MoParams mo)
                    {
                        var      methodParams = method.GetParameters();
                        IMoValue value        = DoubleValue.Zero;

                        object[] parameters = new object[methodParams.Length];
                        //List<object> parameters = new List<object>();

                        if (methodParams.Length == 1 && methodParams[0].ParameterType == typeof(MoParams))
                        {
                            parameters[0] = mo;
                            //parameters.Add(mo);
                        }
                        else
                        {
                            for (var index = 0; index < methodParams.Length; index++)
                            {
                                var parameter = methodParams[index];

                                if (!mo.Contains(index))
                                {
                                    if (!parameter.IsOptional)
                                    {
                                        throw new MissingMethodException($"Missing parameter: {parameter.Name}");
                                    }

                                    break;
                                }

                                var t = parameter.ParameterType;

                                if (t == typeof(MoParams))
                                {
                                    parameters[index] = mo;                                     //.Add(mo);
                                }
                                else if (t == typeof(int))
                                {
                                    parameters[index] = mo.GetInt(index);
                                }
                                else if (t == typeof(double))
                                {
                                    parameters[index] = mo.GetDouble(index);
                                }
                                else if (t == typeof(float))
                                {
                                    parameters[index] = (float)mo.GetDouble(index);
                                }
                                else if (t == typeof(string))
                                {
                                    parameters[index] = mo.GetString(index);
                                }
                                else if (typeof(IMoStruct).IsAssignableFrom(t))
                                {
                                    parameters[index] = mo.GetStruct(index);
                                }
                                else if (typeof(MoLangEnvironment).IsAssignableFrom(t))
                                {
                                    parameters[index] = mo.GetEnv(index);
                                }
                                else
                                {
                                    throw new Exception("Unknown parameter type.");
                                }

                                //TODO: Continue.
                            }
                        }

                        var result = method.Invoke(instance, parameters);

                        if (result != null)
                        {
                            if (result is IMoValue moValue)
                            {
                                return(moValue);
                            }

                            return(MoValue.FromObject(result));
                        }

                        return(value);
                    }

                    functions.Add(name, ExecuteMolangFunction);
                }
            }
        }