Files
Upsilon/Upsilon/BaseTypes/UserData/GenericUserData.cs

81 lines
2.4 KiB
C#
Raw Permalink Normal View History

2019-01-23 13:15:19 +01:00
using Upsilon.BoundTypes;
2018-11-19 17:22:25 +01:00
using Upsilon.Evaluator;
using Upsilon.Text;
2018-11-19 17:22:25 +01:00
namespace Upsilon.BaseTypes.UserData
{
internal sealed class GenericUserData : ScriptType, IUserData
2018-11-19 17:22:25 +01:00
{
public GenericUserData(object obj)
2018-11-19 17:22:25 +01:00
{
Value = obj;
2019-01-23 13:15:19 +01:00
var type = obj.GetType();
_typeInfo = UserDataTypeHandler.GetTypeInfo(type);
Type = new TypeContainer(_typeInfo.BoundTypeName);
2018-11-19 17:22:25 +01:00
}
2019-01-23 13:15:19 +01:00
public override TypeContainer Type { get; }
2018-11-19 17:22:25 +01:00
private object Value { get; }
2018-11-19 17:22:25 +01:00
private readonly UserDataType _typeInfo;
public override object ToCSharpObject()
{
return Value;
}
public override System.Type GetCSharpType()
{
return Value.GetType();
}
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
2018-11-19 17:22:25 +01:00
{
var s = index.ToCSharpObject().ToString();
var (type, failed, error) = _typeInfo.Get(Value, s);
if (failed)
{
diagnostics.LogError(error, span);
}
return type;
2018-11-19 17:22:25 +01:00
}
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value)
2018-11-19 17:22:25 +01:00
{
var s = index.ToCSharpObject().ToString();
var (failed, error) = _typeInfo.Set(Value, s, value);
if (failed)
{
diagnostics.LogError(error, span);
}
2018-11-19 17:22:25 +01:00
}
2018-11-23 18:18:07 +01:00
public (ScriptType Type, bool Failed) BinaryOperator(ScriptType par1, OperatorType op, ScriptType par2)
{
return _typeInfo.BinaryOperator(Value, par1, op, par2);
}
2018-11-23 18:18:07 +01:00
public (ScriptType Type, bool Failed) UnaryOperator(ScriptType par1, OperatorType op)
{
return _typeInfo.UnaryOperator(Value, par1, op);
}
2018-12-09 11:45:38 +01:00
protected bool Equals(GenericUserData other)
{
return Value.Equals(other.Value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
2018-12-09 13:47:13 +01:00
if (obj.GetType() == Value.GetType())
{
return obj.Equals(Value);
}
2018-12-09 11:45:38 +01:00
if (obj.GetType() != this.GetType()) return false;
return Equals((GenericUserData) obj);
}
2018-11-19 17:22:25 +01:00
}
}