Esempio n. 1
0
        private static dynamic Boolean(object value)
        {
            var numberTypes = new[] {
                typeof(short),
                typeof(ushort),
                typeof(int),
                typeof(uint),
                typeof(long),
                typeof(ulong),
                typeof(float),
                typeof(double),
                typeof(decimal)
            };

            if (value == null)
            {
                return(false);
            }
            if (value is KumaNumber)
            {
                value = KumaNumber.Convert((KumaNumber)value);
            }
            if (value is bool)
            {
                return((bool)value);
            }
            if (numberTypes.Contains(value.GetType()))
            {
                dynamic _value = value;
                return(_value != 0);
            }
            return(value != null);
        }
Esempio n. 2
0
        internal static dynamic GenDelegate(object @delegate, List <object> @params, object scope)
        {
            var multicastDelegate = @delegate as MulticastDelegate;
            var realParams        = new List <object>();

            @params.ForEach(p => realParams.Add(p is KumaNumber ? KumaNumber.Convert((KumaNumber)p) : p));

            return(multicastDelegate != null?multicastDelegate.DynamicInvoke(realParams.ToArray()) : null);
        }
Esempio n. 3
0
        internal static dynamic Invoke(Type targetType, MethodBase minfo, List <FunctionArgument> args,
                                       KumaScope scope)
        {
            var isClassMethod = minfo.IsStatic;

            object target = scope.Variables.ContainsKey("self")
                ? scope["self"]
                : isClassMethod ? targetType : targetType.GetConstructor(new Type[] {}).Invoke(null);

            var arguments = new List <object>();

            args.ForEach(arg => {
                var _val = CompilerServices.CompileExpression(arg.Value, scope);
                if (_val is KumaString)
                {
                    _val = (string)_val;
                }
                if (_val is KumaNumber)
                {
                    _val = KumaNumber.Convert((KumaNumber)_val);
                }
                arguments.Add(_val);
            });

            while (arguments.Count < minfo.GetParameters().Count())
            {
                arguments.Add(null);
            }

            if (minfo.IsConstructor)
            {
                var ctor = (ConstructorInfo)minfo;
                return(ctor.Invoke(arguments.ToArray()));
            }

            if (target is KumaInstance && !(target is KumaBoxedInstance) &&
                ((KumaInstance)target).BackingObject != null)
            {
                target = ((KumaInstance)target).BackingObject;
            }

            dynamic val = null;

            if (((MethodInfo)minfo).ReturnType != typeof(void))
            {
                val = minfo.Invoke(target, arguments.ToArray());
            }
            else
            {
                minfo.Invoke(target, arguments.ToArray());
            }

            return(val);
        }
        public override object Run(Scope scope)
        {
            var body = (Body as BlockExpression);

            body.Scope.MergeWithScope(Kuma.Globals);
            body.Scope.MergeWithScope(scope);

            var visitor = new VariableNameVisitor();

            visitor.Visit(body);

            body.SetChildrenScopes(body.Scope);

            var block = CompilerServices.CreateLambdaForExpression(body);
            var res   = block();

            if (res is Symbol)
            {
                var symval = new BlockExpression(new List <Expression> {
                    new VariableExpression(res)
                }, body.Scope);
                res = CompilerServices.CreateLambdaForExpression(symval)();
            }
            else if (res is KumaInstance)
            {
                var so = (KumaInstance)res;
                if (so is KumaBoxedInstance)
                {
                    res = ((KumaBoxedInstance)so).BoxedObject;
                }
            }
            else if (res is KumaNumber)
            {
                res = KumaNumber.Convert(res);
            }
            else if (res is KumaString)
            {
                res = (string)res;
            }
            else if (res is KumaArray)
            {
                res = ConvertElements((KumaArray)res);
            }
            else if (res is KumaDictionary)
            {
                res = ConvertElements((KumaDictionary)res);
            }

            body.Scope.MergeIntoScope(scope);

            return(res);
        }
Esempio n. 5
0
 public dynamic this [Symbol sym] {
     get { return(Resolve(sym, this)); }
     set {
         var val = value;
         if (val is string)
         {
             val = new KumaString(val);
         }
         if (KumaNumber.IsConvertable(val))
         {
             val = new KumaNumber(val);
         }
         SymVars[sym] = val;
     }
 }
Esempio n. 6
0
        // Kuma -> .net
        internal static void SyncInstanceVariablesFrom(KumaInstance KumaObject, object obj)
        {
            var _fields =
                obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();

            KumaObject.InstanceVariables.Variables.Keys.ToList().ForEach(key => {
                var _fq = _fields.Where(field => field.Name == key);
                if (_fq.Any())
                {
                    var val = KumaObject.InstanceVariables[key];
                    if (val is KumaNumber)
                    {
                        val = KumaNumber.Convert(val);
                    }
                    _fq.First().SetValue(obj, val);
                }
            });
        }
        private dynamic ConvertElements(KumaDictionary res)
        {
            List <dynamic> keysToRemove = new List <object>();

            keysToRemove.AddRange(res.Keys.OfType <KumaString>());
            keysToRemove.ForEach(o =>
            {
                string s = o;
                var val  = res[o];
                res.Remove(o);
                res[s] = val;
            });

            keysToRemove.Clear();

            keysToRemove.AddRange(
                res.Keys.Where(
                    key =>
                    res[key] is KumaString || res[key] is KumaNumber || res[key] is KumaArray ||
                    res[key] is KumaDictionary));

            keysToRemove.ForEach(o =>
            {
                if (res[o] is KumaString)
                {
                    string s = res[o];
                    res[o]   = s;
                }
                else if (res[o] is KumaNumber)
                {
                    res[o] = KumaNumber.Convert(res[o]);
                }
                else if (res[o] is KumaArray)
                {
                    res[o] = ConvertElements((KumaArray)res[o]);
                }
                else if (res[o] is KumaDictionary)
                {
                    res[o] = ConvertElements((KumaDictionary)res[o]);
                }
            });

            return(res);
        }
Esempio n. 8
0
 public dynamic this [string name] {
     get { return(Resolve(name)); }
     set {
         if (CheckConstant(name))
         {
             throw new ConstantException(
                       string.Format("{0} is already defined as a constant in this scope or a parent scope.", name));
         }
         var val = value;
         if (val is string)
         {
             val = new KumaString(val);
         }
         if (KumaNumber.IsConvertable(val))
         {
             val = new KumaNumber(val);
         }
         Variables[name] = val;
     }
 }
 private dynamic ConvertElements(KumaArray res)
 {
     for (var i = 0; i < res.Count(); i++)
     {
         if (res[i] is KumaString)
         {
             res[i] = (string)res[i];
         }
         if (res[i] is KumaNumber)
         {
             res[i] = KumaNumber.Convert(res[i]);
         }
         if (res[i] is KumaArray)
         {
             res[i] = ConvertElements((KumaArray)res[i]);
         }
         if (res[i] is KumaDictionary)
         {
             res[i] = ConvertElements((KumaDictionary)res[i]);
         }
     }
     return(res);
 }
Esempio n. 10
0
        internal static dynamic ConvertIfNumber(object value)
        {
            var number = value as KumaNumber;

            return(number != null?KumaNumber.Convert(number) : value);
        }
        private bool CheckForMatch(KumaNativeFunction function, List <FunctionArgument> args)
        {
            if (function.Arguments.Count == args.Count)
            {
                var _args = new List <object>();
                args.ForEach(arg => {
                    var val = CompilerServices.CreateLambdaForExpression(arg.Value)();
                    if (val is KumaString)
                    {
                        val = (string)val;
                    }
                    if (val is KumaNumber)
                    {
                        val = KumaNumber.Convert((KumaNumber)val);
                    }
                    _args.Add(val);
                });
                var match = true;
                var i     = 0;
                foreach (var param in function.Method.GetParameters())
                {
                    if (_args[i++].GetType() != param.ParameterType)
                    {
                        match = false;
                        break;
                    }
                }
                return(match);
            }
            if (args.Count > function.Arguments.Count)
            {
                if (function.Arguments.Any() && function.Arguments.Last().IsVarArg)
                {
                    return(true);
                }
                return(false);
            }
            var myCount    = args.Count;
            var theirCount = function.Arguments.Count;

            function.Arguments.ForEach(arg => {
                if (arg.HasDefault)
                {
                    theirCount--;
                }
            });
            var vo = 0;

            if (function.Arguments.Any() && function.Arguments.Last().IsVarArg)
            {
                vo = 1;
            }
            if (myCount == theirCount)
            {
                return(true);
            }
            if (myCount + vo == theirCount)
            {
                return(true);
            }
            return(false);
        }