refact: 将Compiler类移动到Canon.Console中
This commit is contained in:
parent
1a2ebde4a2
commit
9984cee920
53
Canon.Console/Compiler.cs
Normal file
53
Canon.Console/Compiler.cs
Normal file
|
@ -0,0 +1,53 @@
|
|||
namespace Canon.Console;
|
||||
|
||||
public class Compiler
|
||||
{
|
||||
private readonly Dictionary<string, string> _options = [];
|
||||
private readonly FileInfo _sourceFile;
|
||||
|
||||
public Compiler(string[] args)
|
||||
{
|
||||
if (args.Length < 2)
|
||||
{
|
||||
throw new ArgumentException("Please provide pascal file name with option '-i' at least!");
|
||||
}
|
||||
|
||||
for (int i = 0; i < args.Length; i += 2)
|
||||
{
|
||||
_options.Add(args[i], args[i + 1]);
|
||||
}
|
||||
|
||||
if (!_options.TryGetValue("-i", out string? value))
|
||||
{
|
||||
throw new ArgumentException("Please provide pascal file name with option '-i' at least!");
|
||||
}
|
||||
|
||||
_sourceFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, value));
|
||||
|
||||
if (!_sourceFile.Exists)
|
||||
{
|
||||
throw new InvalidOperationException("Source file not exists!");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Compile()
|
||||
{
|
||||
using StreamReader source = _sourceFile.OpenText();
|
||||
await using StreamWriter output = GetOutputFile().CreateText();
|
||||
|
||||
await output.WriteAsync("""
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
printf("%d", 3);
|
||||
return 0;
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
private FileInfo GetOutputFile()
|
||||
{
|
||||
return new FileInfo(Path.Combine(_sourceFile.DirectoryName!,
|
||||
Path.GetFileNameWithoutExtension(_sourceFile.Name)) + ".c");
|
||||
}
|
||||
}
|
|
@ -1,35 +1,4 @@
|
|||
using Canon.Core;
|
||||
using Canon.Console;
|
||||
|
||||
if (args.Length < 2)
|
||||
{
|
||||
Console.WriteLine("Please provide pascal file name with option '-i' at least!");
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<string, string> options = new();
|
||||
|
||||
for (int i = 0; i < args.Length; i += 2)
|
||||
{
|
||||
options.Add(args[i], args[i + 1]);
|
||||
}
|
||||
|
||||
if (!options.TryGetValue("-i", out string? value))
|
||||
{
|
||||
Console.WriteLine("Please provide pascal file name with option '-i' at least!");
|
||||
return;
|
||||
}
|
||||
|
||||
FileInfo sourceFile = new(value);
|
||||
if (!sourceFile.Exists)
|
||||
{
|
||||
Console.WriteLine("Target source file doesn't exist!");
|
||||
return;
|
||||
}
|
||||
|
||||
Compiler compiler = new();
|
||||
using StreamReader source = sourceFile.OpenText();
|
||||
FileInfo outputFile = new(Path.Combine(sourceFile.DirectoryName!,
|
||||
Path.GetFileNameWithoutExtension(sourceFile.Name)) + ".c");
|
||||
await using StreamWriter writer = outputFile.CreateText();
|
||||
|
||||
await writer.WriteAsync(compiler.Compile(await source.ReadToEndAsync()));
|
||||
Compiler compiler = new(args);
|
||||
await compiler.Compile();
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
namespace Canon.Core;
|
||||
|
||||
public class Compiler
|
||||
{
|
||||
public string Compile(string _)
|
||||
{
|
||||
return """
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
printf("%d", 3);
|
||||
return 0;
|
||||
}
|
||||
""";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user