summaryrefslogtreecommitdiff
path: root/Compiler/Program.cs
blob: 9259205dfd16f65507325a7a7d0cbda2664d3f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namespace Q1.Compiler;

using Antlr4.Runtime;
using CommandLine;
using Parser = CommandLine.Parser;

class Program
{
    static void Main(string[] args)
    {
        Parser commandLineParser = new(with =>
        {
            with.CaseInsensitiveEnumValues = true;
        });
        
        commandLineParser.ParseArguments<CompilerOptions>(args)
            .WithParsed(Program.Main);
    }

    static void Main(CompilerOptions options)
    {
        Environment.CurrentDirectory = Path.GetDirectoryName(options.InputFile) ?? Environment.CurrentDirectory;
        options.InputFile            = Path.GetFileName(options.InputFile);

        try
        {
            AntlrFileStream inputStream = new(options.InputFile);
            CGrammarLexer lexer = new(inputStream);
            lexer.RemoveErrorListeners();

            ITokenStream tokenStream = new BufferedTokenStream(lexer);
            CGrammarParser parser = new(tokenStream);
            parser.RemoveErrorListeners();

            CCompilerVisitor compiler = new(options.CommentCompilationMode);
            IEnumerable<string> result = compiler.Compile(parser.program());

            File.WriteAllLines(options.OutputFile, result);
        }
        catch (CompilerException ex)
        {
            Console.WriteLine($"{ex.Message}");
        }
    }
}