Skip to content

Commit

Permalink
Исправление процесса сериализации типов объектов (ObjectType) (#2)
Browse files Browse the repository at this point in the history
* идея переработки сериализации в строку ObjectType.cs

* remove unnecessary formatting

* some work on it

* Добавил пакет с контрактом паттерна "Посетитель", на который переведу AST

* тест, который отражает суть бага, исправляемого в рамках #8

* Исправление бага по разрешению ссылок (#8)

* Скелет исправления бага через паттерн "Посетитель"

* Реализация некоторых посещений, удаление неактуального функционала, доработка тестов

* Доработка функционала:
- если мы определяем посещение компонента виртуальным методом, то переопределять его надо только в том наследнике компонента, для которого существует специальная логика посещения
- доработка тестов

* Печать объектного типа с помощью паттерна "Посетитель"
  • Loading branch information
Stepami authored Aug 4, 2022
1 parent 06121a6 commit bef5575
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 115 deletions.
1 change: 1 addition & 0 deletions Interpreter.Lib/Interpreter.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Visitor.NET" Version="1.0.0" />
</ItemGroup>

</Project>
28 changes: 10 additions & 18 deletions Interpreter.Lib/Semantic/Types/ArrayType.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
using Interpreter.Lib.Semantic.Types.Visitors;
using Visitor.NET.Lib.Core;

namespace Interpreter.Lib.Semantic.Types
{
public class ArrayType : Type
{
public Type Type { get; private set; }
public Type Type { get; set; }

public ArrayType(Type type) : base($"{type}[]")
{
Type = type;
}

public override void ResolveReference(string reference, Type toAssign)
{
if (Type == reference)
{
Type = toAssign;
}
else switch (Type)
{
case ObjectType objectType:
objectType.ResolveSelfReferences(reference);
break;
default:
Type.ResolveReference(reference, toAssign);
break;
}
}

public override Unit Accept(ReferenceResolver visitor) =>
visitor.Visit(this);

public override string Accept(ObjectTypePrinter visitor) =>
visitor.Visit(this);

public override bool Equals(object obj)
{
Expand Down
44 changes: 9 additions & 35 deletions Interpreter.Lib/Semantic/Types/FunctionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interpreter.Lib.Semantic.Types.Visitors;
using Visitor.NET.Lib.Core;

namespace Interpreter.Lib.Semantic.Types
{
public class FunctionType : Type
{
public Type ReturnType { get; private set; }
public Type ReturnType { get; set; }

public List<Type> Arguments { get; }

Expand All @@ -17,40 +19,12 @@ public FunctionType(Type returnType, IEnumerable<Type> arguments)
Arguments = new List<Type>(arguments);
}

public override void ResolveReference(string reference, Type toAssign)
{
if (ReturnType.ToString() == reference)
{
ReturnType = toAssign;
}
else switch (ReturnType)
{
case ObjectType objectType:
objectType.ResolveSelfReferences(reference);
break;
default:
ReturnType.ResolveReference(reference, toAssign);
break;
}

for (var i = 0; i < Arguments.Count; i++)
{
if (Arguments[i].ToString() == reference)
{
Arguments[i] = toAssign;
}
else switch (Arguments[i])
{
case ObjectType objectType:
objectType.ResolveSelfReferences(reference);
break;
default:
Arguments[i].ResolveReference(reference, toAssign);
break;
}
}
}

public override Unit Accept(ReferenceResolver visitor) =>
visitor.Visit(this);

public override string Accept(ObjectTypePrinter visitor) =>
visitor.Visit(this);

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
Expand Down
26 changes: 9 additions & 17 deletions Interpreter.Lib/Semantic/Types/NullableType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Interpreter.Lib.Semantic.Types.Visitors;
using Visitor.NET.Lib.Core;

namespace Interpreter.Lib.Semantic.Types
{
public class NullableType : Type
{
public Type Type { get; private set; }
public Type Type { get; set; }

public NullableType(Type type) : base($"{type}?")
{
Expand All @@ -13,22 +16,11 @@ protected NullableType()
{
}

public override void ResolveReference(string reference, Type toAssign)
{
if (Type == reference)
{
Type = toAssign;
}
else switch (Type)
{
case ObjectType objectType:
objectType.ResolveSelfReferences(reference);
break;
default:
Type.ResolveReference(reference, toAssign);
break;
}
}
public override Unit Accept(ReferenceResolver visitor) =>
visitor.Visit(this);

public override string Accept(ObjectTypePrinter visitor) =>
visitor.Visit(this);

public override bool Equals(object obj)
{
Expand Down
57 changes: 20 additions & 37 deletions Interpreter.Lib/Semantic/Types/ObjectType.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interpreter.Lib.Semantic.Types.Visitors;
using Visitor.NET.Lib.Core;

namespace Interpreter.Lib.Semantic.Types
{
public class ObjectType : NullableType
{
private readonly Dictionary<string, Type> _properties;
private readonly ObjectTypePrinter _serializer;

public ObjectType(IEnumerable<PropertyType> properties)
{
Expand All @@ -17,38 +19,29 @@ public ObjectType(IEnumerable<PropertyType> properties)
x => x.Id,
x => x.Type
);
_serializer = new ObjectTypePrinter(this);
}

public Type this[string id] => _properties.ContainsKey(id)
? _properties[id]
: null;
public Type this[string id]
{
get => _properties.ContainsKey(id)
? _properties[id]
: null;
set => _properties[id] = value;
}

public IEnumerable<string> Keys => _properties.Keys;

public void ResolveSelfReferences(string self)
{
foreach (var (key, property) in _properties)
{
if (property == self)
{
_properties[key] = this;
}
else switch (property)
{
case ObjectType objectType:
if (objectType != this)
{
objectType.ResolveSelfReferences(self);
}
public void ResolveSelfReferences(string self) =>
new ReferenceResolver(this, self)
.Visit(this);

break;
default:
property.ResolveReference(self, this);
break;
}
}
}
public override Unit Accept(ReferenceResolver visitor) =>
visitor.Visit(this);

public override string Accept(ObjectTypePrinter visitor) =>
visitor.Visit(this);

public override bool Equals(object obj)
{
if (obj is ObjectType that)
Expand All @@ -70,17 +63,7 @@ public override int GetHashCode() =>
.Select(kvp => HashCode.Combine(kvp.Key, kvp.Value))
.Aggregate(HashCode.Combine);

public override string ToString() =>
new StringBuilder()
.Append('{')
.AppendJoin(
"",
_properties
.Select(kvp =>
$"{kvp.Key}: {kvp.GetHashCode()};")
)
.Append('}')
.ToString();
public override string ToString() => _serializer.Visit(this);
}

public record PropertyType(string Id, Type Type);
Expand Down
15 changes: 11 additions & 4 deletions Interpreter.Lib/Semantic/Types/Type.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Interpreter.Lib.Semantic.Types.Visitors;
using Visitor.NET.Lib.Core;

namespace Interpreter.Lib.Semantic.Types
{
public class Type
public class Type :
IVisitable<ReferenceResolver, Unit>,
IVisitable<ObjectTypePrinter, string>
{
private readonly string _name;

Expand All @@ -10,9 +15,11 @@ protected Type()

public Type(string name) => _name = name;

public virtual void ResolveReference(string reference, Type toAssign)
{
}
public virtual Unit Accept(ReferenceResolver visitor) =>
visitor.Visit(this);

public virtual string Accept(ObjectTypePrinter visitor) =>
visitor.Visit(this);

public override bool Equals(object obj)
{
Expand Down
75 changes: 75 additions & 0 deletions Interpreter.Lib/Semantic/Types/Visitors/ObjectTypePrinter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Linq;
using System.Text;
using Visitor.NET.Lib.Core;

namespace Interpreter.Lib.Semantic.Types.Visitors
{
public class ObjectTypePrinter :
IVisitor<Type, string>,
IVisitor<ObjectType, string>,
IVisitor<ArrayType, string>,
IVisitor<NullableType, string>,
IVisitor<FunctionType, string>
{
private readonly ObjectType _reference;

public ObjectTypePrinter(ObjectType reference) =>
_reference = reference;

public string Visit(Type visitable) =>
visitable.ToString();

public string Visit(ObjectType visitable)
{
var sb = new StringBuilder("{");
foreach (var key in visitable.Keys)
{
var type = visitable[key];
var prop = $"{key}: ";
prop += type.Equals(_reference)
? "@this"
: type.Accept(this);
sb.Append(prop).Append(';');
}

return sb.Append('}').ToString();
}

public string Visit(ArrayType visitable)
{
var sb = new StringBuilder();
sb.Append(visitable.Type.Equals(_reference)
? "@this"
: visitable.Type.Accept(this)
);

return sb.Append("[]").ToString();
}

public string Visit(NullableType visitable)
{
var sb = new StringBuilder();
sb.Append(visitable.Type.Equals(_reference)
? "@this"
: visitable.Type.Accept(this)
);

return sb.Append('?').ToString();
}

public string Visit(FunctionType visitable)
{
var sb = new StringBuilder("(");
sb.AppendJoin(", ", visitable.Arguments.Select(x => x.Equals(_reference)
? "@this"
: x.Accept(this)
)).Append(") => ");
sb.Append(visitable.ReturnType.Equals(_reference)
? "@this"
: visitable.ReturnType.Accept(this)
);

return sb.ToString();
}
}
}
Loading

0 comments on commit bef5575

Please sign in to comment.