Exemple #1
0
        private static object ConvertToArray(ElaValue val, Type el)
        {
            var seq = (IEnumerable <ElaValue>)val.Ref;
            var len = 0;

            if (val.Ref is ElaList)
            {
                len = ((ElaList)val.Ref).Length;
            }
            else if (val.Ref is ElaTuple)
            {
                len = ((ElaTuple)val.Ref).Length;
            }

            var arr = Array.CreateInstance(el, len);
            var i   = 0;

            foreach (var e in seq)
            {
                var o = Convert(e, el);
                arr.SetValue(o, i++);
            }

            return(arr);
        }
Exemple #2
0
 public void NoOperator(ElaValue value, string op)
 {
     Fail(ElaRuntimeError.InvalidOp, value.GetTypeName(), op);
 }
Exemple #3
0
 public void UnknownField(string field, ElaValue given)
 {
     Fail(ElaRuntimeError.UnknownField, field, given.ToString(), given.GetTypeName());
 }
Exemple #4
0
 public void InvalidType(string expected, ElaValue given)
 {
     Fail(ElaRuntimeError.InvalidType, expected, given.GetTypeName());
 }
Exemple #5
0
 public void InvalidConstructorCode(ElaValue code)
 {
     Fail(ElaRuntimeError.InvalidConstructorCode, code.I4);
 }
Exemple #6
0
 public void InvalidTypeCode(ElaValue code)
 {
     Fail(ElaRuntimeError.InvalidTypeCode, code.I4);
 }
Exemple #7
0
 public void IndexOutOfRange(ElaValue index, ElaValue obj)
 {
     Fail(ElaRuntimeError.IndexOutOfRange, index.ToString(), index.GetTypeName(),
          obj.ToString(), obj.GetTypeName());
 }
Exemple #8
0
 public void NotAlgebraicType(ElaValue val)
 {
     Fail(ElaRuntimeError.NotAlgebraicType, val.GetTypeName());
 }
Exemple #9
0
 public void InvalidIndexType(ElaValue index)
 {
     Fail(ElaRuntimeError.InvalidIndexType, index.GetTypeName());
 }
Exemple #10
0
 public void DivideByZero(ElaValue value)
 {
     Fail(ElaRuntimeError.DivideByZero, value.ToString(), value.GetTypeName());
 }
Exemple #11
0
 public void InvalidFormat(string format, ElaValue value)
 {
     Fail(ElaRuntimeError.InvalidFormat, format, value.ToString(), value.GetTypeName());
 }
Exemple #12
0
 private static Exception InvalidCast(ElaValue val, System.Type target)
 {
     return(new InvalidCastException(Strings.GetMessage("InvalidCast", TCF.GetShortForm(val.TypeCode),
                                                        target.Name)));
 }
Exemple #13
0
 private static Exception InvalidCast(ElaValue val, ElaTypeCode type)
 {
     return(new ElaRuntimeException(ElaRuntimeError.InvalidType, TCF.GetShortForm(type), val.GetTypeName()));
 }
Exemple #14
0
        public static object Convert(ElaValue val, Type ti)
        {
            if (ti == typeof(Int32) && val.TypeId == ElaMachine.INT)
            {
                return(val.I4);
            }
            else if (ti == typeof(Single) && val.TypeId <= ElaMachine.REA)
            {
                return(val.GetSingle());
            }
            else if (ti == typeof(Int64) && val.TypeId <= ElaMachine.LNG)
            {
                return(val.GetLong());
            }
            else if (ti == typeof(Double) && val.TypeId <= ElaMachine.DBL)
            {
                return(val.GetDouble());
            }
            else if (ti == typeof(Boolean) && val.TypeId == ElaMachine.BYT)
            {
                return(val.I4 == 1);
            }
            else if (ti == typeof(String) && val.TypeId == ElaMachine.STR)
            {
                return(val.DirectGetString());
            }
            else if (ti == typeof(Char) && val.TypeId == ElaMachine.CHR)
            {
                return((Char)val.I4);
            }
            else if (ti == typeof(ElaList) && val.TypeId == ElaMachine.LST)
            {
                return((ElaList)val.Ref);
            }
            else if (ti == typeof(ElaRecord) && val.TypeId == ElaMachine.REC)
            {
                return((ElaRecord)val.Ref);
            }
            else if (ti == typeof(ElaTuple) && val.TypeId == ElaMachine.TUP)
            {
                return((ElaTuple)val.Ref);
            }
            else if (ti == typeof(ElaFunction) && val.TypeId == ElaMachine.FUN)
            {
                return((ElaFunction)val.Ref);
            }
            else if (ti == typeof(ElaModule) && val.TypeId == ElaMachine.MOD)
            {
                return((ElaModule)val.Ref);
            }
            else if (ti == typeof(ElaUnit) && val.TypeId == ElaMachine.UNI)
            {
                return(ElaUnit.Instance);
            }
            else if (ti == typeof(ElaObject))
            {
                return(val.Ref);
            }
            else if (ti == typeof(ElaValue))
            {
                return(val);
            }
            else if (ti == typeof(Object))
            {
                return(val.AsObject());
            }
            else if (val.TypeId == ElaMachine.LAZ)
            {
                var la = (ElaLazy)val.Ref;

                if (la.Evaled)
                {
                    return(Convert(la.Value, ti));
                }
                else
                {
                    throw InvalidCast(val, TypeToElaTypeCode(ti));
                }
            }
            else if (ti.IsArray)
            {
                return(ConvertToArray(val, ti.GetElementType()));
            }
            else if (ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                return(ConvertToArray(val, ti.GetGenericArguments()[0]));
            }
            else
            {
                try
                {
                    return(System.Convert.ChangeType(val.Ref, ti));
                }
                catch (Exception) { }
            }

            throw InvalidCast(val, TypeToElaTypeCode(ti));
        }
Exemple #15
0
        public static T Convert <T>(ElaValue val)
        {
            var ti = typeof(T);

            return((T)Convert(val, ti));
        }
Exemple #16
0
 internal ExecutionResult(ElaValue val)
 {
     ReturnValue = val;
 }