From 9984cee920939bfceba0d153090a0a0fb8a5f2b0 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 11 Mar 2024 00:26:25 +0800 Subject: [PATCH] =?UTF-8?q?refact:=20=E5=B0=86Compiler=E7=B1=BB=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=88=B0Canon.Console=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Canon.Console/Compiler.cs | 53 +++++++++++++++++++++++++++++++++++++++ Canon.Console/Program.cs | 37 +++------------------------ Canon.Core/Compiler.cs | 16 ------------ 3 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 Canon.Console/Compiler.cs delete mode 100644 Canon.Core/Compiler.cs diff --git a/Canon.Console/Compiler.cs b/Canon.Console/Compiler.cs new file mode 100644 index 0000000..2cd1139 --- /dev/null +++ b/Canon.Console/Compiler.cs @@ -0,0 +1,53 @@ +namespace Canon.Console; + +public class Compiler +{ + private readonly Dictionary _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 + int main() + { + printf("%d", 3); + return 0; + } + """); + } + + private FileInfo GetOutputFile() + { + return new FileInfo(Path.Combine(_sourceFile.DirectoryName!, + Path.GetFileNameWithoutExtension(_sourceFile.Name)) + ".c"); + } +} diff --git a/Canon.Console/Program.cs b/Canon.Console/Program.cs index 50711a7..7bc7245 100644 --- a/Canon.Console/Program.cs +++ b/Canon.Console/Program.cs @@ -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 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(); diff --git a/Canon.Core/Compiler.cs b/Canon.Core/Compiler.cs deleted file mode 100644 index a6c39e1..0000000 --- a/Canon.Core/Compiler.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Canon.Core; - -public class Compiler -{ - public string Compile(string _) - { - return """ - #include - int main() - { - printf("%d", 3); - return 0; - } - """; - } -} \ No newline at end of file