Skip to content

Commit 31b0da6

Browse files
authored
Refactoring/frame (#215)
* remove return address from Frame.cs * use concrete type * Left string -> Name * update pull_request_template.md
1 parent 619abd7 commit 31b0da6

File tree

25 files changed

+165
-161
lines changed

25 files changed

+165
-161
lines changed

.github/pull_request_template.md

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
### Description
2-
<!--- Describe your changes in detail -->
3-
<!--- Why is this change required? What problem does it solve? -->
1+
**Is your pull request related to a problem? Please describe.**
2+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
43

4+
**Describe the solution you've done**
5+
Key points of your tech and arch decisions.
56

6-
### Related Issues
7-
<!--- If it fixes an open issue, please link to the issue here. -->
8-
9-
10-
### References
11-
<!--- References would be helpful to understand the changes. -->
12-
<!--- References can be books, links, etc. -->
13-
14-
15-
### Checklist:
16-
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
17-
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
18-
- [ ] I have followed the [contribution guidelines](../CONTRIBUTING.md) and code style for this project.
19-
- [ ] I have added tests covering my contributions.
20-
- [ ] I have updated the documentation accordingly.
21-
- [ ] I have added corresponding labels with respect to the part of the interpreter i've worked on.
7+
**Related Issues**
8+
If it fixes an open issue, please link to the issue here.

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public AddressedInstructions Visit(ArrayLiteral visitable)
4646
{
4747
var arraySize = visitable.Expressions.Count;
4848

49-
var arrayName = visitable.Id;
49+
var arrayName = new Name(visitable.Id);
5050
var createArray = new CreateArray(arrayName, arraySize);
5151

5252
var result = new AddressedInstructions { createArray };
@@ -64,7 +64,7 @@ public AddressedInstructions Visit(ArrayLiteral visitable)
6464
else
6565
{
6666
result.AddRange(expression.Accept(This));
67-
var last = new Name(result.OfType<Simple>().Last().Left!);
67+
var last = result.OfType<Simple>().Last().Left!;
6868
result.Add(new IndexAssignment(arrayName, index, last));
6969
}
7070
}
@@ -75,7 +75,7 @@ public AddressedInstructions Visit(ArrayLiteral visitable)
7575
public AddressedInstructions Visit(ObjectLiteral visitable)
7676
{
7777
var objectId = visitable.Id;
78-
var createObject = new CreateObject(objectId);
78+
var createObject = new CreateObject(new Name(objectId));
7979

8080
var result = new AddressedInstructions { createObject };
8181

@@ -89,7 +89,7 @@ public AddressedInstructions Visit(ObjectLiteral visitable)
8989

9090
public AddressedInstructions Visit(Property visitable)
9191
{
92-
var objectId = visitable.Object.Id;
92+
var objectId = new Name(visitable.Object.Id);
9393

9494
var (id, expression) = visitable;
9595
var propertyId = new Constant(id);
@@ -101,7 +101,7 @@ public AddressedInstructions Visit(Property visitable)
101101
_valueDtoConverter.Convert(primary.ToValueDto()))];
102102

103103
var instructions = expression.Accept(This);
104-
var last = new Name(instructions.OfType<Simple>().Last().Left!);
104+
var last = instructions.OfType<Simple>().Last().Left!;
105105
instructions.Add(new DotAssignment(objectId, propertyId, last));
106106

107107
return instructions;
@@ -113,7 +113,7 @@ public AddressedInstructions Visit(UnaryExpression visitable)
113113
return [new Simple(visitable.Operator, _valueDtoConverter.Convert(primary.ToValueDto()))];
114114

115115
var result = visitable.Expression.Accept(This);
116-
var last = new Name(result.OfType<Simple>().Last().Left!);
116+
var last = result.OfType<Simple>().Last().Left!;
117117
result.Add(new Simple(visitable.Operator, last));
118118

119119
return result;
@@ -122,7 +122,7 @@ public AddressedInstructions Visit(UnaryExpression visitable)
122122
public AddressedInstructions Visit(BinaryExpression visitable)
123123
{
124124
if (visitable is { Left: IdentifierReference arr, Right: PrimaryExpression primary, Operator: "::" })
125-
return [new RemoveFromArray(arr.Name, index: _valueDtoConverter.Convert(primary.ToValueDto()))];
125+
return [new RemoveFromArray(new Name(arr), index: _valueDtoConverter.Convert(primary.ToValueDto()))];
126126

127127
var result = new AddressedInstructions();
128128
IValue left, right;
@@ -132,15 +132,15 @@ public AddressedInstructions Visit(BinaryExpression visitable)
132132
else
133133
{
134134
result.AddRange(visitable.Left.Accept(This));
135-
left = new Name(result.OfType<Simple>().Last().Left!);
135+
left = result.OfType<Simple>().Last().Left!;
136136
}
137137

138138
if (visitable.Right is PrimaryExpression primaryRight)
139139
right = _valueDtoConverter.Convert(primaryRight.ToValueDto());
140140
else
141141
{
142142
result.AddRange(visitable.Right.Accept(This));
143-
right = new Name(result.OfType<Simple>().Last().Left!);
143+
right = result.OfType<Simple>().Last().Left!;
144144
}
145145

146146
result.Add(new Simple(left, visitable.Operator, right));
@@ -154,15 +154,15 @@ public AddressedInstructions Visit(CastAsExpression visitable)
154154
return [new AsString(_valueDtoConverter.Convert(primary.ToValueDto()))];
155155

156156
var result = visitable.Expression.Accept(This);
157-
var last = new Name(result.OfType<Simple>().Last().Left!);
157+
var last = result.OfType<Simple>().Last().Left!;
158158
result.Add(new AsString(last));
159159

160160
return result;
161161
}
162162

163163
public AddressedInstructions Visit(WithExpression visitable)
164164
{
165-
var objectId = visitable.ObjectLiteral.Id;
165+
var objectId = new Name(visitable.ObjectLiteral.Id);
166166
var createObject = new CreateObject(objectId);
167167

168168
var result = new AddressedInstructions { createObject };
@@ -188,7 +188,7 @@ public AddressedInstructions Visit(WithExpression visitable)
188188

189189
var copyFrom = visitable.Expression is IdentifierReference objectIdent
190190
? new Name(objectIdent)
191-
: new Name(result.OfType<Simple>().Last().Left!);
191+
: result.OfType<Simple>().Last().Left!;
192192

193193
for (var i = 0; i < visitable.ComputedCopiedProperties.Count; i++)
194194
{
@@ -214,7 +214,7 @@ public AddressedInstructions Visit(ConditionalExpression visitable)
214214
else
215215
{
216216
result.AddRange(visitable.Test.Accept(This));
217-
var last = new Name(result.OfType<Simple>().Last().Left!);
217+
var last = result.OfType<Simple>().Last().Left!;
218218
result.Add(new IfNotGoto(last, startBlockLabel));
219219
}
220220

@@ -227,7 +227,7 @@ public AddressedInstructions Visit(ConditionalExpression visitable)
227227
result.OfType<Simple>().Last().Left = temp;
228228
result.Add(new EndBlock(BlockType.Condition, blockId), endBlockLabel.Name);
229229

230-
result.Add(new Simple(new Name(temp)));
230+
result.Add(new Simple(temp));
231231

232232
return result;
233233
}
@@ -241,14 +241,14 @@ public AddressedInstructions Visit(AssignmentExpression visitable)
241241
if (last is IWriteToComplexData assignment)
242242
result.Add(assignment.ToSimple());
243243
else
244-
result.Add(new Simple(new Name(last.Left!)));
244+
result.Add(new Simple(last.Left!));
245245
}
246246

247247
if (visitable.Destination.Empty())
248-
result.OfType<Simple>().Last().Left = visitable.Destination.Id;
248+
result.OfType<Simple>().Last().Left = new Name(visitable.Destination.Id);
249249
else
250250
{
251-
var last = new Name(result.OfType<Simple>().Last().Left!);
251+
var last = result.OfType<Simple>().Last().Left!;
252252
result.AddRange(visitable.Destination.Accept(This));
253253
var lastRead = result.OfType<IReadFromComplexData>().Last();
254254
result.Replace(lastRead.ToInstruction(), lastRead.ToAssignment(last));
@@ -270,7 +270,7 @@ public AddressedInstructions Visit(DotAccess visitable)
270270
return [new DotRead(new Name(lhs.Id), right)];
271271

272272
var result = visitable.Prev?.Accept(This) ?? [];
273-
var left = new Name(result.OfType<Simple>().Last().Left!);
273+
var left = result.OfType<Simple>().Last().Left!;
274274
result.Add(new DotRead(left, right));
275275

276276
return result;
@@ -287,15 +287,15 @@ public AddressedInstructions Visit(IndexAccess visitable)
287287
else
288288
{
289289
result.AddRange(visitable.Index.Accept(This));
290-
right = new Name(result.OfType<Simple>().Last().Left!);
290+
right = result.OfType<Simple>().Last().Left!;
291291
}
292292

293293
if (!visitable.HasPrev() && visitable.Parent is LeftHandSideExpression lhs)
294294
result.Add(new IndexRead(new Name(lhs.Id), right));
295295
else
296296
{
297297
result.AddRange(visitable.Prev?.Accept(This) ?? []);
298-
var left = new Name(result.OfType<Simple>().Last().Left!);
298+
var left = result.OfType<Simple>().Last().Left!;
299299
result.Add(new IndexRead(left, right));
300300
}
301301

@@ -320,8 +320,8 @@ public AddressedInstructions Visit(CallExpression visitable)
320320

321321
if (methodCall)
322322
{
323-
var caller = result.Any() ? result.OfType<Simple>().Last().Left! : visitable.Id;
324-
result.Add(new PushParameter(new Name(caller)));
323+
var caller = result.Count > 0 ? result.OfType<Simple>().Last().Left! : new Name(visitable.Id);
324+
result.Add(new PushParameter(caller));
325325
}
326326

327327
for (var i = 0; i < visitable.Parameters.Count; i++)
@@ -333,7 +333,7 @@ public AddressedInstructions Visit(CallExpression visitable)
333333
{
334334
result.AddRange(expr.Accept(This));
335335
var id = result.OfType<Simple>().Last().Left!;
336-
result.Add(new PushParameter(new Name(id)));
336+
result.Add(new PushParameter(id));
337337
}
338338
}
339339

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ public AddressedInstructions Visit(ReturnStatement visitable)
107107
}
108108

109109
var result = visitable.Expression.Accept(_expressionVisitor);
110-
var last = new Name(result.OfType<Simple>().Last().Left!);
110+
var last = result.OfType<Simple>().Last().Left!;
111111
result.Add(new Return(last));
112112

113113
return result;
114114
}
115115

116116
public AddressedInstructions Visit(FunctionDeclaration visitable)
117117
{
118-
if (!visitable.Statements.Any())
118+
if (visitable.IsEmpty)
119119
return [];
120120

121121
var functionInfo = new FunctionInfo(visitable.ComputedFunctionAddress);
@@ -132,7 +132,7 @@ public AddressedInstructions Visit(FunctionDeclaration visitable)
132132
for (var i = 0; i < visitable.Arguments.Count; i++)
133133
{
134134
var arg = visitable.Arguments[i];
135-
result.Add(new PopParameter(arg.Name, arg.Info.Value));
135+
result.Add(new PopParameter(new Name(arg.Name), arg.Info.Value));
136136
}
137137

138138
result.AddRange(visitable.Statements.Accept(This));
@@ -160,7 +160,7 @@ public AddressedInstructions Visit(WhileStatement visitable)
160160
else
161161
{
162162
result.AddRange(visitable.Condition.Accept(_expressionVisitor));
163-
var last = new Name(result.OfType<Simple>().Last().Left!);
163+
var last = result.OfType<Simple>().Last().Left!;
164164
result.Add(new IfNotGoto(last, endBlockLabel));
165165
}
166166

@@ -204,7 +204,7 @@ public AddressedInstructions Visit(IfStatement visitable)
204204
else
205205
{
206206
result.AddRange(visitable.Test.Accept(_expressionVisitor));
207-
var last = new Name(result.OfType<Simple>().Last().Left!);
207+
var last = result.OfType<Simple>().Last().Left!;
208208
result.Add(new IfNotGoto(last,
209209
visitable.HasElseBlock()
210210
? startBlockLabel
@@ -239,11 +239,11 @@ public AddressedInstructions Visit(PrintStatement visitable)
239239
else
240240
{
241241
result.AddRange(visitable.Expression.Accept(_expressionVisitor));
242-
var name = new Name(result.OfType<Simple>().Last().Left!);
242+
var name = result.OfType<Simple>().Last().Left!;
243243
result.Add(new AsString(name));
244244
}
245245

246-
result.Add(new Print(new Name((result[result.End] as AsString)!.Left!)));
246+
result.Add(new Print((result[result.End] as AsString)!.Left!));
247247

248248
return result;
249249
}

src/Domain/HydraScript.Domain.BackEnd/AddressedInstructions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace HydraScript.Domain.BackEnd;
66

7-
public class AddressedInstructions : IEnumerable<IExecutableInstruction>
7+
public class AddressedInstructions : IReadOnlyCollection<IExecutableInstruction>
88
{
99
private readonly LinkedList<IAddress> _addresses = new();
1010
private readonly Dictionary<IAddress, LinkedListNode<IAddress>> _addressToNode = new();
@@ -85,6 +85,8 @@ public IEnumerator<IExecutableInstruction> GetEnumerator() =>
8585

8686
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
8787

88+
public int Count => _addresses.Count;
89+
8890
public override string ToString() =>
8991
ZString.Join<IExecutableInstruction>('\n', this);
9092
}

src/Domain/HydraScript.Domain.BackEnd/Call.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using HydraScript.Domain.BackEnd.Impl.Addresses;
2+
using HydraScript.Domain.BackEnd.Impl.Values;
23

34
namespace HydraScript.Domain.BackEnd;
45

56
public record Call(
67
IAddress From,
78
FunctionInfo To,
8-
string? Where = null)
9+
Name? Where = null)
910
{
1011
public override string ToString() =>
1112
$"{From}: {Where} => {To.Start}: {To.Id}";

src/Domain/HydraScript.Domain.BackEnd/Frame.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
namespace HydraScript.Domain.BackEnd;
22

3-
public class Frame(IAddress returnAddress, Frame? parentFrame = null)
3+
public class Frame(Frame? parentFrame = null)
44
{
55
private readonly Dictionary<string, object?> _variables = new();
66

7-
public IAddress ReturnAddress { get; } = returnAddress;
8-
97
public object? this[string id]
108
{
119
get => _variables.TryGetValue(id, out var value)

src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/PopParameter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
using HydraScript.Domain.BackEnd.Impl.Values;
2+
13
namespace HydraScript.Domain.BackEnd.Impl.Instructions;
24

3-
public class PopParameter(string parameter, object? defaultValue) : Instruction
5+
public class PopParameter(Name parameter, object? defaultValue) : Instruction
46
{
57
public override IAddress? Execute(IExecuteParams executeParams)
68
{
79
var frame = executeParams.Frames.Peek();
8-
if (executeParams.Arguments.TryDequeue(out var argument))
9-
frame[parameter] = argument;
10-
else
11-
frame[parameter] = defaultValue;
10+
parameter.Set(
11+
frame,
12+
executeParams.Arguments.TryDequeue(out var argument)
13+
? argument
14+
: defaultValue);
1215
return Address.Next;
1316
}
1417

src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/RemoveFromArray.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using HydraScript.Domain.BackEnd.Impl.Values;
2+
13
namespace HydraScript.Domain.BackEnd.Impl.Instructions;
24

3-
public class RemoveFromArray(string id, IValue index) : Instruction
5+
public class RemoveFromArray(Name id, IValue index) : Instruction
46
{
57
public override IAddress? Execute(IExecuteParams executeParams)
68
{
79
var frame = executeParams.Frames.Peek();
8-
if (frame[id] is List<object> list)
10+
if (id.Get(frame) is List<object> list)
911
{
1012
list.RemoveAt(Convert.ToInt32(index.Get(frame)));
1113
}

src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/Return.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ namespace HydraScript.Domain.BackEnd.Impl.Instructions;
22

33
public class Return(IValue? value = null) : Instruction
44
{
5-
public override IAddress Execute(IExecuteParams executeParams)
5+
public override IAddress? Execute(IExecuteParams executeParams)
66
{
7-
var frame = executeParams.Frames.Pop();
7+
var callFrame = executeParams.Frames.Pop();
88
var call = executeParams.CallStack.Pop();
99
if (call.Where != null && value != null)
1010
{
11-
executeParams.Frames.Peek()[call.Where] = value.Get(frame);
11+
var frame = executeParams.Frames.Peek();
12+
call.Where?.Set(frame, value.Get(callFrame));
1213
}
1314

14-
return frame.ReturnAddress;
15+
return call.From.Next;
1516
}
1617

1718
protected override string ToStringInternal() =>

src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/AsString.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ public partial class AsString(IValue value) : Simple(value)
1010
{
1111
var frame = executeParams.Frames.Peek();
1212
var value = Right.right!.Get(frame);
13-
frame[Left!] = value is string
13+
var valueAsString = value is string
1414
? value
1515
: JsonSerializer.Serialize(
1616
value: Right.right!.Get(frame)!,
1717
AsStringSerializationContext.Default.Object);
18+
Left?.Set(frame, valueAsString);
1819

1920
return Address.Next;
2021
}

0 commit comments

Comments
 (0)