feat(dev): 支持 compiler --help 输出帮助信息
This commit is contained in:
@@ -7,11 +7,16 @@
|
|||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
#include "sem/Sema.h"
|
#include "sem/Sema.h"
|
||||||
#include "utils/CLI.h"
|
#include "utils/CLI.h"
|
||||||
|
#include "utils/Log.h"
|
||||||
#include "ast/AstNodes.h"
|
#include "ast/AstNodes.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
try {
|
try {
|
||||||
auto opts = ParseCLI(argc, argv);
|
auto opts = ParseCLI(argc, argv);
|
||||||
|
if (opts.show_help) {
|
||||||
|
PrintHelp(std::cout);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
auto antlr = ParseFileWithAntlr(opts.input);
|
auto antlr = ParseFileWithAntlr(opts.input);
|
||||||
auto ast = BuildAst(antlr.tree);
|
auto ast = BuildAst(antlr.tree);
|
||||||
ast::PrintAST(*ast); // 调试 AST
|
ast::PrintAST(*ast); // 调试 AST
|
||||||
|
|||||||
@@ -5,13 +5,38 @@
|
|||||||
|
|
||||||
#include "utils/CLI.h"
|
#include "utils/CLI.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
CLIOptions ParseCLI(int argc, char** argv) {
|
CLIOptions ParseCLI(int argc, char** argv) {
|
||||||
if (argc <= 1) {
|
|
||||||
throw std::runtime_error("用法: compiler <input.sy>");
|
|
||||||
}
|
|
||||||
CLIOptions opt;
|
CLIOptions opt;
|
||||||
opt.input = argv[1];
|
|
||||||
|
if (argc <= 1) {
|
||||||
|
throw std::runtime_error("用法: compiler [--help] <input.sy>");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
const char* arg = argv[i];
|
||||||
|
if (std::strcmp(arg, "-h") == 0 || std::strcmp(arg, "--help") == 0) {
|
||||||
|
opt.show_help = true;
|
||||||
|
return opt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg[0] == '-') {
|
||||||
|
throw std::runtime_error(std::string("未知参数: ") + arg +
|
||||||
|
"(使用 --help 查看用法)");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!opt.input.empty()) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"参数过多:当前只支持 1 个输入文件(使用 --help 查看用法)");
|
||||||
|
}
|
||||||
|
opt.input = arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opt.input.empty()) {
|
||||||
|
throw std::runtime_error("缺少输入文件:请提供 <input.sy>(使用 --help 查看用法)");
|
||||||
|
}
|
||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
struct CLIOptions {
|
struct CLIOptions {
|
||||||
std::string input;
|
std::string input;
|
||||||
|
bool show_help = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
CLIOptions ParseCLI(int argc, char** argv);
|
CLIOptions ParseCLI(int argc, char** argv);
|
||||||
|
|||||||
@@ -3,3 +3,19 @@
|
|||||||
// - 提供可配置的日志级别与输出位置(按需要实现)
|
// - 提供可配置的日志级别与输出位置(按需要实现)
|
||||||
|
|
||||||
#include "utils/Log.h"
|
#include "utils/Log.h"
|
||||||
|
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
void PrintHelp(std::ostream& os) {
|
||||||
|
os << "SysY Compiler (课程实验最小可运行示例)\n"
|
||||||
|
<< "\n"
|
||||||
|
<< "用法:\n"
|
||||||
|
<< " compiler [--help] <input.sy>\n"
|
||||||
|
<< "\n"
|
||||||
|
<< "选项:\n"
|
||||||
|
<< " -h, --help 打印帮助信息并退出\n"
|
||||||
|
<< "\n"
|
||||||
|
<< "说明:\n"
|
||||||
|
<< " - 当前默认将 IR 输出到标准输出,可使用重定向写入文件:\n"
|
||||||
|
<< " compiler test/test_case/simple_add.sy > out.ll\n";
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
// 轻量日志接口。
|
// 轻量日志接口。
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <iosfwd>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define LOG_INFO(msg) std::cerr << "[info] " << msg << "\n"
|
#define LOG_INFO(msg) std::cerr << "[info] " << msg << "\n"
|
||||||
#define LOG_ERROR(msg) std::cerr << "[error] " << msg << "\n"
|
#define LOG_ERROR(msg) std::cerr << "[error] " << msg << "\n"
|
||||||
|
|
||||||
|
// 打印命令行帮助信息(用于 `compiler --help`)。
|
||||||
|
void PrintHelp(std::ostream& os);
|
||||||
|
|||||||
Reference in New Issue
Block a user