-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
181 lines (152 loc) · 5.42 KB
/
Program.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.CommandLine;
using System.Linq;
using System.Threading.Tasks;
namespace Cirno;
internal sealed class Program
{
internal static async Task<int> Main(string[] args)
{
var rootCommand = new System.CommandLine.RootCommand(
"一个基于LLVMSharp实现的C-编译器前端"
);
var inputFilePathOpt
= new System.CommandLine.Option<string?>(
name: "--file", description: "输入的c-文件"
)
{
IsRequired = true
};
inputFilePathOpt.AddValidator(result =>
{
var filePath = result.GetValueForOption(inputFilePathOpt);
if (!System.IO.File.Exists(filePath))
{
result.ErrorMessage = $"无效的输入文件路径: {filePath}";
}
});
rootCommand.AddOption(inputFilePathOpt);
// rootCommand.SetHandler(file => );
var outputFilePathOpt
= new System.CommandLine.Option<string?>(
name: "--output", description: "输出文件地址"
)
{
IsRequired = true
};
outputFilePathOpt.AddValidator(result =>
{
var filePath = result.GetValueForOption(outputFilePathOpt);
if (string.IsNullOrWhiteSpace(filePath))
{
result.ErrorMessage = $"无效的输出文件路径: {filePath}";
}
});
rootCommand.AddOption(outputFilePathOpt);
var emit2IrOpt = new System.CommandLine.Option<bool>(
name: "--emit-llvm",
description: "输出LLVM IR"
)
{
IsRequired = false
};
rootCommand.AddOption(emit2IrOpt);
var dumpExprOpt = new System.CommandLine.Option<bool>(
name: "--dump-expr",
description: "输出语法树"
)
{
IsRequired = false
};
rootCommand.AddOption(dumpExprOpt);
var dumpAstOpt = new System.CommandLine.Option<bool>(
name: "--dump-ast",
description: "输出抽象语法树"
)
{
IsRequired = false
};
rootCommand.AddOption(dumpAstOpt);
var dumpTokensOpt = new System.CommandLine.Option<bool>(
name: "--dump-tokens",
description: "输出记号流"
)
{
IsRequired = false
};
rootCommand.AddOption(dumpTokensOpt);
rootCommand.SetHandler(async (inputFilePath, outputFilePath, isEmitIr, isDumpExpr, isDumpAst, isDumpTokens) =>
await StartSolution(inputFilePath!, outputFilePath!, isEmitIr, isDumpExpr, isDumpAst, isDumpTokens),
inputFilePathOpt,
outputFilePathOpt,
emit2IrOpt,
dumpExprOpt,
dumpAstOpt,
dumpTokensOpt
);
return await rootCommand.InvokeAsync(args);
}
internal static string[] ReadFile(string file)
{
return System.IO.File.ReadLines(file).Select(line => $"{line} ").ToArray();
}
internal static async Task StartSolution(string inputFilePath, string outputFilePath, bool isEmitIr, bool isDumpExpr, bool isDumpAst, bool isDumpTokens)
{
var lines = ReadFile(inputFilePath);
var lexer = new Cirno.Lexer.Lexer(lines);
var tokens = lexer.GetTokens();
if (isDumpTokens)
{
Console.WriteLine("Dump tokens.");
foreach (var token in tokens)
{
Console.WriteLine(token);
}
}
if (lexer.Diagnostics.Count > 0)
{
lexer.Diagnostics.Dump(lines);
System.Environment.Exit(-1);
}
var parser = new Cirno.Parser.Parser(tokens, lexer.Diagnostics);
var exprTree = parser.Parse();
if (isDumpExpr)
{
Console.WriteLine("Dump expression tree.");
exprTree.Dump();
}
if (parser.Diagnostics.Count > 0)
{
parser.Diagnostics.Dump(lines);
System.Environment.Exit(-1);
}
var ast = new Cirno.AbstractSyntaxTree.AST(exprTree);
if (isDumpAst)
{
Console.WriteLine("Dump ast.");
ast.Dump();
}
var moduleName = System.IO.Path.GetFileNameWithoutExtension(inputFilePath);
using var codeGenVisitor = new Cirno.CodeGen.CodeGenVisitor(moduleName, parser.Diagnostics);
ast.Root.Accept(codeGenVisitor);
var llvmIrCode = codeGenVisitor.Module.ToString();
System.Diagnostics.Debug.WriteLine(llvmIrCode);
if (codeGenVisitor.Diagnostics.Count > 0)
{
codeGenVisitor.Diagnostics.Dump(lines);
System.Environment.Exit(-1);
}
if (!codeGenVisitor.Verify(out _))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error in code gen.");
System.Environment.Exit(-1);
}
if (isEmitIr)
{
await System.IO.File.WriteAllTextAsync(outputFilePath, llvmIrCode);
return;
}
await codeGenVisitor.CompileIR2ExeFile(outputFilePath);
}
}