Fixed issue with userdata type on evaluation

This commit is contained in:
2019-01-19 17:50:54 +01:00
parent cf023af50d
commit 246aba3e95
14 changed files with 36 additions and 25 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.BoundTypes;
using Upsilon.Evaluator;
using Upsilon.Text;
@@ -11,10 +12,12 @@ namespace Upsilon.BaseTypes.UserData
internal class DictionaryUserData : ScriptType, IUserData, ILengthType, IIterable
{
public IDictionary Dictionary { get; }
private string BoundTypeName { get; }
public DictionaryUserData(IDictionary dictionary)
{
Dictionary = dictionary;
BoundTypeName = BoundTypeHandler.GetTypeName(dictionary.GetType());
}
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
@@ -41,7 +44,7 @@ namespace Upsilon.BaseTypes.UserData
}
}
public override Type Type => Type.UserData;
public override TypeContainer Type => new TypeContainer(BoundTypeName);
public override object ToCSharpObject()
{
return Dictionary;

View File

@@ -10,7 +10,7 @@ namespace Upsilon.BaseTypes.UserData
Value = obj;
_typeInfo = UserDataTypeHandler.GetTypeInfo(obj.GetType());
}
public override Type Type => Type.UserData;
public override TypeContainer Type => new TypeContainer(_typeInfo.BoundTypeName);
private object Value { get; }
private readonly UserDataType _typeInfo;

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.BoundTypes;
using Upsilon.Evaluator;
using Upsilon.Exceptions;
using Upsilon.Text;
@@ -12,21 +13,23 @@ namespace Upsilon.BaseTypes.UserData
internal class ListUserData : ScriptType, IUserData, IIterable, ILengthType
{
public IList List { get; }
public string TypeName { get; }
public ListUserData(IList list)
{
List = list;
TypeName = BoundTypeHandler.GetTypeName(list.GetType());
}
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
{
int i;
if (index.Type == Type.String)
if (index.Type == BaseTypes.Type.String)
{
var str = (ScriptString)index;
i = int.Parse(str.Value) - 1;
}
else if (index.Type == Type.Number)
else if (index.Type == BaseTypes.Type.Number)
{
var ind = (Number.ScriptNumberLong) index;
i = (int) ind.Value - 1;
@@ -41,11 +44,11 @@ namespace Upsilon.BaseTypes.UserData
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType scriptIndex, ScriptType value)
{
var index = -1;
if (scriptIndex.Type == Type.Number || scriptIndex.Type == Type.Unknown)
if (scriptIndex.Type == BaseTypes.Type.Number || scriptIndex.Type == BaseTypes.Type.Unknown)
{
index = Convert.ToInt32(scriptIndex.ToCSharpObject());
}
else if (scriptIndex.Type == Type.String)
else if (scriptIndex.Type == BaseTypes.Type.String)
{
index = int.Parse(((ScriptString) scriptIndex).Value);
}
@@ -75,7 +78,7 @@ namespace Upsilon.BaseTypes.UserData
}
}
public override Type Type => Type.UserData;
public override TypeContainer Type => new TypeContainer(TypeName);
public override object ToCSharpObject()
{
return List;

View File

@@ -3,6 +3,7 @@ using System.Globalization;
using System.Linq;
using System.Reflection;
using Upsilon.BaseTypes.ScriptFunction;
using Upsilon.BoundTypes;
using Upsilon.StandardLibraries;
namespace Upsilon.BaseTypes.UserData
@@ -16,6 +17,7 @@ namespace Upsilon.BaseTypes.UserData
type = type.GetGenericTypeDefinition();
}
Type = type;
BoundTypeName = BoundTypeHandler.GetTypeName(type);
Variables = type.GetFields().ToDictionary(x => x.Name.ToLowerInvariant(), x => x);
Properties = type.GetProperties().ToDictionary(x => x.Name.ToLowerInvariant(), x => x);
Methods = new Dictionary<string, UserDataMethod>();
@@ -44,6 +46,7 @@ namespace Upsilon.BaseTypes.UserData
OperatorHandler = new UserDataTypeOperators(type);
}
public string BoundTypeName { get; }
private System.Type Type { get; }
private Dictionary<string, FieldInfo> Variables { get; }
private Dictionary<string, PropertyInfo> Properties { get; }