/// <summary> /// Converts from c# datatypes to fluentscript datatypes inside /// </summary> /// <param name="val"></param> public static LObject ConvertToLangValue(LType ltype, object val) { if (val == null) return LObjects.Null; if (ltype == LTypes.Number) return new LNumber(Convert.ToDouble(val)); if (ltype == LTypes.String) return new LString(Convert.ToString(val)); if (ltype == LTypes.Date) return new LDate(Convert.ToDateTime(val)); if (ltype == LTypes.Time) { var valText = Convert.ToString(val); var result = DateTimeTypeHelper.ParseTime(valText); var time = result.Item1; return new LTime(time); } if (ltype == LTypes.Bool) return new LBool(Convert.ToBoolean(val)); return LObjects.Null; }
/// <summary> /// Registers methods on a specific type if no existing methods implementation are already /// registered for the type. /// </summary> /// <param name="type">The datatype for which the methods implementation are applicable</param> /// <param name="methods">The method implementations</param> public void RegisterIfNotPresent(LType type, ITypeMethods methods) { if (!_typeToMethods.ContainsKey(type)) { _typeToMethods[type] = methods; methods.OnRegistered(); } }
/// <summary> /// Register the type /// </summary> /// <param name="type">The type</param> /// <param name="fullName">The fullname of the type</param> /// <param name="name">The short name of the type</param> public static void RegisterAlias(LType type, string fullName, string name) { _types[fullName] = type; if (type.IsSystemType) { _sysBasicTypes[name] = type; } }
/// <summary> /// Expects the objects type to be of the supplied type. /// </summary> /// <param name="node"></param> /// <param name="result"></param> /// <param name="lType"></param> public static void ExpectType(AstNode node, object result, LType lType) { if(!(result is LObject) || ((LObject)result).Type != lType) { throw new LangException("Runtime Error", "Expected type " + lType.Name, node.Ref.ScriptName, node.Ref.Line, node.Ref.CharPos); } }
/// <summary> /// Expects the objects type to be of the supplied type. /// </summary> /// <param name="node"></param> /// <param name="result"></param> /// <param name="lType"></param> public static void NotNullType(AstNode node, object obj, string nullMessage, LType lType) { if (obj == null || obj == LObjects.Null) { throw new LangException("Runtime Error", nullMessage, node.Ref.ScriptName, node.Ref.Line, node.Ref.CharPos); } if (!(obj is LObject) || ((LObject)obj).Type != lType) { throw new LangException("Runtime Error", "Expected type " + lType.Name, node.Ref.ScriptName, node.Ref.Line, node.Ref.CharPos); } }
/// <summary> /// Register methods on a specific type. /// </summary> /// <param name="type">The datatype for which the methods implementation are applicable</param> /// <param name="methods">The method implementations</param> public void Register(LType type, ITypeMethods methods) { _typeToMethods[type] = methods; }
/// <summary> /// Get the type in the host language that represents the same type in fluentscript. /// </summary> /// <param name="ltype">The LType in fluentscript.</param> /// <returns></returns> public static Type ConvertToHostLangType(LType ltype) { if (ltype == LTypes.Bool) return typeof(bool); if (ltype == LTypes.Date) return typeof(DateTime); //if (ltype == LTypes.Number) return typeof(int); if (ltype == LTypes.Number) return typeof(double); if (ltype == LTypes.String) return typeof(string); if (ltype == LTypes.Time) return typeof(TimeSpan); if (ltype == LTypes.Array) return typeof(IList); if (ltype == LTypes.Map) return typeof(IDictionary); if (ltype == LTypes.Null) return typeof(Nullable); return typeof(object); }
/// <summary> /// Register the type /// </summary> /// <param name="type">The type</param> /// <param name="fullName">The fullname of the type</param> /// <param name="name">The short name of the type</param> public static void RegisterAlias(LType type, string fullName, string name) { _types[fullName] = type; if (type.IsSystemType) _sysBasicTypes[name] = type; }
/// <summary> /// Register the type /// </summary> /// <param name="type"></param> public static void Register(LType type) { RegisterAlias(type, type.FullName, type.Name); }
/// <summary> /// Get the methods implementation for the supplied type. /// </summary> /// <param name="type"></param> public ITypeMethods Get(LType type) { return(_typeToMethods[type]); }
/// <summary> /// Whether or not there are methods for the supplied type. /// </summary> /// <param name="type"></param> public bool Contains(LType type) { return(_typeToMethods.ContainsKey(type)); }
/// <summary> /// Whether or not there are methods for the supplied type. /// </summary> /// <param name="type"></param> public bool Contains(LType type) { return _typeToMethods.ContainsKey(type); }
/// <summary> /// Is match with the type supplied and the /// </summary> /// <param name="type"></param> /// <param name="obj1"></param> /// <param name="obj2"></param> /// <returns></returns> private bool IsTypeMatch(LType type, LObject obj1, LObject obj2) { if (obj1.Type == type && obj2.Type == type) return true; return false; }
/// <summary> /// Get the methods implementation for the supplied type. /// </summary> /// <param name="type"></param> public ITypeMethods Get(LType type) { return _typeToMethods[type]; }