From 7ccd4a14ac4447dfcf8d76e4bd277f7ec3a3bd51 Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Tue, 6 May 2025 23:08:42 -0400 Subject: [PATCH] Build to Base64-based bytecode BBBB --- .gitignore | 2 ++ CompileCommand.cs | 32 ++++++++++++++++++++++++++++++++ Program.cs | 1 + README.md | 4 ++-- ScriptHelper.cs | 8 ++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 CompileCommand.cs diff --git a/.gitignore b/.gitignore index 0babe19..b0876d7 100644 --- a/.gitignore +++ b/.gitignore @@ -542,3 +542,5 @@ FodyWeavers.xsd # End of https://www.toptal.com/developers/gitignore/api/rider,visualbasic,visualstudio # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) + +*.glb diff --git a/CompileCommand.cs b/CompileCommand.cs new file mode 100644 index 0000000..241deca --- /dev/null +++ b/CompileCommand.cs @@ -0,0 +1,32 @@ +namespace Glyph; + +[Command("build", Description = "Build Glyph program.")] +public class CompileCommand : ICommand +{ + [CommandParameter(0, Name = "file", Description = "Path to the Glyph source file.")] + public string FilePath { get; set; } + + [CommandOption("verbose", 'v', Description = "Enable verbose output.")] + public bool Verbose { get; set; } + + public async ValueTask ExecuteAsync(IConsole console) + { + + if (!System.IO.File.Exists(FilePath)) + { + await console.Error.WriteLineAsync($"Error: File not found: {FilePath}"); + return; + } + + var lines = await System.IO.File.ReadAllLinesAsync(FilePath); + var source = ScriptHelper.LinesToString(lines); + + if (Verbose) + { + await console.Output.WriteLineAsync($"Compiling: {FilePath}"); + } + + var binary = ScriptHelper.EncodeTo64(source); + File.WriteAllText(Path.Combine(Path.GetDirectoryName(FilePath), "output.glb"), binary); + } +} diff --git a/Program.cs b/Program.cs index c55e416..1c4ae69 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ await new CliApplicationBuilder() .AddCommand() + .AddCommand() .Build() .RunAsync(); diff --git a/README.md b/README.md index 879cdd7..20f1256 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Glyph is a human-readable esoteric programming language that is designed to fit - [x] Parser -- [ ] CLI tool +- [x] CLI tool - [ ] Unicode support @@ -14,7 +14,7 @@ Glyph is a human-readable esoteric programming language that is designed to fit - [ ] QR Code - - [ ] Base64 + - [x] Base64 ## 🧾 Program Structure diff --git a/ScriptHelper.cs b/ScriptHelper.cs index 6b5f956..aac1678 100644 --- a/ScriptHelper.cs +++ b/ScriptHelper.cs @@ -15,4 +15,12 @@ public static class ScriptHelper return returnValue; } + + /// + /// Converts an array of strings to a single string with each element separated by a newline. + /// + /// The array of strings to convert. + /// The resulting string. + static public string LinesToString(string[] lines) => string.Join(Environment.NewLine, lines); + }