diff --git a/src/error.hxx b/src/error.hxx index c0e5da2..0003abf 100644 --- a/src/error.hxx +++ b/src/error.hxx @@ -39,4 +39,12 @@ namespace err { inline void undeclared(std::optional err) { std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mundeclared varible '" << err.value() << "'\033[0m" << std::endl; } + + inline void nofile() { + std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mno input files\033[0m" << std::endl; + } + + inline void help() { + std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mhelp no implemented... just... uhm... look at main.cxx?\033[0m" << std::endl; + } } \ No newline at end of file diff --git a/src/main.cxx b/src/main.cxx index a191ee0..ad7ffa0 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -3,6 +3,7 @@ #include #include #include +#include #include "arena.hxx" #include "parser.hxx" @@ -10,15 +11,52 @@ #include "tokenization.hxx" int main(int argc, char* argv[]) { - if (argc != 2) { - std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mno input files\033[0m" << std::endl; // yes i spent 10 minutes to emulate clangs output... + std::string outfile = "out.asm"; + std::optional infile; + int opt; + struct option long_options[] = { + {"help", no_argument, nullptr, 'h'}, + {"version", no_argument, nullptr, 'v'}, + {"output", required_argument, nullptr, 'o'}, + {nullptr, 0, nullptr, 0} + }; + + while (true) { + int option_index = 0; + opt = getopt_long(argc, argv, "hvo:", long_options, &option_index); + if (opt == -1) break; + + switch (opt) { + case 'h': + err::help(); + break; + case 'v': + std::cout << "Version 0.0.1a\n"; + break; + case 'o': + outfile = optarg; + break; + default: + err::help(); + return EXIT_FAILURE; + } + } + + if (optind < argc) { + infile = argv[optind]; + } else { + err::nofile(); return EXIT_FAILURE; } + if (!infile.has_value()) { + err::nofile(); + return EXIT_FAILURE; + } std::string contents; { std::stringstream contents_stream; - std::fstream input(argv[1], std::ios::in); + std::fstream input(infile.value(), std::ios::in); contents_stream << input.rdbuf(); contents = contents_stream.str(); } @@ -37,11 +75,13 @@ int main(int argc, char* argv[]) { Generator generator(prog.value()); { - std::fstream output("out.asm", std::ios::out); + std::fstream output(outfile, std::ios::out); output << generator.gen_prog(); } - system("nasm -felf64 out.asm"); + std::stringstream nasmcmd; + nasmcmd << "nasm -felf64 " << outfile; + system(nasmcmd.str().c_str()); system("ld -o out out.o"); return EXIT_SUCCESS;