summaryrefslogtreecommitdiff
path: root/Compiler/Program.cs
blob: fdd87224f58093fdbc6c659faf7bd92c96bab381 (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
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);

        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);
    }
}