Changes and tweaks for the binder

This commit is contained in:
Deukhoofd 2018-12-09 20:53:07 +01:00
parent 796f82e823
commit 3d9b7a53e9
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 53 additions and 20 deletions

View File

@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Evaluator;
@ -7,7 +8,7 @@ using Upsilon.Text;
namespace Upsilon.BaseTypes.UserData
{
internal class DictionaryUserData : ScriptType, IUserData, ILengthType
internal class DictionaryUserData : ScriptType, IUserData, ILengthType, IIterable
{
public IDictionary Dictionary { get; }
@ -73,5 +74,15 @@ namespace Upsilon.BaseTypes.UserData
return Equals((DictionaryUserData) obj);
}
public ScriptType GetValueFromIndex(ScriptType index)
{
return Dictionary[index.ToCSharpObject()].ToScriptType();
}
public IEnumerator<ScriptType> GetScriptEnumerator()
{
return (from object key in Dictionary.Keys
select key.ToScriptType()).GetEnumerator();
}
}
}

View File

@ -11,7 +11,7 @@ namespace Upsilon.BaseTypes.UserData
{
internal class ListUserData : ScriptType, IUserData, IIterable, ILengthType
{
private IList List { get; }
public IList List { get; }
public ListUserData(IList list)
{

View File

@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
@ -141,12 +143,6 @@ namespace Upsilon.BaseTypes.UserData
return Convert.ChangeType(numeric.ToCSharpObject(), typeCode);
}
break;
case TypeCode.Object:
if (value is ScriptType t)
{
return t.ToCSharpObject();
}
break;
}
if (value is ScriptNull)
@ -154,6 +150,44 @@ namespace Upsilon.BaseTypes.UserData
return null;
}
var valueType = value.GetType();
if (type == typeof(IIterable))
{
if (typeof(IDictionary).IsAssignableFrom(valueType))
{
return new DictionaryUserData((IDictionary) value);
}
if (typeof(IList).IsAssignableFrom(valueType))
{
return new ListUserData((IList) value);
}
}
if (typeof(IDictionary).IsAssignableFrom(type))
{
if (value is DictionaryUserData d)
return d.Dictionary;
if (value is ScriptTable.ScriptTable table)
{
var generics = type.GetGenericArguments();
var d1 = typeof(Dictionary<,>);
var requiredDictionaryType = d1.MakeGenericType(typeof(string), generics[1]);
var dictionary = (IDictionary)Activator.CreateInstance(requiredDictionaryType);
foreach (var variable in table.EvaluationScope.Variables)
{
dictionary.Add(variable.Key, variable.Value.ToCSharpObject());
}
return dictionary;
}
}
if (typeof(IList).IsAssignableFrom(type))
{
if (value is ListUserData d)
return d.List;
if (value is ScriptTable.ScriptTable table)
return table.EvaluationScope.Variables.Select(x => x.Value.ToCSharpObject());
}
var isScriptTypeRequired = typeof(ScriptType).IsAssignableFrom(type);
var isScriptType = value is ScriptType;
if (!isScriptTypeRequired && isScriptType)
@ -166,18 +200,6 @@ namespace Upsilon.BaseTypes.UserData
return value.ToScriptType();
}
if (type == typeof(IIterable))
{
var valueType = value.GetType();
if (typeof(IDictionary).IsAssignableFrom(valueType))
{
return new DictionaryUserData((IDictionary) value);
}
if (typeof(IList).IsAssignableFrom(valueType))
{
return new ListUserData((IList) value);
}
}
return value;
}