Improve interpreter input handling

This commit is contained in:
Nikita Lisitsa 2026-03-18 18:48:01 +03:00
parent 25672d3f05
commit ae01f53d15

View file

@ -97,36 +97,85 @@ int main(int argc, char ** argv)
std::vector<std::string> filenames; std::vector<std::string> filenames;
std::vector<ast::statement_list_ptr> parsed; std::vector<ast::statement_list_ptr> parsed;
bool no_more_options = false;
for (int arg = 1; arg < argc; ++arg) for (int arg = 1; arg < argc; ++arg)
{ {
if (std::strcmp(argv[arg], "-d") == 0 || std::strcmp(argv[arg], "--dump") == 0) if (argv[arg][0] == 0)
continue;
if (std::strcmp(argv[arg], "--") == 0)
{ {
dump = true; no_more_options = true;
continue; continue;
} }
if (std::strcmp(argv[arg], "-t") == 0 || std::strcmp(argv[arg], "--trace") == 0) if (!no_more_options)
{ {
context.trace = true; if (argv[arg][0] == '-' && argv[arg][1] != 0 && argv[arg][1] != '-')
continue; {
for (char * p = argv[arg] + 1; *p != 0; ++p)
{
if (*p == 'd')
dump = true;
else if (*p == 't')
context.trace = true;
else if (*p == 'p')
dump_ast = true;
else if (*p == 'i')
dump_ir = true;
else if (*p == 'j')
jit = true;
else
{
std::cerr << "Unknown option '" << *p << "'\n";
return EXIT_FAILURE;
}
}
continue;
}
if (std::strcmp(argv[arg], "--dump") == 0)
{
dump = true;
continue;
}
if (std::strcmp(argv[arg], "--trace") == 0)
{
context.trace = true;
continue;
}
if (std::strcmp(argv[arg], "--print") == 0)
{
dump_ast = true;
continue;
}
if (std::strcmp(argv[arg], "--ir") == 0)
{
dump_ir = true;
continue;
}
if (std::strcmp(argv[arg], "--jit") == 0)
{
jit = true;
continue;
}
} }
if (std::strcmp(argv[arg], "-p") == 0 || std::strcmp(argv[arg], "--print") == 0) if (!std::filesystem::exists(argv[arg]))
{ {
dump_ast = true; std::cerr << "Error: input file \"" << argv[arg] << "\" does not exist\n";
continue; return EXIT_FAILURE;
} }
if (std::strcmp(argv[arg], "-i") == 0 || std::strcmp(argv[arg], "--ir") == 0) if (std::filesystem::is_directory(argv[arg]))
{ {
dump_ir = true; std::cerr << "Error: input file \"" << argv[arg] << "\" is a directory\n";
continue; return EXIT_FAILURE;
}
if (std::strcmp(argv[arg], "-j") == 0 || std::strcmp(argv[arg], "--jit") == 0)
{
jit = true;
continue;
} }
try try