Skip to content

Commit

Permalink
ergo-driven dialogues
Browse files Browse the repository at this point in the history
  • Loading branch information
G3Kappa committed Jan 17, 2024
1 parent 5b02e32 commit 6449f7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Ergo/Lang/Types/ErgoTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,31 @@ public virtual object FromTerm(ITerm t)
}
else
{
if (Type.IsArray && t is List list)
if (t is List list)
{
var instance = Array.CreateInstance(Type.GetElementType(), list.Contents.Length);
for (var i = 0; i < list.Contents.Length; i++)
if (Type.IsArray)
{
var obj = TermMarshall.FromTerm(list.Contents[i], Type.GetElementType(), Marshalling);
instance.SetValue(obj, i);
var instance = Array.CreateInstance(Type.GetElementType(), list.Contents.Length);
for (var i = 0; i < list.Contents.Length; i++)
{
var obj = TermMarshall.FromTerm(list.Contents[i], Type.GetElementType(), Marshalling);
instance.SetValue(obj, i);
}

return instance;
}
else if (Type.GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>)) is { } iList)
{
var instance = (IList)Activator.CreateInstance(Type);
for (var i = 0; i < list.Contents.Length; i++)
{
var obj = TermMarshall.FromTerm(list.Contents[i], Type.GetElementType(), Marshalling);
instance.Add(obj);
}

return instance;
return instance;
}
else throw new NotSupportedException();
}
else
{
Expand Down
10 changes: 10 additions & 0 deletions Ergo/Lang/Types/TermMarshall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Ergo.Lang;
public sealed class TermMarshall
{

public readonly record struct Unmarshalled(object Value);

internal static readonly ConcurrentDictionary<Type, TermAttribute> AttributeCache = new();
internal static readonly ConcurrentDictionary<Type, ITypeResolver> PositionalResolvers = new();
internal static readonly ConcurrentDictionary<Type, ITypeResolver> NamedResolvers = new();
Expand Down Expand Up @@ -54,6 +56,8 @@ private static TermAttribute GetAttribute(Type type)

public static ITerm ToTerm<T>(T value, Maybe<Atom> functor = default, Maybe<TermMarshalling> mode = default, TermMarshallingContext ctx = null)
{
if (value is Unmarshalled)
return new Atom(value);
if (typeof(T) == typeof(ITerm))
return (ITerm)value;
ctx ??= new();
Expand All @@ -69,6 +73,8 @@ public static ITerm ToTerm<T>(T value, Maybe<Atom> functor = default, Maybe<Term
}
public static ITerm ToTerm(object value, Type type, Maybe<Atom> functor = default, Maybe<TermMarshalling> mode = default, TermMarshallingContext ctx = null)
{
if (value is Unmarshalled)
return new Atom(value);
if (type == typeof(ITerm))
return (ITerm)value;
ctx ??= new();
Expand Down Expand Up @@ -101,6 +107,8 @@ internal static object Transform(object o)
}
public static T FromTerm<T>(ITerm value, T _ = default, Maybe<TermMarshalling> mode = default)
{
if (value is Atom { Value: Unmarshalled { Value: var v } })
return (T)v;
if (typeof(T) == typeof(ITerm))
return (T)value;
var interfaceType = typeof(IErgoMarshalling<>).MakeGenericType(typeof(T));
Expand All @@ -116,6 +124,8 @@ public static T FromTerm<T>(ITerm value, T _ = default, Maybe<TermMarshalling> m
}
public static object FromTerm(ITerm value, Type type, Maybe<TermMarshalling> mode = default)
{
if (value is Atom { Value: Unmarshalled { Value: var v } })
return v;
if (type == typeof(ITerm))
return value;
var interfaceType = typeof(IErgoMarshalling<>).MakeGenericType(type);
Expand Down

0 comments on commit 6449f7c

Please sign in to comment.