From 5d7f15555077207f14ce4460596926727551d6d3 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 9 Dec 2018 15:51:16 +0100 Subject: [PATCH] Handle conversion for enumerables --- .../ScriptTable/GenericKeyedScriptTable.cs | 13 ++++++++ .../ScriptTable/NumeratedScriptTable.cs | 12 +++++++ Upsilon/BaseTypes/ScriptTable/ScriptTable.cs | 9 ----- Upsilon/Evaluator/Script.cs | 33 ++++++++++++++++--- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs index fdfa008..c6b26d2 100644 --- a/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs +++ b/Upsilon/BaseTypes/ScriptTable/GenericKeyedScriptTable.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using Upsilon.Evaluator; namespace Upsilon.BaseTypes.ScriptTable @@ -8,5 +10,16 @@ namespace Upsilon.BaseTypes.ScriptTable { } + + public override System.Type GetCSharpType() + { + return typeof(Dictionary); + } + + public override object ToCSharpObject() + { + return EvaluationScope.Variables.ToDictionary(x => x.Key, x => x.Value.ToCSharpObject()); + } + } } \ No newline at end of file diff --git a/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs index 5e99a99..ffdb4e8 100644 --- a/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs +++ b/Upsilon/BaseTypes/ScriptTable/NumeratedScriptTable.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; using Upsilon.Evaluator; namespace Upsilon.BaseTypes.ScriptTable @@ -22,5 +23,16 @@ namespace Upsilon.BaseTypes.ScriptTable yield return variable.Value; } } + + public override System.Type GetCSharpType() + { + return typeof(IEnumerable); + } + + public override object ToCSharpObject() + { + return EvaluationScope.Variables.Select(x => x.Value.ToCSharpObject()).ToArray(); + } + } } \ No newline at end of file diff --git a/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs b/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs index 1f4836f..acd8cea 100644 --- a/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs +++ b/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs @@ -19,15 +19,6 @@ namespace Upsilon.BaseTypes.ScriptTable } 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); - } public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope) { diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index f117be2..b00402e 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -1,5 +1,7 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; using Upsilon.BaseTypes; using Upsilon.Binder; using Upsilon.Parser; @@ -115,11 +117,32 @@ namespace Upsilon.Evaluator { if (t == null) return default(T); - if (typeof(T) == typeof(double)) - return (T)(object)System.Convert.ToDouble(t.ToCSharpObject()); - if (typeof(T) == typeof(long)) - return (T)(object)System.Convert.ToInt64(t.ToCSharpObject()); - return (T) t.ToCSharpObject(); + var res = Convert(typeof(T), t); + if (res == null) + return default(T); + if (typeof(IEnumerable).IsAssignableFrom(typeof(T))) + { + 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)