Example #1
0
        static object ConvertFromSchemeObject <T>(object o)
        {
            try
            {
                switch (Type.GetTypeCode(o.GetType()))
                {
                case TypeCode.Boolean:
                case TypeCode.Char:
                case TypeCode.DateTime:
                case TypeCode.DBNull:
                case TypeCode.Object:
                    return((T)o);

                default:
                    switch (Type.GetTypeCode(typeof(T)))
                    {
                    case TypeCode.Boolean:
                        return(Builtins.IsTrue(o));

                    case TypeCode.Byte:
                        return(Convert.ToByte(o));

                    case TypeCode.Char:
                        return(Convert.ToChar(o));

                    case TypeCode.DateTime:
                        return(Convert.ToDateTime(o));

                    case TypeCode.Decimal:
                        return(Convert.ToDecimal(o));

                    case TypeCode.Double:
                        return(Convert.ToDouble(o));

                    case TypeCode.Int16:
                        return(Convert.ToInt16(o));

                    case TypeCode.Int32:
                        return(Convert.ToInt32(o));

                    case TypeCode.Int64:
                        return(Convert.ToInt64(o));

                    case TypeCode.SByte:
                        return(Convert.ToSByte(o));

                    case TypeCode.Single:
                        return(Convert.ToSingle(o));

                    case TypeCode.String:
                        return(Convert.ToString(o));

                    case TypeCode.UInt16:
                        return(Convert.ToUInt16(o));

                    case TypeCode.UInt32:
                        return(Convert.ToUInt32(o));

                    case TypeCode.UInt64:
                        return(Convert.ToUInt64(o));

                    default:
                        return((T)o);
                    }
                }
            }
            catch
            {
                // nothing i can do now :\
                return(null);
            }
        }
Example #2
0
        public static Expression MakeRecordTypeDescriptor(Expression[] obj)
        {
            if ((obj.Length == 6 || obj.Length == 7) && IronScheme.Compiler.Generator.VarHint != SymbolId.Empty)
            {
                try
                {
                    var name     = Unwrap(obj[0]);
                    var parent   = Unwrap(obj[1]);
                    var uid      = Unwrap(obj[2]);
                    var issealed = Unwrap(obj[3]);
                    var isopaque = Unwrap(obj[4]);
                    var fields   = obj[5];
                    if (fields is BoundExpression)
                    {
                        return(null);
                    }

                    if (name is BoundExpression)
                    {
                        return(null);
                    }

                    var rname   = ((ConstantExpression)name).Value;
                    var ruid    = ((ConstantExpression)uid).Value;
                    var rsealed = ((ConstantExpression)issealed).Value;
                    var ropaque = ((ConstantExpression)isopaque).Value;

                    object[] rfields = { };

                    if (fields is NewArrayExpression)
                    {
                        var ff      = ((NewArrayExpression)fields).Expressions;
                        var dfields = new Expression[ff.Count];
                        ff.CopyTo(dfields, 0);

                        rfields = Array.ConvertAll(dfields, x => ((ConstantExpression)x).Value);
                    }

                    object[] tfields = Array.ConvertAll(rfields, x => SymbolTable.StringToObject("Object"));

                    if (obj.Length == 7)
                    {
                        var ftypes = obj[6];

                        if (ftypes is NewArrayExpression)
                        {
                            var ff      = ((NewArrayExpression)ftypes).Expressions;
                            var dfields = new Expression[ff.Count];
                            ff.CopyTo(dfields, 0);

                            tfields = Array.ConvertAll(dfields, x => ((ConstantExpression)((UnaryExpression)x).Operand).Value);
                        }
                    }

                    if (!Builtins.IsTrue(ruid))
                    {
                        ruid   = Guid.NewGuid().ToString(); //TODO: recall why this was done :\ Change to gensym if possible
                        obj[2] = Ast.Convert(Ast.Constant(ruid), typeof(object));
                    }

                    object par = null;

                    if (parent is BoundExpression)
                    {
                        par = ((BoundExpression)parent).Variable.Name;
                    }

                    var rtdc = new RecordTypeDescriptorConstant
                    {
                        RecordName = rname,
                        Uid        = ruid,
                        Sealed     = rsealed,
                        Opaque     = ropaque,
                        Parent     = par,
                        Fields     = rfields,
                        FieldTypes = tfields,
                        NameHint   = IronScheme.Compiler.Generator.VarHint,
                    };

                    var at = rtdc.Generate();

                    if (at != null)
                    {
                        ClrGenerator.AddCompileTimeType(at);
                    }

                    var e = Ast.Constant(rtdc);

                    return(Ast.Comma(e, Ast.Call(typeof(Records).GetMethod("MakeRecordTypeDescriptor"), obj)));
                }
                catch
                {
                    throw;
                    //kaboom, redirect to runtime
                }
            }

            return(null);
        }