Skip to content

Commit b2992f6

Browse files
author
Минин Степан Александрович
committed
refactor(MemberExpression) - remove computed
1 parent db9886b commit b2992f6

10 files changed

Lines changed: 52 additions & 80 deletions

File tree

src/Application/HydraScript.Application.CodeGeneration/Visitors/ExpressionInstructionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public AddressedInstructions Visit(AssignmentExpression visitable)
270270
public AddressedInstructions Visit(MemberExpression visitable) =>
271271
visitable.Empty()
272272
? []
273-
: visitable.Tail?.Accept(This) ?? [];
273+
: visitable.AccessChain.Last?.Value.Accept(This) ?? [];
274274

275275
public AddressedInstructions Visit(DotAccess visitable)
276276
{

src/Application/HydraScript.Application.StaticAnalysis/IComputedTypesStorage.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Application/HydraScript.Application.StaticAnalysis/Impl/ComputedTypesStorage.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Application/HydraScript.Application.StaticAnalysis/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public static IServiceCollection AddStaticAnalysis(this IServiceCollection servi
1515
services.AddSingleton<IMethodStorage, MethodStorage>();
1616
services.AddSingleton<ISymbolTableStorage, SymbolTableStorage>();
1717

18-
services.AddSingleton<IComputedTypesStorage, ComputedTypesStorage>();
1918
services.AddSingleton<ITypeDeclarationsResolver, TypeDeclarationsResolver>();
2019

2120
services.AddSingleton<IStandardLibraryProvider, StandardLibraryProvider>();

src/Application/HydraScript.Application.StaticAnalysis/Visitors/SemanticChecker.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ internal class SemanticChecker : VisitorBase<IAbstractSyntaxTreeNode, Type>,
4949
private readonly IFunctionWithUndefinedReturnStorage _functionStorage;
5050
private readonly IMethodStorage _methodStorage;
5151
private readonly ISymbolTableStorage _symbolTables;
52-
private readonly IComputedTypesStorage _computedTypes;
5352
private readonly IAmbiguousInvocationStorage _ambiguousInvocations;
5453
private readonly IVisitor<TypeValue, Type> _typeBuilder;
5554

@@ -58,15 +57,13 @@ public SemanticChecker(
5857
IFunctionWithUndefinedReturnStorage functionStorage,
5958
IMethodStorage methodStorage,
6059
ISymbolTableStorage symbolTables,
61-
IComputedTypesStorage computedTypes,
6260
IAmbiguousInvocationStorage ambiguousInvocations,
6361
IVisitor<TypeValue, Type> typeBuilder)
6462
{
6563
_typesService = typesService;
6664
_functionStorage = functionStorage;
6765
_methodStorage = methodStorage;
6866
_symbolTables = symbolTables;
69-
_computedTypes = computedTypes;
7067
_ambiguousInvocations = ambiguousInvocations;
7168
_typeBuilder = typeBuilder;
7269
}
@@ -83,7 +80,6 @@ public Type Visit(ScriptBody visitable)
8380

8481
_methodStorage.Clear();
8582
_symbolTables.Clear();
86-
_computedTypes.Clear();
8783
_ambiguousInvocations.Clear();
8884

8985
return _typesService.Undefined;
@@ -341,17 +337,18 @@ public Type Visit(AssignmentExpression visitable)
341337
public Type Visit(MemberExpression visitable)
342338
{
343339
IAbstractSyntaxTreeNode id = visitable.Id;
344-
var idType = id.Accept(This);
345-
visitable.ComputedIdTypeGuid = _computedTypes.Save(idType);
346-
return visitable.Empty() ? idType : visitable.AccessChain?.Accept(This) ?? _typesService.Undefined;
340+
return visitable.Empty()
341+
? id.Accept(This)
342+
: visitable.AccessChain.Last?.Value.Accept(This) ?? _typesService.Undefined;
347343
}
348344

349345
public Type Visit(IndexAccess visitable)
350346
{
351-
var prevTypeGuid =
352-
visitable.Prev?.ComputedTypeGuid
353-
?? (visitable.Parent as MemberExpression)!.ComputedIdTypeGuid;
354-
var prevType = _computedTypes.Get(prevTypeGuid);
347+
var prevType =
348+
visitable.Prev?.Accept(This) ??
349+
(visitable.Parent is MemberExpression { Id: IAbstractSyntaxTreeNode id }
350+
? id.Accept(This)
351+
: _typesService.Undefined);
355352

356353
if (!prevType.TryGetOperator("[]", out var indexOperator))
357354
throw new NonAccessibleType(prevType);
@@ -361,16 +358,16 @@ public Type Visit(IndexAccess visitable)
361358
if (!indexOperator.TryGetResultType(indexAccessDescriptor, out var elemType))
362359
throw new ArrayAccessException(visitable.Segment, indexType);
363360

364-
visitable.ComputedTypeGuid = _computedTypes.Save(elemType);
365-
return visitable.HasNext() ? visitable.Next?.Accept(This) ?? _typesService.Undefined : elemType;
361+
return elemType;
366362
}
367363

368364
public Type Visit(DotAccess visitable)
369365
{
370-
var prevTypeGuid =
371-
visitable.Prev?.ComputedTypeGuid
372-
?? (visitable.Parent as MemberExpression)!.ComputedIdTypeGuid;
373-
var prevType = _computedTypes.Get(prevTypeGuid);
366+
var prevType =
367+
visitable.Prev?.Accept(This) ??
368+
(visitable.Parent is MemberExpression { Id: IAbstractSyntaxTreeNode id }
369+
? id.Accept(This)
370+
: _typesService.Undefined);
374371

375372
if (prevType is not ObjectType objectType)
376373
throw new NonAccessibleType(prevType);
@@ -381,8 +378,8 @@ public Type Visit(DotAccess visitable)
381378
return hasMethod
382379
? objectType
383380
: throw new ObjectAccessException(visitable.Segment, objectType, visitable.Property);
384-
visitable.ComputedTypeGuid = _computedTypes.Save(fieldType);
385-
return visitable.HasNext() ? visitable.Next?.Accept(This) ?? _typesService.Undefined : fieldType;
381+
382+
return fieldType;
386383
}
387384

388385
public ObjectType Visit(WithExpression visitable)

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/AccessExpressions/AccessExpression.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.AccessEx
22

33
public abstract class AccessExpression : Expression
44
{
5-
public AccessExpression? Next { get; private set; }
5+
protected AccessExpression? Next { get; private set; }
66

7-
public AccessExpression? Prev =>
8-
Parent as AccessExpression;
9-
10-
public Guid ComputedTypeGuid { get; set; } = Guid.Empty;
7+
public AccessExpression? Prev => Parent as AccessExpression;
118

129
protected AccessExpression(AccessExpression? prev)
1310
{
@@ -18,12 +15,10 @@ protected AccessExpression(AccessExpression? prev)
1815
}
1916
}
2017

21-
public bool HasNext() =>
22-
Next is not null;
23-
24-
public bool HasPrev() =>
25-
Prev is not null;
18+
public bool HasPrev() => Prev is not null;
2619

2720
public abstract override TReturn Accept<TReturn>(
2821
IVisitor<IAbstractSyntaxTreeNode, TReturn> visitor);
22+
23+
public abstract override AccessExpression Clone();
2924
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/AccessExpressions/DotAccess.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.AccessEx
66
public partial class DotAccess : AccessExpression
77
{
88
protected override IReadOnlyList<IAbstractSyntaxTreeNode> Children =>
9-
HasNext() ? [Property, Next!] : [Property];
9+
Next is { } next ? [Property, next] : [Property];
1010

1111
public IdentifierReference Property { get; }
1212

@@ -17,4 +17,6 @@ public DotAccess(IdentifierReference property, AccessExpression? prev = null) :
1717
}
1818

1919
protected override string NodeRepresentation() => ".";
20+
21+
public override DotAccess Clone() => new(Property.Clone(), Prev?.Clone());
2022
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/AccessExpressions/IndexAccess.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.AccessEx
44
public partial class IndexAccess : AccessExpression
55
{
66
protected override IReadOnlyList<IAbstractSyntaxTreeNode> Children =>
7-
HasNext() ? [Index, Next!] : [Index];
7+
Next is { } next ? [Index, next] : [Index];
88

99
public Expression Index { get; }
1010

@@ -15,4 +15,6 @@ public IndexAccess(Expression index, AccessExpression? prev = null) : base(prev)
1515
}
1616

1717
protected override string NodeRepresentation() => "[]";
18+
19+
public override IndexAccess Clone() => new(Index.Clone(), Prev?.Clone());
1820
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/MemberExpression.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,41 @@ public partial class MemberExpression : LeftHandSideExpression
99
private readonly IdentifierReference _identifierReference;
1010

1111
protected override IReadOnlyList<IAbstractSyntaxTreeNode> Children =>
12-
AccessChain is not null ? [Id, AccessChain] : [Id];
12+
AccessChain.First is { Value: { } head } ? [Id, head] : [Id];
1313

14-
public AccessExpression? AccessChain { get; }
15-
public AccessExpression? Tail { get; }
14+
public LinkedList<AccessExpression> AccessChain { get; }
1615

17-
public Guid ComputedIdTypeGuid { get; set; } = Guid.Empty;
18-
19-
public MemberExpression(IdentifierReference identifierReference)
16+
public MemberExpression(IdentifierReference identifierReference) :
17+
this(identifierReference, [])
2018
{
21-
_identifierReference = identifierReference;
22-
_identifierReference.Parent = this;
2319
}
2420

2521
public MemberExpression(
2622
IdentifierReference identifierReference,
27-
AccessExpression? accessChain,
28-
AccessExpression? tail) : this(identifierReference)
23+
LinkedList<AccessExpression> accessChain)
2924
{
30-
AccessChain = accessChain;
31-
AccessChain?.Parent = this;
25+
_identifierReference = identifierReference;
26+
_identifierReference.Parent = this;
3227

33-
Tail = tail;
28+
AccessChain = accessChain;
29+
AccessChain.First?.Value.Parent = this;
3430
}
3531

3632
public override IdentifierReference Id => _identifierReference;
3733

3834
public bool Empty() => AccessChain.Count == 0;
3935

4036
protected override string NodeRepresentation() => nameof(MemberExpression);
37+
38+
public override MemberExpression Clone()
39+
{
40+
var clonedAccessChain = new LinkedList<AccessExpression>();
41+
var clonedTail = AccessChain.Last?.Value.Clone();
42+
while (clonedTail != null)
43+
{
44+
clonedAccessChain.AddFirst(clonedTail);
45+
clonedTail = clonedTail.Prev;
46+
}
47+
return new MemberExpression(Id.Clone(), clonedAccessChain);
48+
}
4149
}

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/TopDownParser.Expression.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ private MemberExpression MemberExpression()
8383
}
8484
}
8585

86-
return new MemberExpression(
87-
memberRoot,
88-
accessChain.First?.Value,
89-
tail: accessChain.Last?.Value)
86+
return new MemberExpression(memberRoot, accessChain)
9087
{
9188
Segment = memberRoot.Segment
9289
};
@@ -208,8 +205,8 @@ private Expression EqualityExpression()
208205
private Expression RelationExpression()
209206
{
210207
var left = AdditiveExpression();
211-
while (CurrentIsOperator(">") || CurrentIsOperator("<") || CurrentIsOperator(">=") ||
212-
CurrentIsOperator("<="))
208+
while (CurrentIsOperator(">") || CurrentIsOperator("<") ||
209+
CurrentIsOperator(">=") || CurrentIsOperator("<="))
213210
{
214211
var op = Expect("Operator");
215212
var right = AdditiveExpression();

0 commit comments

Comments
 (0)