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 NovaNumber) { value = NovaNumber.Convert((NovaNumber)value); } if (value is bool) { return((bool)value); } if (numberTypes.Contains(value.GetType())) { dynamic _value = value; return(_value != 0); } return(value != null); }
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 NovaNumber ? NovaNumber.Convert((NovaNumber)p) : p)); return(multicastDelegate != null?multicastDelegate.DynamicInvoke(realParams.ToArray()) : null); }
internal static dynamic Invoke(Type targetType, MethodBase minfo, List <FunctionArgument> args, NovaScope 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 NovaString) { _val = (string)_val; } if (_val is NovaNumber) { _val = NovaNumber.Convert((NovaNumber)_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 NovaInstance && !(target is NovaBoxedInstance) && ((NovaInstance)target).BackingObject != null) { target = ((NovaInstance)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(Nova.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 NovaInstance) { var so = (NovaInstance)res; if (so is NovaBoxedInstance) { res = ((NovaBoxedInstance)so).BoxedObject; } } else if (res is NovaNumber) { res = NovaNumber.Convert(res); } else if (res is NovaString) { res = (string)res; } else if (res is NovaArray) { res = ConvertElements((NovaArray)res); } else if (res is NovaDictionary) { res = ConvertElements((NovaDictionary)res); } body.Scope.MergeIntoScope(scope); return(res); }
public dynamic this[Symbol sym] { get { return(Resolve(sym, this)); } set { var val = value; if (val is string) { val = new NovaString(val); } if (NovaNumber.IsConvertable(val)) { val = new NovaNumber(val); } SymVars[sym] = val; } }
// Nova -> .net internal static void SyncInstanceVariablesFrom(NovaInstance NovaObject, object obj) { var _fields = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList(); NovaObject.InstanceVariables.Variables.Keys.ToList().ForEach(key => { var _fq = _fields.Where(field => field.Name == key); if (_fq.Any()) { var val = NovaObject.InstanceVariables[key]; if (val is NovaNumber) { val = NovaNumber.Convert(val); } _fq.First().SetValue(obj, val); } }); }
private dynamic ConvertElements(NovaDictionary res) { List <dynamic> keysToRemove = new List <object>(); keysToRemove.AddRange(res.Keys.OfType <NovaString>()); 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 NovaString || res[key] is NovaNumber || res[key] is NovaArray || res[key] is NovaDictionary)); keysToRemove.ForEach(o => { if (res[o] is NovaString) { string s = res[o]; res[o] = s; } else if (res[o] is NovaNumber) { res[o] = NovaNumber.Convert(res[o]); } else if (res[o] is NovaArray) { res[o] = ConvertElements((NovaArray)res[o]); } else if (res[o] is NovaDictionary) { res[o] = ConvertElements((NovaDictionary)res[o]); } }); return(res); }
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 NovaString(val); } if (NovaNumber.IsConvertable(val)) { val = new NovaNumber(val); } Variables[name] = val; } }
private dynamic ConvertElements(NovaArray res) { for (var i = 0; i < res.Count(); i++) { if (res[i] is NovaString) { res[i] = (string)res[i]; } if (res[i] is NovaNumber) { res[i] = NovaNumber.Convert(res[i]); } if (res[i] is NovaArray) { res[i] = ConvertElements((NovaArray)res[i]); } if (res[i] is NovaDictionary) { res[i] = ConvertElements((NovaDictionary)res[i]); } } return(res); }
internal static dynamic ConvertIfNumber(object value) { var number = value as NovaNumber; return(number != null?NovaNumber.Convert(number) : value); }
private bool CheckForMatch(NovaNativeFunction 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 NovaString) { val = (string)val; } if (val is NovaNumber) { val = NovaNumber.Convert((NovaNumber)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); }