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); }
public void NoOperator(ElaValue value, string op) { Fail(ElaRuntimeError.InvalidOp, value.GetTypeName(), op); }
public void UnknownField(string field, ElaValue given) { Fail(ElaRuntimeError.UnknownField, field, given.ToString(), given.GetTypeName()); }
public void InvalidType(string expected, ElaValue given) { Fail(ElaRuntimeError.InvalidType, expected, given.GetTypeName()); }
public void InvalidConstructorCode(ElaValue code) { Fail(ElaRuntimeError.InvalidConstructorCode, code.I4); }
public void InvalidTypeCode(ElaValue code) { Fail(ElaRuntimeError.InvalidTypeCode, code.I4); }
public void IndexOutOfRange(ElaValue index, ElaValue obj) { Fail(ElaRuntimeError.IndexOutOfRange, index.ToString(), index.GetTypeName(), obj.ToString(), obj.GetTypeName()); }
public void NotAlgebraicType(ElaValue val) { Fail(ElaRuntimeError.NotAlgebraicType, val.GetTypeName()); }
public void InvalidIndexType(ElaValue index) { Fail(ElaRuntimeError.InvalidIndexType, index.GetTypeName()); }
public void DivideByZero(ElaValue value) { Fail(ElaRuntimeError.DivideByZero, value.ToString(), value.GetTypeName()); }
public void InvalidFormat(string format, ElaValue value) { Fail(ElaRuntimeError.InvalidFormat, format, value.ToString(), value.GetTypeName()); }
private static Exception InvalidCast(ElaValue val, System.Type target) { return(new InvalidCastException(Strings.GetMessage("InvalidCast", TCF.GetShortForm(val.TypeCode), target.Name))); }
private static Exception InvalidCast(ElaValue val, ElaTypeCode type) { return(new ElaRuntimeException(ElaRuntimeError.InvalidType, TCF.GetShortForm(type), val.GetTypeName())); }
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)); }
public static T Convert <T>(ElaValue val) { var ti = typeof(T); return((T)Convert(val, ti)); }
internal ExecutionResult(ElaValue val) { ReturnValue = val; }