mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[FunC] Use td::OptionParser in func-main.cpp
This commit is contained in:
parent
ef5719d7e6
commit
0628e17c7d
1 changed files with 61 additions and 75 deletions
|
@ -26,83 +26,69 @@
|
||||||
Copyright 2017-2020 Telegram Systems LLP
|
Copyright 2017-2020 Telegram Systems LLP
|
||||||
*/
|
*/
|
||||||
#include "func.h"
|
#include "func.h"
|
||||||
#include "parser/srcread.h"
|
#include "td/utils/OptionParser.h"
|
||||||
#include "parser/lexer.h"
|
|
||||||
#include "parser/symtable.h"
|
|
||||||
#include <getopt.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "git.h"
|
#include "git.h"
|
||||||
|
|
||||||
void usage(const char* progname) {
|
int main(int argc, char* argv[]) {
|
||||||
std::cerr
|
|
||||||
<< "usage: " << progname
|
|
||||||
<< " [-vIAPSR][-O<level>][-i<indent-spc>][-o<output-filename>][-W<boc-filename>] {<func-source-filename> ...}\n"
|
|
||||||
"\tGenerates Fift TVM assembler code from a funC source\n"
|
|
||||||
"-I\tEnables interactive mode (parse stdin)\n"
|
|
||||||
"-o<fift-output-filename>\tWrites generated code into specified file instead of stdout\n"
|
|
||||||
"-v\tIncreases verbosity level (extra information output into stderr)\n"
|
|
||||||
"-i<indent>\tSets indentation for the output code (in two-space units)\n"
|
|
||||||
"-A\tPrefix code with `\"Asm.fif\" include` preamble\n"
|
|
||||||
"-O<level>\tSets optimization level (2 by default)\n"
|
|
||||||
"-P\tEnvelope code into PROGRAM{ ... }END>c\n"
|
|
||||||
"-S\tInclude stack layout comments in the output code\n"
|
|
||||||
"-R\tInclude operation rewrite comments in the output code\n"
|
|
||||||
"-W<output-boc-file>\tInclude Fift code to serialize and save generated code into specified BoC file. Enables "
|
|
||||||
"-A and -P.\n"
|
|
||||||
"\t-s\tOutput semantic version of FunC and exit\n"
|
|
||||||
"\t-V<version>\tShow func build information\n";
|
|
||||||
std::exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* const argv[]) {
|
|
||||||
int i;
|
|
||||||
std::string output_filename;
|
std::string output_filename;
|
||||||
while ((i = getopt(argc, argv, "Ahi:Io:O:PRsSvW:V")) != -1) {
|
td::OptionParser p;
|
||||||
switch (i) {
|
p.set_description("usage: func "
|
||||||
case 'A':
|
"[-vIAPSR][-O<level>][-i<indent-spc>][-o<output-filename>][-W<boc-filename>] {<func-source-filename> ...}\n"
|
||||||
funC::asm_preamble = true;
|
"Generates Fift TVM assembler code from FunC sources");
|
||||||
break;
|
|
||||||
case 'I':
|
p.add_option('I', "interactive", "Enables interactive mode (parse stdin)", [] {
|
||||||
funC::interactive = true;
|
funC::interactive = true;
|
||||||
break;
|
});
|
||||||
case 'i':
|
p.add_option('o', "output", "Writes generated code into specified file instead of stdout", [&output_filename](td::Slice arg) {
|
||||||
funC::indent = std::max(0, atoi(optarg));
|
output_filename = arg.str();
|
||||||
break;
|
});
|
||||||
case 'o':
|
p.add_option('v', "verbose", "Increases verbosity level (extra information output into stderr)", [] {
|
||||||
output_filename = optarg;
|
++funC::verbosity;
|
||||||
break;
|
});
|
||||||
case 'O':
|
p.add_option('i', "indent", "Sets indentation for the output code (in two-space units)", [](td::Slice arg) {
|
||||||
funC::opt_level = std::max(0, atoi(optarg));
|
funC::indent = std::max(0, std::atoi(arg.str().c_str()));
|
||||||
break;
|
});
|
||||||
case 'P':
|
p.add_option('A', "asm-preamble", "prefix code with `\"Asm.fif\" include` preamble", [] {
|
||||||
funC::program_envelope = true;
|
funC::asm_preamble = true;
|
||||||
break;
|
});
|
||||||
case 'R':
|
p.add_option('O', "opt-level", "Sets optimization level (2 by default)", [](td::Slice arg) {
|
||||||
funC::op_rewrite_comments = true;
|
funC::opt_level = std::max(0, std::atoi(arg.str().c_str()));
|
||||||
break;
|
});
|
||||||
case 'S':
|
p.add_option('P', "program-envelope", "Envelope code into PROGRAM{ ... }END>c", [] {
|
||||||
funC::stack_layout_comments = true;
|
funC::program_envelope = true;
|
||||||
break;
|
});
|
||||||
case 'v':
|
p.add_option('S', "stack-comments", "Include stack layout comments in the output code", []{
|
||||||
++funC::verbosity;
|
funC::stack_layout_comments = true;
|
||||||
break;
|
});
|
||||||
case 'W':
|
p.add_option('R', "rewrite-comments", "Include operation rewrite comments in the output code", [] {
|
||||||
funC::boc_output_filename = optarg;
|
funC::op_rewrite_comments = true;
|
||||||
funC::asm_preamble = funC::program_envelope = true;
|
});
|
||||||
break;
|
p.add_option('W', "boc-output", "Include Fift code to serialize and save generated code into specified BoC file. Enables -A and -P", [](td::Slice arg) {
|
||||||
case 's':
|
funC::boc_output_filename = arg.str();
|
||||||
std::cout << funC::func_version << "\n";
|
funC::asm_preamble = funC::program_envelope = true;
|
||||||
std::exit(0);
|
});
|
||||||
break;
|
p.add_option('s', "version", "Output semantic version of FunC and exit", [] {
|
||||||
case 'V':
|
std::cout << funC::func_version << "\n";
|
||||||
std::cout << "FunC semantic version: v" << funC::func_version << "\n";
|
std::exit(0);
|
||||||
std::cout << "Build information: [ Commit: " << GitMetadata::CommitSHA1() << ", Date: " << GitMetadata::CommitDate() << "]\n";
|
});
|
||||||
std::exit(0);
|
p.add_option('V', "full-version", "Show FunC build information and exit", [] {
|
||||||
break;
|
std::cout << "FunC semantic version: v" << funC::func_version << "\n";
|
||||||
case 'h':
|
std::cout << "Build information: [ Commit: " << GitMetadata::CommitSHA1() << ", Date: " << GitMetadata::CommitDate() << "]\n";
|
||||||
default:
|
std::exit(0);
|
||||||
usage(argv[0]);
|
});
|
||||||
}
|
p.add_option('h', "help", "Print help and exit", [&p] {
|
||||||
|
char b[10240];
|
||||||
|
td::StringBuilder sb(td::MutableSlice{b, 10000});
|
||||||
|
sb << p;
|
||||||
|
std::cout << sb.as_cslice().c_str();
|
||||||
|
std::exit(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
auto parse_result = p.run(argc, argv);
|
||||||
|
if (parse_result.is_error()) {
|
||||||
|
std::cerr << "failed to parse options: " << parse_result.error().message().c_str() << "\n";
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream *outs = &std::cout;
|
std::ostream *outs = &std::cout;
|
||||||
|
@ -119,8 +105,8 @@ int main(int argc, char* const argv[]) {
|
||||||
|
|
||||||
std::vector<std::string> sources;
|
std::vector<std::string> sources;
|
||||||
|
|
||||||
while (optind < argc) {
|
for (const char* in_filename : parse_result.ok()) {
|
||||||
sources.push_back(std::string(argv[optind++]));
|
sources.emplace_back(in_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
funC::read_callback = funC::fs_read_callback;
|
funC::read_callback = funC::fs_read_callback;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue