Handle conversion for enumerables

This commit is contained in:
Deukhoofd 2018-12-09 15:51:16 +01:00
parent 74bc57bb1a
commit 5d7f155550
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 53 additions and 14 deletions

View File

@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Upsilon.Evaluator; using Upsilon.Evaluator;
namespace Upsilon.BaseTypes.ScriptTable namespace Upsilon.BaseTypes.ScriptTable
@ -8,5 +10,16 @@ namespace Upsilon.BaseTypes.ScriptTable
{ {
} }
public override System.Type GetCSharpType()
{
return typeof(Dictionary<string, object>);
}
public override object ToCSharpObject()
{
return EvaluationScope.Variables.ToDictionary(x => x.Key, x => x.Value.ToCSharpObject());
}
} }
} }

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Upsilon.Evaluator; using Upsilon.Evaluator;
namespace Upsilon.BaseTypes.ScriptTable namespace Upsilon.BaseTypes.ScriptTable
@ -22,5 +23,16 @@ namespace Upsilon.BaseTypes.ScriptTable
yield return variable.Value; yield return variable.Value;
} }
} }
public override System.Type GetCSharpType()
{
return typeof(IEnumerable<object>);
}
public override object ToCSharpObject()
{
return EvaluationScope.Variables.Select(x => x.Value.ToCSharpObject()).ToArray();
}
} }
} }

View File

@ -19,15 +19,6 @@ namespace Upsilon.BaseTypes.ScriptTable
} }
public override Type Type => Type.Table; public override Type Type => Type.Table;
public override object ToCSharpObject()
{
return EvaluationScope.Variables.ToDictionary(x => x.Key, x => x.Value.ToCSharpObject());
}
public override System.Type GetCSharpType()
{
return typeof(Dictionary<string, object>);
}
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope) public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
{ {

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes; using Upsilon.BaseTypes;
using Upsilon.Binder; using Upsilon.Binder;
using Upsilon.Parser; using Upsilon.Parser;
@ -115,11 +117,32 @@ namespace Upsilon.Evaluator
{ {
if (t == null) if (t == null)
return default(T); return default(T);
if (typeof(T) == typeof(double)) var res = Convert(typeof(T), t);
return (T)(object)System.Convert.ToDouble(t.ToCSharpObject()); if (res == null)
if (typeof(T) == typeof(long)) return default(T);
return (T)(object)System.Convert.ToInt64(t.ToCSharpObject()); if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
return (T) t.ToCSharpObject(); {
if (typeof(T).IsGenericType)
{
var elementType = typeof(T).GetGenericArguments()[0];
var input = (object[])res;
Array array = Array.CreateInstance(elementType, input.Length);
input.CopyTo(array, 0);
res = array;
}
}
return (T)res;
}
private static object Convert(System.Type type, ScriptType t)
{
if (t == null)
return null;
if (type == typeof(double))
return System.Convert.ToDouble(t.ToCSharpObject());
if (type == typeof(long))
return System.Convert.ToInt64(t.ToCSharpObject());
return t.ToCSharpObject();
} }
private static object Convert(ScriptType t) private static object Convert(ScriptType t)