Skip to content

Commit 904bce6

Browse files
committed
#201 - refac IValueFactory.cs
1 parent 85d8c2f commit 904bce6

File tree

8 files changed

+32
-16
lines changed

8 files changed

+32
-16
lines changed

src/Application/HydraScript.Application.CodeGeneration/IValueFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ public interface IValueFactory
88
{
99
public IValue Create(ValueDto dto);
1010

11-
public Name CreateName(string id, bool env = false);
11+
public Name CreateName(IdentifierReference id);
12+
13+
public Name CreateName(string id);
1214
}

src/Application/HydraScript.Application.CodeGeneration/Impl/ValueFactory.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@ public IValue Create(ValueDto dto) =>
1515
{ Type: ValueDtoType.Constant, Label: not null } =>
1616
new Constant(dto.Value, dto.Label),
1717
{ Type: ValueDtoType.Name, Name: not null } =>
18-
CreateName(dto.Name),
18+
new Name(dto.Name, CurrentFrame),
1919
{ Type: ValueDtoType.Env, Name: not null } =>
20-
CreateName(dto.Name, env: true),
20+
new EnvName(dto.Name, EnvFrame),
2121
_ => throw new ArgumentOutOfRangeException(nameof(dto))
2222
};
2323

24-
public Name CreateName(string id, bool env = false) =>
25-
env ? new EnvName(id, EnvFrame) : new Name(id, CurrentFrame);
24+
public Name CreateName(IdentifierReference id)
25+
{
26+
var dto = id.ToValueDto();
27+
return dto switch
28+
{
29+
{ Type: ValueDtoType.Name, Name: not null } =>
30+
new Name(dto.Name, CurrentFrame),
31+
{ Type: ValueDtoType.Env, Name: not null } =>
32+
new EnvName(dto.Name, EnvFrame),
33+
_ => throw new ArgumentOutOfRangeException(nameof(dto))
34+
};
35+
}
36+
37+
public Name CreateName(string id) => new(id, CurrentFrame);
2638

2739
private CurrentFrame CurrentFrame { get; } = new(frameContext);
2840

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,7 @@ public AddressedInstructions Visit(AssignmentExpression visitable)
245245
}
246246

247247
if (visitable.Destination.Empty())
248-
{
249-
var isEnv = visitable.Destination.Id.ToValueDto().Type is ValueDtoType.Env;
250-
result.OfType<Simple>().Last().Left = _valueFactory.CreateName(visitable.Destination.Id, isEnv);
251-
}
248+
result.OfType<Simple>().Last().Left = _valueFactory.CreateName(visitable.Destination.Id);
252249
else
253250
{
254251
var last = result.OfType<Simple>().Last().Left!;

src/Domain/HydraScript.Domain.BackEnd/Impl/Values/Name.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public bool Equals(IValue? other) =>
1717
other is Name that &&
1818
Id == that.Id;
1919

20-
internal static IFrame NullFrameInstance { get; } = new NullFrame();
20+
internal static readonly IFrame NullFrameInstance = new NullFrame();
2121

2222
private sealed class NullFrame : IFrame
2323
{

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
2+
13
namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
24

35
[AutoVisitable<IAbstractSyntaxTreeNode>]
@@ -12,9 +14,9 @@ public partial class ArrayLiteral : ComplexLiteral
1214

1315
protected override string NullIdPrefix => "arr";
1416

15-
public override string Id => Parent is AssignmentExpression assignment
17+
public override IdentifierReference Id => new(Parent is AssignmentExpression assignment
1618
? assignment.Destination.Id
17-
: NullId;
19+
: NullId);
1820

1921
public ArrayLiteral(List<Expression> expressions)
2022
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Cysharp.Text;
2+
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
23

34
namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
45

@@ -15,5 +16,5 @@ protected string NullId
1516
}
1617
}
1718

18-
public abstract string Id { get; }
19+
public abstract IdentifierReference Id { get; }
1920
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
2+
13
namespace HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
24

35
[AutoVisitable<IAbstractSyntaxTreeNode>]
@@ -12,7 +14,7 @@ public partial class ObjectLiteral : ComplexLiteral
1214

1315
protected override string NullIdPrefix => "obj";
1416

15-
public override string Id
17+
public override IdentifierReference Id
1618
{
1719
get
1820
{
@@ -22,7 +24,7 @@ public override string Id
2224
if (Parent is WithExpression{Parent:AssignmentExpression withAssignment})
2325
return withAssignment.Destination.Id;
2426

25-
return NullId;
27+
return new(NullId);
2628
}
2729
}
2830

tests/HydraScript.UnitTests/Domain/FrontEnd/ComplexLiteralTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public void Id_DifferentType_StartsWithExpected(ComplexLiteral complexLiteral, s
99
{
1010
var complexLiteralId = complexLiteral.Id;
1111
testOutputHelper.WriteLine(complexLiteralId);
12-
complexLiteralId.Should().StartWith(expectedPrefix);
12+
complexLiteralId.Name.Should().StartWith(expectedPrefix);
1313
}
1414

1515
public static TheoryData<ComplexLiteral, string> StartsWithData =>

0 commit comments

Comments
 (0)