Skip to content

Commit d179f50

Browse files
committed
#16 - add codegen visitor implementation for 'with' expressions and remove dead code in DotRead
1 parent 3a2c2e6 commit d179f50

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal class ExpressionInstructionProvider : VisitorBase<IAbstractSyntaxTreeNo
2424
IVisitor<UnaryExpression, AddressedInstructions>,
2525
IVisitor<BinaryExpression, AddressedInstructions>,
2626
IVisitor<CastAsExpression, AddressedInstructions>,
27+
IVisitor<WithExpression, AddressedInstructions>,
2728
IVisitor<ConditionalExpression, AddressedInstructions>,
2829
IVisitor<AssignmentExpression, AddressedInstructions>,
2930
IVisitor<MemberExpression, AddressedInstructions>,
@@ -36,6 +37,8 @@ internal class ExpressionInstructionProvider : VisitorBase<IAbstractSyntaxTreeNo
3637
public ExpressionInstructionProvider(IValueDtoConverter valueDtoConverter) =>
3738
_valueDtoConverter = valueDtoConverter;
3839

40+
public override AddressedInstructions Visit(IAbstractSyntaxTreeNode visitable) => [];
41+
3942
public AddressedInstructions Visit(PrimaryExpression visitable) =>
4043
[new Simple(_valueDtoConverter.Convert(visitable.ToValueDto()))];
4144

@@ -76,9 +79,8 @@ public AddressedInstructions Visit(ObjectLiteral visitable)
7679

7780
var result = new AddressedInstructions { createObject };
7881

79-
var propInstructions =
80-
visitable.Properties.AsValueEnumerable()
81-
.SelectMany(property => property.Accept(This));
82+
var propInstructions = visitable.AsValueEnumerable()
83+
.SelectMany(property => property.Accept(This));
8284
foreach (var propInstruction in propInstructions)
8385
result.Add(propInstruction);
8486

@@ -158,6 +160,42 @@ public AddressedInstructions Visit(CastAsExpression visitable)
158160
return result;
159161
}
160162

163+
public AddressedInstructions Visit(WithExpression visitable)
164+
{
165+
if (visitable is { Expression: PrimaryExpression, ComputedCopiedProperties.Count: 0 })
166+
return [];
167+
168+
var objectId = visitable.ObjectLiteral.Id;
169+
var createObject = new CreateObject(objectId);
170+
171+
var result = new AddressedInstructions { createObject };
172+
173+
var propInstructions = visitable.ObjectLiteral
174+
.AsValueEnumerable()
175+
.SelectMany(property => property.Accept(This));
176+
foreach (var propInstruction in propInstructions)
177+
result.Add(propInstruction);
178+
179+
if (visitable.ComputedCopiedProperties.Count is 0)
180+
return result;
181+
182+
result.AddRange(visitable.Expression is PrimaryExpression ? [] : visitable.Expression.Accept(This));
183+
184+
var copyFrom = visitable.Expression is PrimaryExpression primary
185+
? (Name)_valueDtoConverter.Convert(primary.ToValueDto())
186+
: new Name(result.OfType<Simple>().Last().Left!);
187+
188+
for (var i = 0; i < visitable.ComputedCopiedProperties.Count; i++)
189+
{
190+
var property = new Constant(visitable.ComputedCopiedProperties[i]);
191+
result.Add(new DotRead(copyFrom, property));
192+
var read = result[result.End].Address.Name;
193+
result.Add(new DotAssignment(objectId, property, new Name(read)));
194+
}
195+
196+
return result;
197+
}
198+
161199
public AddressedInstructions Visit(ConditionalExpression visitable)
162200
{
163201
var blockId = $"cond_{visitable.GetHashCode()}";

src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/ComplexData/Read/DotRead.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public class DotRead(Name @object, IValue property) : Simple(
1010
{
1111
private readonly IValue _property = property;
1212

13-
public string Property => (string)_property.Get(frame: null)!;
14-
1513
public Simple ToAssignment(IValue value) =>
1614
new DotAssignment(@object.ToString(), _property, value);
1715

0 commit comments

Comments
 (0)