Skip to content

Commit

Permalink
#42 - внедрил system command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Jul 27, 2024
1 parent 63ecf14 commit 67eeece
Show file tree
Hide file tree
Showing 28 changed files with 108 additions and 583 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HydraScript.Application.StaticAnalysis.Impl;
using HydraScript.Application.StaticAnalysis.Visitors;
using HydraScript.Domain.FrontEnd.Parser;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations;
using Microsoft.Extensions.DependencyInjection;

namespace HydraScript.Application.StaticAnalysis;
Expand All @@ -20,9 +21,11 @@ public static IServiceCollection AddStaticAnalysis(this IServiceCollection servi
services.AddTransient<IJavaScriptTypesProvider, JavaScriptTypesProvider>();
services.AddTransient<IDefaultValueForTypeCalculator, DefaultValueForTypeCalculator>();

services.AddTransient<IVisitor<TypeValue, Type>, TypeBuilder>();

services.AddTransient<IVisitor<IAbstractSyntaxTreeNode>, SymbolTableInitializer>();
services.AddTransient<IVisitor<IAbstractSyntaxTreeNode, TypeSystemLoader>>();
services.AddTransient<IVisitor<IAbstractSyntaxTreeNode, DeclarationVisitor>>();
services.AddTransient<IVisitor<IAbstractSyntaxTreeNode>, TypeSystemLoader>();
services.AddTransient<IVisitor<IAbstractSyntaxTreeNode>, DeclarationVisitor>();

services.AddTransient<IVisitor<IAbstractSyntaxTreeNode, Type>, SemanticChecker>();

Expand Down
30 changes: 0 additions & 30 deletions src/HydraScript/CommandLineSettings.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/HydraScript/ExecuteCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.CommandLine;

namespace HydraScript;

internal class ExecuteCommand : RootCommand
{
public ExecuteCommand() : base("HydraScript interpreter")
{
PathArgument = new Argument<FileInfo>(
name: "path",
description: "Path to input file");
AddArgument(PathArgument);

DumpOption = new Option<bool>(
["-d", "--dump"],
getDefaultValue: () => false,
description: "Show dump data of interpreter");
AddOption(DumpOption);
}

public Argument<FileInfo> PathArgument { get; }
public Option<bool> DumpOption { get; }
}
45 changes: 45 additions & 0 deletions src/HydraScript/ExecuteCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.CommandLine.Invocation;
using HydraScript.Application.CodeGeneration;
using HydraScript.Application.StaticAnalysis.Exceptions;
using HydraScript.Domain.BackEnd;
using HydraScript.Domain.FrontEnd.Lexer;
using HydraScript.Domain.FrontEnd.Parser;
using HydraScript.Infrastructure;

namespace HydraScript;

internal class ExecuteCommandHandler(
ISourceCodeProvider sourceCodeProvider,
IParser parser,
ICodeGenerator codeGenerator,
IVirtualMachine virtualMachine,
TextWriter writer) : ICommandHandler
{

public int Invoke(InvocationContext context)
{
try
{
var sourceCode = sourceCodeProvider.GetText();
var ast = parser.Parse(sourceCode);
var instructions = codeGenerator.GetInstructions(ast);
virtualMachine.Run(instructions);
return 0;
}
catch (Exception ex)
when (ex is LexerException or ParserException or SemanticException)
{
writer.WriteLine(ex.Message);
return 1;
}
catch (Exception ex)
{
writer.WriteLine("Internal HydraScript Error");
writer.WriteLine(ex);
return 2;
}
}

public Task<int> InvokeAsync(InvocationContext context) =>
Task.FromResult(Invoke(context));
}
6 changes: 2 additions & 4 deletions src/HydraScript/HydraScript.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="System.IO.Abstractions" Version="19.2.18" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
</ItemGroup>

</Project>
79 changes: 23 additions & 56 deletions src/HydraScript/Program.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,28 @@
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using CommandLine;
using HydraScript.Domain.FrontEnd.Lexer;
using HydraScript.Domain.FrontEnd.Lexer.Impl;
using HydraScript.Lib.FrontEnd.GetTokens;
using HydraScript.Services.CodeGen;
using HydraScript.Services.CodeGen.Impl;
using HydraScript.Services.Executor;
using HydraScript.Services.Executor.Impl;
using HydraScript.Services.Parsing;
using HydraScript.Services.Parsing.Impl;
using HydraScript.Services.Providers.LexerProvider;
using HydraScript.Services.Providers.LexerProvider.Impl;
using HydraScript.Services.Providers.ParserProvider;
using HydraScript.Services.Providers.ParserProvider.Impl;
using HydraScript.Services.Providers.StructureProvider;
using HydraScript.Services.Providers.StructureProvider.Impl;
using HydraScript.Services.SourceCode;
using HydraScript.Services.SourceCode.Impl;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Parsing;
using HydraScript;
using HydraScript.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace HydraScript;

[ExcludeFromCodeCoverage]
public static class Program
{
private static IServiceCollection ServiceCollection { get; } = new ServiceCollection();
private static IServiceProvider? ServiceProvider { get; set; }

private static void Main(string[] args) =>
Parser.Default.ParseArguments<CommandLineSettings>(args)
.WithParsed(options =>
var command = new ExecuteCommand();
var runner = new CommandLineBuilder(command)
.UseHost(
Host.CreateDefaultBuilder,
configureHost: builder => builder
.ConfigureServices((context, services) =>
{
ConfigureServices(options);
ServiceProvider?
.GetService<IExecutor>()!
.Execute();
services.AddLogging(c => c.ClearProviders());
var parseResult = context.GetInvocationContext().ParseResult;
var fileInfo = parseResult.GetValueForArgument(command.PathArgument);
var dump = parseResult.GetValueForOption(command.DumpOption);
services.AddDomain();
services.AddApplication();
services.AddInfrastructure(dump, fileInfo);
})
.WithNotParsed(errors => errors.Output());


private static void ConfigureServices(CommandLineSettings settings)
{
ServiceCollection.AddSingleton<IStructureProvider, StructureProvider>();
ServiceCollection.AddSingleton<ILexerProvider, LexerProvider>();
ServiceCollection.AddSingleton<IParserProvider, ParserProvider>();
ServiceCollection.AddSingleton<IParsingService, ParsingService>();
ServiceCollection.AddSingleton<ISourceCodeProvider, SourceCodeProvider>();
ServiceCollection.AddSingleton<IFileSystem, FileSystem>();
ServiceCollection.AddSingleton<ITextCoordinateSystemComputer, TextCoordinateSystemComputer>();
ServiceCollection.AddSingleton<ICodeGenService, CodeGenService>();

ServiceCollection.AddSingleton<IExecutor, Executor>();
.UseCommandHandler<ExecuteCommand, ExecuteCommandHandler>())
.UseDefaults().Build();

ServiceCollection.AddSingleton(_ => Options.Create(settings));

ServiceProvider = ServiceCollection.BuildServiceProvider();
}
}
await runner.InvokeAsync(args);
10 changes: 0 additions & 10 deletions src/HydraScript/Services/CodeGen/ICodeGenService.cs

This file was deleted.

76 changes: 0 additions & 76 deletions src/HydraScript/Services/CodeGen/Impl/CodeGenService.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/HydraScript/Services/Executor/IExecutor.cs

This file was deleted.

51 changes: 0 additions & 51 deletions src/HydraScript/Services/Executor/Impl/Executor.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/HydraScript/Services/Parsing/IParsingService.cs

This file was deleted.

Loading

0 comments on commit 67eeece

Please sign in to comment.