Skip to content

Commit 5591496

Browse files
committed
#165 - refactoring
1 parent 9a989a9 commit 5591496

File tree

5 files changed

+12
-23
lines changed

5 files changed

+12
-23
lines changed
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.IO.Abstractions;
32
using HydraScript.Domain.FrontEnd.Lexer;
43
using Microsoft.Extensions.DependencyInjection;
5-
using Microsoft.Extensions.Options;
64

75
namespace HydraScript.Infrastructure.Dumping;
86

97
internal class DumpingLexer(
108
[FromKeyedServices(DecoratorKey.Value)]
119
ILexer lexer,
12-
IFileSystem fileSystem,
13-
IOptions<FileInfo> inputFile) : ILexer
10+
IDumpingService dumpingService) : ILexer
1411
{
1512
[ExcludeFromCodeCoverage]
1613
public IStructure Structure => lexer.Structure;
1714

1815
public List<Token> GetTokens(string text)
1916
{
2017
var tokens = lexer.GetTokens(text);
21-
var fileName = inputFile.Value.Name.Split(".js")[0];
22-
fileSystem.File.WriteAllText(
23-
$"{fileName}.tokens",
24-
lexer.ToString());
18+
dumpingService.Dump(lexer.ToString(), "tokens");
2519
return tokens;
2620
}
2721
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
using System.IO.Abstractions;
21
using HydraScript.Domain.FrontEnd.Parser;
32
using Microsoft.Extensions.DependencyInjection;
43

54
namespace HydraScript.Infrastructure.Dumping;
65

76
internal class DumpingParser(
87
[FromKeyedServices(DecoratorKey.Value)] IParser parser,
9-
IFileSystem fileSystem) : IParser
8+
IDumpingService dumpingService) : IParser
109
{
1110
public IAbstractSyntaxTree Parse(string text)
1211
{
1312
var ast = parser.Parse(text);
14-
fileSystem.File.WriteAllText("ast.dot", ast.ToString());
13+
dumpingService.Dump(ast.ToString(), "dot");
1514
return ast;
1615
}
1716
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
using System.IO.Abstractions;
21
using HydraScript.Domain.BackEnd;
32
using Microsoft.Extensions.DependencyInjection;
4-
using Microsoft.Extensions.Options;
53

64
namespace HydraScript.Infrastructure.Dumping;
75

86
internal class DumpingVirtualMachine(
97
[FromKeyedServices(DecoratorKey.Value)]
108
IVirtualMachine virtualMachine,
11-
IFileSystem fileSystem,
12-
IOptions<FileInfo> inputFile) : IVirtualMachine
9+
IDumpingService dumpingService) : IVirtualMachine
1310
{
1411
public IExecuteParams ExecuteParams => virtualMachine.ExecuteParams;
1512

1613
public void Run(AddressedInstructions instructions)
1714
{
18-
var fileName = inputFile.Value.Name.Split(".js")[0];
19-
fileSystem.File.WriteAllText($"{fileName}.tac", instructions.ToString());
20-
15+
dumpingService.Dump(instructions.ToString(), "tac");
2116
virtualMachine.Run(instructions);
2217
}
2318
}

src/Infrastructure/HydraScript.Infrastructure/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ public static IServiceCollection AddApplication(this IServiceCollection services
3737
public static IServiceCollection AddInfrastructure(
3838
this IServiceCollection services,
3939
bool dump,
40-
FileInfo inputFileInfo)
40+
FileInfo fileInfo)
4141
{
4242
services.AddSingleton<IFileSystem, FileSystem>();
43-
services.AddSingleton(Options.Create(inputFileInfo));
43+
services.AddSingleton(Options.Create(fileInfo));
4444

4545
services.AddSingleton<IStaticAnalyzer, StaticAnalyzer>();
4646
services.AddSingleton<ICodeGenerator, CodeGenerator>();
4747

4848
services.AddSingleton<ISourceCodeProvider, SourceCodeProvider>();
49+
services.AddSingleton<IDumpingService, DumpingService>();
4950

5051
if (dump)
5152
{

tests/HydraScript.IntegrationTests/DumpOptionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public void Invoke_DumpOptionPassed_FilesCreated()
2424
runner.Invoke();
2525
fileSystemMock.File.Received(1)
2626
.WriteAllText(
27-
TestHostFixture.ScriptFileName + ".tokens",
27+
Arg.Is<string>(s => s.EndsWith(TestHostFixture.ScriptFileName + ".tokens")),
2828
Arg.Any<string>());
2929
fileSystemMock.File.Received(1)
3030
.WriteAllText(
31-
"ast.dot",
31+
Arg.Is<string>(s => s.EndsWith(TestHostFixture.ScriptFileName + ".dot")),
3232
Arg.Any<string>());
3333
fileSystemMock.File.Received(1)
3434
.WriteAllText(
35-
TestHostFixture.ScriptFileName + ".tac",
35+
Arg.Is<string>(s => s.EndsWith(TestHostFixture.ScriptFileName + ".tac")),
3636
Arg.Any<string>());
3737
}
3838
}

0 commit comments

Comments
 (0)