blob: 09f7127900c117bb0fc3b6265b83d496c68227b3 (
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
namespace Q1.Compiler;
using System.Text;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using CommandLine;
using visitors;
using Parser = CommandLine.Parser;
public class Program
{
static void Main(string[] args)
{
Parser commandLineParser = new(with =>
{
with.CaseInsensitiveEnumValues = true;
});
commandLineParser
.ParseArguments<CompilerOptions>(args)
.WithParsed(Program.Main);
}
public static void Main(CompilerOptions options)
{
options.InputFile = options.InputFile;
try
{
ICharStream inputStream = new PreprocessingCharStream(options.InputFile);
CGrammarLexer lexer = new(inputStream);
lexer.RemoveErrorListeners();
ITokenStream tokenStream = new BufferedTokenStream(lexer);
CGrammarParser parser = new(tokenStream);
parser.RemoveErrorListeners();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding encoding = Encoding.GetEncoding(437);
CCompilerVisitor compiler = new(options.CommentCompilationMode, encoding);
IEnumerable<string> result = compiler.Compile(parser.program());
File.WriteAllLines(options.OutputFile, result);
Console.WriteLine("Compilation successful.");
}
catch (CompilerException ex)
{
Console.WriteLine($"{ex.Message}");
}
}
}
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);
}
|