Skip to content

Commit

Permalink
доработка вывода типов
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Feb 16, 2024
1 parent 568a835 commit 87b3dfd
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 66 deletions.
7 changes: 3 additions & 4 deletions Interpreter.Lib/IR/CheckSemantics/Types/ArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ public override void ResolveReference(

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (obj == null || GetType() != obj.GetType()) return false;
var that = (ArrayType) obj;
return Equals(Type, that.Type);
if (obj is ArrayType that)
return Equals(Type, that.Type);
return obj is Any;
}

public override int GetHashCode() =>
Expand Down
14 changes: 7 additions & 7 deletions Interpreter.Lib/IR/CheckSemantics/Types/FunctionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public override void ResolveReference(

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (obj == null || GetType() != obj.GetType()) return false;
var that = (FunctionType) obj;
return ReturnType.Equals(that.ReturnType) &&
Arguments.Count == that.Arguments.Count &&
Arguments.Zip(that.Arguments)
.All(pair => pair.First.Equals(pair.Second));
if (obj is FunctionType that)
return ReturnType.Equals(that.ReturnType) &&
Arguments.Count == that.Arguments.Count &&
Arguments.Zip(that.Arguments)
.All(pair => pair.First.Equals(pair.Second));

return obj is Any;
}

public override int GetHashCode() =>
Expand Down
6 changes: 2 additions & 4 deletions Interpreter.Lib/IR/CheckSemantics/Types/NullType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ public NullType() : base("null")
{
}

public override bool Equals(object obj)
{
return obj is NullableType or NullType;
}
public override bool Equals(object obj) =>
obj is NullableType or NullType or Any;

public override int GetHashCode() =>
"null".GetHashCode();
Expand Down
7 changes: 3 additions & 4 deletions Interpreter.Lib/IR/CheckSemantics/Types/NullableType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public override void ResolveReference(
public override bool Equals(object obj)
{
if (obj is NullableType that)
{
return Type.Equals(that.Type);
}
return obj is NullType;
return Equals(Type, that.Type);

return obj is NullType or Any;
}

public override int GetHashCode() =>
Expand Down
63 changes: 29 additions & 34 deletions Interpreter.Lib/IR/CheckSemantics/Types/ObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public ObjectType(IEnumerable<PropertyType> properties)
.OrderBy(x => x.Id)
.ToDictionary(
x => x.Id,
x => x.Type
);
x => x.Type);

_hasher = new ObjectTypeHasher(this);
_serializer = new ObjectTypePrinter(this);
Expand Down Expand Up @@ -46,17 +45,14 @@ public override void ResolveReference(
public override bool Equals(object obj)
{
if (obj is ObjectType that)
{
return ReferenceEquals(this, that) || _properties.Count == that._properties.Count &&
_properties
.Zip(that._properties)
.All(pair =>
pair.First.Key == pair.Second.Key &&
pair.First.Value.Equals(pair.Second.Value)
);
}
.Zip(that._properties).All(
pair =>
pair.First.Key == pair.Second.Key &&
pair.First.Value.Equals(pair.Second.Value));

return obj is NullType;
return obj is NullType or Any;
}

public override int GetHashCode() =>
Expand Down Expand Up @@ -86,11 +82,12 @@ public ObjectTypeHasher(ObjectType reference) =>
};

public int HashObjectType(ObjectType objectType) =>
objectType._properties.Keys.Select(key => HashCode.Combine(
key,
objectType[key].Equals(_reference)
? "@this".GetHashCode()
: objectType[key].GetType().GetHashCode()))
objectType._properties.Keys.Select(
key => HashCode.Combine(
key,
objectType[key].Equals(_reference)
? "@this".GetHashCode()
: objectType[key].GetType().GetHashCode()))
.Aggregate(36, HashCode.Combine);

private int HashArrayType(ArrayType arrayType) =>
Expand All @@ -108,11 +105,11 @@ private int HashFunctionType(FunctionType functionType) =>
functionType.ReturnType.Equals(_reference)
? "@this".GetHashCode()
: Hash(functionType.ReturnType),
functionType.Arguments.Select(arg =>
arg.Equals(_reference)
? "@this".GetHashCode()
: Hash(arg)
).Aggregate(36, HashCode.Combine));
functionType.Arguments.Select(
arg => arg.Equals(_reference)
? "@this".GetHashCode()
: Hash(arg))
.Aggregate(36, HashCode.Combine));
}

private class ObjectTypePrinter
Expand Down Expand Up @@ -142,9 +139,7 @@ public string PrintObjectType(ObjectType objectType)
if (_visited.Contains(objectType))
return string.Empty;
if (!objectType.Equals(_reference))
{
_visited.Add(objectType);
}

var sb = new StringBuilder("{");
foreach (var key in objectType._properties.Keys)
Expand Down Expand Up @@ -173,8 +168,7 @@ private string PrintArrayType(ArrayType arrayType)
var sb = new StringBuilder();
sb.Append(arrayType.Type.Equals(_reference)
? "@this"
: Print(arrayType.Type)
);
: Print(arrayType.Type));

return sb.Append("[]").ToString();
}
Expand All @@ -184,24 +178,25 @@ private string PrintNullableType(NullableType nullableType)
var sb = new StringBuilder();
sb.Append(nullableType.Type.Equals(_reference)
? "@this"
: Print(nullableType.Type)
);
: Print(nullableType.Type));

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

private string PrintFunctionType(FunctionType functionType)
{
var sb = new StringBuilder("(");
sb.AppendJoin(", ", functionType.Arguments.Select(
x => x.Equals(_reference)
sb.AppendJoin(
", ",
functionType.Arguments.Select(
x => x.Equals(_reference)
? "@this"
: Print(x)))
.Append(") => ");
sb.Append(
functionType.ReturnType.Equals(_reference)
? "@this"
: Print(x)
)).Append(") => ");
sb.Append(functionType.ReturnType.Equals(_reference)
? "@this"
: Print(functionType.ReturnType)
);
: Print(functionType.ReturnType));

return sb.ToString();
}
Expand Down
14 changes: 7 additions & 7 deletions Interpreter.Lib/IR/CheckSemantics/Types/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public virtual void ResolveReference(
{
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (obj == null || GetType() != obj.GetType()) return false;
var that = (Type) obj;
return Equals(_name, that._name);
}
public override bool Equals(object obj) =>
obj switch
{
Any => true,
Type that => _name == that._name,
_ => false
};

public override int GetHashCode() =>
_name.GetHashCode();
Expand Down
14 changes: 8 additions & 6 deletions Interpreter.Lib/IR/CheckSemantics/Visitors/SemanticChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public Type Visit(ImplicitLiteral visitable)

public Type Visit(ArrayLiteral visitable)
{
if (!visitable.Expressions.Any())
return "undefined";
if (visitable.Expressions.Count == 0)
return new ArrayType(new Any());

var type = visitable.First().Accept(this);
if (visitable.Expressions.All(e => e.Accept(this).Equals(type)))
Expand Down Expand Up @@ -200,11 +200,13 @@ public Type Visit(BinaryExpression visitable)
">" or ">=" or "<" or "<=" => lType.Equals(number)
? boolean
: throw new UnsupportedOperation(visitable.Segment, lType, visitable.Operator),
"++" => lType is ArrayType && rType is ArrayType
? lType
"++" when lType is ArrayType { Type: Any } && rType is ArrayType { Type: Any } =>
throw new CannotDefineType(visitable.Segment),
"++" => lType is ArrayType lArrType && rType is ArrayType rArrType
? new List<ArrayType> { lArrType, rArrType }.First(x => x.Type is not Any)
: throw new UnsupportedOperation(visitable.Segment, lType, visitable.Operator),
"::" when lType is not ArrayType => throw new UnsupportedOperation(visitable.Segment, lType,
visitable.Operator),
"::" when lType is not ArrayType =>
throw new UnsupportedOperation(visitable.Segment, lType, visitable.Operator),
"::" => rType.Equals(number) ? "void" : throw new ArrayAccessException(visitable.Segment, rType),
_ => "undefined"
};
Expand Down

0 comments on commit 87b3dfd

Please sign in to comment.