add argparse

This commit is contained in:
ridethepig 2023-04-30 01:01:03 +08:00
parent 8324561db1
commit 5c492ddbf4
2 changed files with 1733 additions and 6 deletions

1698
include/argparse.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +1,55 @@
#include <iostream>
#include <fstream>
#include <string>
#include "antlr4-runtime.h"
#include "SysyLexer.h"
#include "SysyParser.h"
#include "antlr4-runtime.h"
#include <argparse.hpp>
using namespace antlrSysY;
using namespace antlr4;
int main(int , const char **) {
ANTLRInputStream input(u8"🍴 = 🍐 + \"😎\";(((x * π))) * µ + ∰; a + (x * (y ? 0 : 1) + z);");
int main(int argc, const char **argv) {
#pragma region ArgParse
argparse::ArgumentParser arg_parser("Catfood's SysY Compiler");
arg_parser.add_argument("source").help("A single SysY source file");
arg_parser.add_argument("-S")
.implicit_value(true)
.help("Useless but required by the contest")
.required();
arg_parser.add_argument("-o").help("Output file name").required();
arg_parser.add_argument("-O1").implicit_value(true).default_value(false).help(
"Performance mode");
try {
arg_parser.parse_args(argc, argv);
} catch (const std::runtime_error &err) {
std::cerr << err.what() << std::endl;
std::cerr << arg_parser;
std::exit(1);
}
auto source_file = arg_parser.get<std::string>("source");
auto output_file = arg_parser.get<std::string>("-o");
auto flg_O1 = arg_parser["-O1"] == true;
// std::cout << source_file << " " << output_file << " " << flg_O1 << std::endl;
#pragma endregion
std::ifstream ifs_source_file(source_file);
ANTLRInputStream input(ifs_source_file);
SysyLexer lexer(&input);
CommonTokenStream tokens(&lexer);
tokens.fill();
#if 0
for (auto token : tokens.getTokens()) {
std::cout << token->toString() << std::endl;
}
#endif
SysyParser parser(&tokens);
tree::ParseTree* tree = parser.program();
tree::ParseTree *tree = parser.program();
#if 0
std::cout << tree->toStringTree(&parser) << std::endl << std::endl;
#endif
return 0;
}