summaryrefslogtreecommitdiff
path: root/Compiler
diff options
context:
space:
mode:
authorMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-29 07:33:48 +1000
committerMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-29 07:33:48 +1000
commit40473797a117e6791e94140b0f01d034bce8bbb1 (patch)
tree553b84b0c2491343523bc7ebf91dda54080991aa /Compiler
parent5d012f16b2a0200087afe9dffc906046d258b3b8 (diff)
some cleanup
Diffstat (limited to 'Compiler')
-rw-r--r--Compiler/CompilerOptions.cs3
-rw-r--r--Compiler/PreprocessingCharStream.cs25
-rw-r--r--Compiler/Preprocessor.cs2
-rw-r--r--Compiler/Program.cs36
4 files changed, 40 insertions, 26 deletions
diff --git a/Compiler/CompilerOptions.cs b/Compiler/CompilerOptions.cs
index c1f5c3a..87ec384 100644
--- a/Compiler/CompilerOptions.cs
+++ b/Compiler/CompilerOptions.cs
@@ -12,4 +12,7 @@ public class CompilerOptions
[Option('c', "comment-compilation", Required = false, HelpText = "Comments to preserve in the compiled result")]
public CommentCompilationMode CommentCompilationMode { get; set; } = CommentCompilationMode.UserDefined;
+
+ [Option('v', "verbose", Required = false, HelpText = "Enable verbose output during compilation.")]
+ public bool Verbose { get; set; } = false;
} \ No newline at end of file
diff --git a/Compiler/PreprocessingCharStream.cs b/Compiler/PreprocessingCharStream.cs
new file mode 100644
index 0000000..428cbff
--- /dev/null
+++ b/Compiler/PreprocessingCharStream.cs
@@ -0,0 +1,25 @@
+namespace Q1.Compiler;
+
+using Antlr4.Runtime;
+using Antlr4.Runtime.Misc;
+
+public class PreprocessingCharStream : ICharStream
+{
+ private readonly AntlrInputStream inner;
+
+ public PreprocessingCharStream(string filePath)
+ {
+ string preprocessed = Preprocessor.Process(filePath);
+ this.inner = new AntlrInputStream(preprocessed);
+ }
+
+ public int Index => this.inner.Index;
+ public int Size => this.inner.Size;
+ public string SourceName => this.inner.SourceName;
+ public void Consume() => this.inner.Consume();
+ public int LA(int i) => this.inner.LA(i);
+ public int Mark() => this.inner.Mark();
+ public void Release(int marker) => this.inner.Release(marker);
+ public void Seek(int index) => this.inner.Seek(index);
+ public string GetText(Interval interval) => this.inner.GetText(interval);
+} \ No newline at end of file
diff --git a/Compiler/Preprocessor.cs b/Compiler/Preprocessor.cs
index fba28c1..f11ae3a 100644
--- a/Compiler/Preprocessor.cs
+++ b/Compiler/Preprocessor.cs
@@ -26,7 +26,6 @@ public static class Preprocessor
if (line.StartsWith("#include"))
{
- // simple format: #include "file"
Match match = Regex.Match(line, "#include\\s+\"([^\"]+)\"");
if (match.Success)
{
@@ -37,7 +36,6 @@ public static class Preprocessor
}
else if (line.StartsWith("#define"))
{
- // simple format: #define NAME VALUE
Match match = Regex.Match(line, "#define\\s+(\\w+)\\s+(.*)");
if (match.Success)
Defines[match.Groups[1].Value] = match.Groups[2].Value;
diff --git a/Compiler/Program.cs b/Compiler/Program.cs
index 09f7127..cde49a5 100644
--- a/Compiler/Program.cs
+++ b/Compiler/Program.cs
@@ -2,7 +2,6 @@
using System.Text;
using Antlr4.Runtime;
-using Antlr4.Runtime.Misc;
using CommandLine;
using visitors;
using Parser = CommandLine.Parser;
@@ -41,33 +40,22 @@ public class Program
IEnumerable<string> result = compiler.Compile(parser.program());
File.WriteAllLines(options.OutputFile, result);
+ if (options.Verbose)
+ {
+ Console.WriteLine();
+ foreach (string line in result)
+ Console.WriteLine(line);
+ Console.WriteLine();
+ }
- Console.WriteLine("Compilation successful.");
+ Console.WriteLine($"Compilation successful. ({result.Count(x=>x != string.Empty)} instructions)");
}
catch (CompilerException ex)
{
- Console.WriteLine($"{ex.Message}");
+ Console.ForegroundColor = ConsoleColor.Red;
+ Console.Error.WriteLine($"Error: {ex.Message}");
+ Console.ResetColor();
+ Environment.Exit(1);
}
}
-}
-
-public class PreprocessingCharStream : ICharStream
-{
- private readonly AntlrInputStream inner;
-
- public PreprocessingCharStream(string filePath)
- {
- string preprocessed = Preprocessor.Process(filePath);
- inner = new AntlrInputStream(preprocessed);
- }
-
- public int Index => inner.Index;
- public int Size => inner.Size;
- public string SourceName => inner.SourceName;
- public void Consume() => inner.Consume();
- public int LA(int i) => inner.LA(i);
- public int Mark() => inner.Mark();
- public void Release(int marker) => inner.Release(marker);
- public void Seek(int index) => inner.Seek(index);
- public string GetText(Interval interval) => inner.GetText(interval);
} \ No newline at end of file