Skip to content

Commit d2d7311

Browse files
authored
#170 - ZLinq (#183)
* remove unused properties * remove Where from lexer * #170 - Structure is read-only list * #170 - zlinq
1 parent a8cbc3e commit d2d7311

File tree

26 files changed

+145
-152
lines changed

26 files changed

+145
-152
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageVersion Include="System.IO.Abstractions" Version="22.0.16" />
1515
<PackageVersion Include="Visitor.NET" Version="4.2.0" />
1616
<PackageVersion Include="Visitor.NET.AutoVisitableGen" Version="1.5.2" />
17+
<PackageVersion Include="ZLinq" Version="1.5.2" />
1718
<PackageVersion Include="ZLogger" Version="2.5.10" />
1819
<PackageVersion Include="ZString" Version="2.6.0" />
1920
</ItemGroup>

benchmarks/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Job=InProcess Toolchain=InProcessEmitToolchain
1010
```
1111
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
1212
|--------|---------:|----------:|----------:|---------:|---------:|----------:|
13-
| Invoke | 8.748 ms | 0.1720 ms | 0.2574 ms | 828.1250 | 343.7500 | 10.06 MB |
13+
| Invoke | 8.369 ms | 0.1315 ms | 0.1230 ms | 812.5000 | 218.7500 | 9.79 MB |

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.AccessExpressions;
1313
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.ComplexLiterals;
1414
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
15+
using ZLinq;
1516

1617
namespace HydraScript.Application.CodeGeneration.Visitors;
1718

@@ -75,9 +76,11 @@ public AddressedInstructions Visit(ObjectLiteral visitable)
7576

7677
var result = new AddressedInstructions { createObject };
7778

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

8285
return result;
8386
}
@@ -278,8 +281,9 @@ public AddressedInstructions Visit(CallExpression visitable)
278281
result.Add(new PushParameter(new Name(caller)));
279282
}
280283

281-
foreach (var expr in visitable.Parameters)
284+
for (var i = 0; i < visitable.Parameters.Count; i++)
282285
{
286+
var expr = visitable.Parameters[i];
283287
if (expr is PrimaryExpression primary)
284288
result.Add(new PushParameter(_valueDtoConverter.Convert(primary.ToValueDto())));
285289
else

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
1212
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Statements;
1313
using Microsoft.Extensions.DependencyInjection;
14+
using ZLinq;
1415

1516
namespace HydraScript.Application.CodeGeneration.Visitors;
1617

@@ -168,7 +169,8 @@ public AddressedInstructions Visit(WhileStatement visitable)
168169
}
169170

170171
result.AddRange(visitable.Statement.Accept(This));
171-
result.OfType<Goto>().Where(g => g.JumpType is not null)
172+
result.AsValueEnumerable()
173+
.OfType<Goto>().Where(g => g.JumpType is not null)
172174
.ToList().ForEach(g =>
173175
{
174176
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
@@ -220,7 +222,8 @@ public AddressedInstructions Visit(IfStatement visitable)
220222
if (visitable.HasElseBlock())
221223
result.AddRange(visitable.Else?.Accept(This) ?? []);
222224

223-
result.OfType<Goto>().Where(g => g.JumpType is InsideStatementJumpType.Break)
225+
result.AsValueEnumerable()
226+
.OfType<Goto>().Where(g => g.JumpType is InsideStatementJumpType.Break)
224227
.ToList().ForEach(g => g.SetJump(endBlockLabel));
225228

226229
result.Add(new EndBlock(BlockType.Condition, blockId), endBlockLabel.Name);

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ internal class FunctionWithUndefinedReturnStorage : IFunctionWithUndefinedReturn
88
{
99
private readonly OrderedDictionary<FunctionSymbolId, FunctionDeclaration> _declarations = [];
1010

11-
public void Save(FunctionSymbol symbol, FunctionDeclaration declaration)
12-
{
11+
public void Save(FunctionSymbol symbol, FunctionDeclaration declaration) =>
1312
_declarations[symbol.Id] = declaration;
14-
}
1513

1614
public FunctionDeclaration Get(FunctionSymbol symbol)
1715
{
@@ -21,10 +19,7 @@ public FunctionDeclaration Get(FunctionSymbol symbol)
2119
return declaration;
2220
}
2321

24-
public void RemoveIfPresent(FunctionSymbol symbol)
25-
{
26-
_declarations.Remove(symbol.Id);
27-
}
22+
public void RemoveIfPresent(FunctionSymbol symbol) => _declarations.Remove(symbol.Id);
2823

2924
public IEnumerable<FunctionDeclaration> Flush() => _declarations.Keys.ToList()
3025
.Select(x =>

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations;
22
using HydraScript.Domain.IR.Impl.Symbols;
3-
using HydraScript.Domain.IR.Impl.Symbols.Ids;
3+
using ZLinq;
44

55
namespace HydraScript.Application.StaticAnalysis.Impl;
66

@@ -17,26 +17,21 @@ public void Store(TypeDeclaration declaration) =>
1717
public void Resolve()
1818
{
1919
var defaults = provider.GetDefaultTypes()
20+
.AsValueEnumerable()
2021
.Select(x => new TypeSymbol(x))
2122
.ToList();
2223

23-
foreach (var declarationToResolve in _declarationsToResolve)
24-
{
25-
symbolTables[declarationToResolve.Scope].AddSymbol(
26-
new TypeSymbol(
27-
declarationToResolve.TypeValue.Accept(typeBuilder),
28-
declarationToResolve.TypeId));
29-
}
30-
3124
while (_declarationsToResolve.Count != 0)
3225
{
3326
var declarationToResolve = _declarationsToResolve.Dequeue();
34-
35-
var typeSymbol = symbolTables[declarationToResolve.Scope]
36-
.FindSymbol(new TypeSymbolId(declarationToResolve.TypeId))!;
27+
var typeSymbol = new TypeSymbol(
28+
declarationToResolve.TypeValue.Accept(typeBuilder),
29+
declarationToResolve.TypeId);
30+
symbolTables[declarationToResolve.Scope].AddSymbol(typeSymbol);
3731

3832
var resolvingCandidates = symbolTables[declarationToResolve.Scope]
3933
.GetAvailableSymbols()
34+
.AsValueEnumerable()
4035
.OfType<TypeSymbol>()
4136
.Except(defaults);
4237

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using HydraScript.Domain.IR.Impl.Symbols;
88
using HydraScript.Domain.IR.Impl.Symbols.Ids;
99
using HydraScript.Domain.IR.Types;
10+
using ZLinq;
1011

1112
namespace HydraScript.Application.StaticAnalysis.Visitors;
1213

@@ -71,14 +72,15 @@ public VisitUnit Visit(LexicalDeclaration visitable)
7172
public VisitUnit Visit(FunctionDeclaration visitable)
7273
{
7374
var parentTable = _symbolTables[visitable.Parent.Scope];
74-
var indexOfFirstDefaultArgument = visitable.Arguments
75+
var indexOfFirstDefaultArgument = visitable.Arguments.AsValueEnumerable()
7576
.Select((x, i) => new { Argument = x, Index = i })
7677
.FirstOrDefault(pair => pair.Argument is DefaultValueArgument)?.Index ?? -1;
7778

78-
var parameters = visitable.Arguments.Select(x =>
79-
new VariableSymbol(
80-
x.Name,
81-
x.TypeValue.Accept(_typeBuilder))).ToList();
79+
var parameters = visitable.Arguments.AsValueEnumerable()
80+
.Select(x =>
81+
new VariableSymbol(
82+
x.Name,
83+
x.TypeValue.Accept(_typeBuilder))).ToList();
8284
var functionSymbolId = new FunctionSymbolId(visitable.Name, parameters.Select(x => x.Type));
8385
_ambiguousInvocations.Clear(functionSymbolId);
8486
visitable.ComputedFunctionAddress = functionSymbolId.ToString();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using HydraScript.Domain.IR.Impl.Symbols;
1313
using HydraScript.Domain.IR.Impl.Symbols.Ids;
1414
using HydraScript.Domain.IR.Types;
15+
using ZLinq;
1516

1617
namespace HydraScript.Application.StaticAnalysis.Visitors;
1718

@@ -176,7 +177,7 @@ public Type Visit(ArrayLiteral visitable)
176177

177178
public Type Visit(ObjectLiteral visitable)
178179
{
179-
var properties = visitable.Properties.Select(prop =>
180+
var properties = visitable.Properties.AsValueEnumerable().Select(prop =>
180181
{
181182
var propType = prop.Expression.Accept(This);
182183

@@ -195,7 +196,9 @@ public Type Visit(ObjectLiteral visitable)
195196
propSymbol.Initialize();
196197
_symbolTables[visitable.Scope].AddSymbol(propSymbol);
197198
return new PropertyType(prop.Id, propType);
198-
});
199+
}).OrderBy(x => x.Id).ToDictionary(
200+
x => x.Id,
201+
x => x.Type);
199202
var objectLiteralType = new ObjectType(properties);
200203
return objectLiteralType;
201204
}
@@ -437,7 +440,8 @@ public Type Visit(CallExpression visitable)
437440
visitable.IsEmptyCall = functionSymbol.IsEmpty;
438441
var functionReturnType = functionSymbol.Type;
439442

440-
visitable.Parameters.Zip(parameters).Zip(functionSymbol.Parameters.ToArray()[(methodCall ? 1 : 0)..])
443+
visitable.Parameters.AsValueEnumerable()
444+
.Zip(parameters).Zip(functionSymbol.Parameters.AsValueEnumerable().Skip(methodCall ? 1 : 0))
441445
.ToList().ForEach(pair =>
442446
{
443447
var ((expr, actualType), expected) = pair;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations;
33
using HydraScript.Domain.IR.Impl.Symbols.Ids;
44
using HydraScript.Domain.IR.Types;
5+
using ZLinq;
56

67
namespace HydraScript.Application.StaticAnalysis.Visitors;
78

@@ -33,12 +34,13 @@ public NullableType Visit(NullableTypeValue visitable)
3334
}
3435

3536
public ObjectType Visit(ObjectTypeValue visitable) =>
36-
new(visitable.Properties
37+
new(visitable.Properties.AsValueEnumerable()
3738
.Select(x =>
3839
{
3940
x.TypeValue.Scope = visitable.Scope;
4041
return new PropertyType(
4142
Id: x.Key,
4243
x.TypeValue.Accept(This));
43-
}));
44+
}).OrderBy(x => x.Id)
45+
.ToDictionary(x => x.Id, x => x.Type));
4446
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ private void AddWithAddress(IExecutableInstruction instruction, IAddress newAddr
5252
_instructions.Add(newNode, instruction);
5353
}
5454

55-
public void AddRange(IEnumerable<IExecutableInstruction> instructions)
55+
public void AddRange(AddressedInstructions instructions)
5656
{
57-
foreach (var instruction in instructions)
58-
AddWithAddress(instruction, instruction.Address);
57+
// ReSharper disable once ConstantConditionalAccessQualifier
58+
for (var address = instructions.Start; address != null; address = address?.Next)
59+
{
60+
AddWithAddress(instructions[address], address);
61+
}
5962
}
6063

6164
public void Remove(IExecutableInstruction instruction)
@@ -75,11 +78,9 @@ public void Remove(IExecutableInstruction instruction)
7578
}
7679

7780
public IEnumerator<IExecutableInstruction> GetEnumerator() =>
78-
_addresses.Select(address => this[address])
79-
.GetEnumerator();
81+
_addresses.Select(address => this[address]).GetEnumerator();
8082

81-
IEnumerator IEnumerable.GetEnumerator() =>
82-
GetEnumerator();
83+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
8384

8485
public override string ToString() =>
8586
ZString.Join<IExecutableInstruction>('\n', this);

0 commit comments

Comments
 (0)