2024-04-20 22:23:29 +08:00
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using Canon.Console.Models;
|
|
|
|
|
using Canon.Console.Services;
|
|
|
|
|
using Canon.Core.Abstractions;
|
2024-04-22 21:26:34 +08:00
|
|
|
|
using Canon.Core.GrammarParser;
|
2024-04-20 22:23:29 +08:00
|
|
|
|
using Canon.Core.LexicalParser;
|
2024-04-30 15:04:58 +08:00
|
|
|
|
using Canon.Core.SemanticParser;
|
2024-04-20 22:23:29 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Console.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class RootCommandExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void SetCompile(this RootCommand rootCommand)
|
|
|
|
|
{
|
|
|
|
|
Option<string> sourceFilenameOption = new("--input", "The source pascal filename")
|
|
|
|
|
{
|
|
|
|
|
IsRequired = true
|
|
|
|
|
};
|
|
|
|
|
sourceFilenameOption.AddAlias("-i");
|
|
|
|
|
|
|
|
|
|
rootCommand.AddOption(sourceFilenameOption);
|
|
|
|
|
|
|
|
|
|
rootCommand.SetHandler(async (context) =>
|
|
|
|
|
{
|
|
|
|
|
string? sourceFilename = context.ParseResult.GetValueForOption(sourceFilenameOption);
|
|
|
|
|
if (sourceFilename is null)
|
|
|
|
|
{
|
|
|
|
|
System.Console.WriteLine("Error: please provide source filename with option '-i'.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
|
|
|
|
|
|
|
|
|
|
builder.Logging.ClearProviders();
|
|
|
|
|
builder.Logging.Services.AddSingleton<ILoggerProvider, CompilerLoggerProvider>();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<CompilerOption>(
|
|
|
|
|
_ => new CompilerOption { SourceFilename = sourceFilename });
|
|
|
|
|
builder.Services.AddTransient<ILexer, Lexer>();
|
2024-04-30 15:04:58 +08:00
|
|
|
|
builder.Services.AddSingleton<SyntaxTreeTraveller>();
|
2024-04-20 22:23:29 +08:00
|
|
|
|
builder.Services.AddSingleton<IGrammarParser>(_ => GeneratedGrammarParser.Instance);
|
|
|
|
|
builder.Services.AddHostedService<Compiler>();
|
|
|
|
|
|
|
|
|
|
using IHost host = builder.Build();
|
|
|
|
|
await host.RunAsync();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|