[lab2] runnable
This commit is contained in:
@@ -18,9 +18,9 @@ static bool argFormat = false;
|
||||
void usage(int code = EXIT_FAILURE) {
|
||||
const char *msg = "Usage: sysyc [options] inputfile\n\n"
|
||||
"Supported options:\n"
|
||||
" -h \tprint help message and exit\n";
|
||||
" -f \tpretty-format the input file\n";
|
||||
" -s {ast,ir,asm}\tstop after generating AST/IR/Assembly\n";
|
||||
" -h \tprint help message and exit\n"
|
||||
" -f \tpretty-format the input file\n"
|
||||
" -s {ast,ir,asm}\tstop after generating AST/IR/Assembly\n";
|
||||
cerr << msg;
|
||||
exit(code);
|
||||
}
|
||||
@@ -51,14 +51,14 @@ void parseArgs(int argc, char **argv) {
|
||||
int main(int argc, char **argv) {
|
||||
parseArgs(argc, argv);
|
||||
|
||||
// open the input file
|
||||
// 打开输入文件
|
||||
ifstream fin(argInputFile);
|
||||
if (not fin) {
|
||||
cerr << "Failed to open file " << argv[1];
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// parse sysy source to AST
|
||||
// 解析 SysY 源码为 AST
|
||||
ANTLRInputStream input(fin);
|
||||
SysYLexer lexer(&input);
|
||||
CommonTokenStream tokens(&lexer);
|
||||
@@ -69,28 +69,27 @@ int main(int argc, char **argv) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// pretty format the input file
|
||||
// 格式化输入文件
|
||||
if (argFormat) {
|
||||
ASTPrinter printer;
|
||||
printer.visitCompUnit(moduleAST);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// visit AST to generate IR
|
||||
// 遍历 AST 生成 IR
|
||||
SysYIRGenerator generator;
|
||||
generator.visitCompUnit(moduleAST);
|
||||
auto moduleIR = generator.get();
|
||||
generator.generateIR(moduleAST); // 使用公共接口生成 IR
|
||||
if (argStopAfter == "ir") {
|
||||
moduleIR->print(cout);
|
||||
cout << generator.getIR(); // 输出生成的 IR
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// generate assembly
|
||||
CodeGen codegen(moduleIR);
|
||||
string asmCode = codegen.code_gen();
|
||||
cout << asmCode << endl;
|
||||
if (argStopAfter == "asm")
|
||||
return EXIT_SUCCESS;
|
||||
// // 生成汇编代码
|
||||
// CodeGen codegen(generator.getIR()); // 假设 CodeGen 接受字符串作为输入
|
||||
// string asmCode = codegen.code_gen();
|
||||
// cout << asmCode << endl;
|
||||
// if (argStopAfter == "asm")
|
||||
// return EXIT_SUCCESS;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user