/* * Copyright (C) 2024 Johnny Richard * * SPDX-License-Identifier: LGPL-3.0-or-later * * This file is part of obe. * * obe is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) * any later version. * * obe is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with obe. If not, see . */ #include #include #include #include #include #include #include typedef struct obe_cli_args { int argc; char** argv; } obe_cli_args_t; typedef struct obe_cli_opts { uint32_t options; char* ir_file; char* program_path; } obe_cli_opts_t; typedef enum { OBE_CLI_OPT_HELP = 1, OBE_CLI_OPT_VERSION = 1 << 1, OBE_CLI_OPT_OUTPUT = 1 << 2, } cli_opt_t; obe_cli_opts_t obe_cli_parse_args(int argc, char** argv); static char* obe_cli_args_shift(obe_cli_args_t* args) { if (args->argc == 0) return NULL; --(args->argc); return *(args->argv)++; } void obe_cli_print_usage(FILE* stream) { fprintf(stream, "Usage: " PACKAGE_NAME " [OPTIONS] file...\n" "\n" "Options:\n" " -h, --help display the help and exit\n" " -v, --version output version information and exit\n" " -o, --output output gas assembly\n" "\n" "Report bugs to " PACKAGE_BUGREPORT "\n"); } void obe_cli_print_version(void) { printf(PACKAGE_STRING "\n" "\n" "Copyright (C) 2025 Olang project.\n" "This is free software; see the source for copying conditions. " "There is NO\n" "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR " "PURPOSE.\n" "\n" "Written by Johnny Richard \n"); } int main(int argc, char** argv) { obe_cli_opts_t opts = { 0 }; obe_cli_args_t args = { .argc = argc, .argv = argv }; opts.program_path = obe_cli_args_shift(&args); char* arg = obe_cli_args_shift(&args); while (arg != NULL) { if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { opts.options |= OBE_CLI_OPT_HELP; } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) { opts.options |= OBE_CLI_OPT_VERSION; } else if (strcmp(arg, "-o") == 0 || strcmp(arg, "--output") == 0) { opts.options |= OBE_CLI_OPT_OUTPUT; } else { opts.ir_file = arg; } arg = obe_cli_args_shift(&args); } if (opts.ir_file != NULL) { obe_arena_t arena = { 0 }; obe_lexer_t lexer = { 0 }; obe_lexer_init(&lexer, opts.ir_file); obe_parser_t parser = { 0 }; obe_parser_init(&parser, &lexer, &arena); obe_ir_translation_unit_t* tu = obe_parser_parse(&parser); obe_x86_64_codegen_emit(tu, stdout); return EXIT_SUCCESS; } if (opts.options & OBE_CLI_OPT_HELP) { obe_cli_print_usage(stdout); return EXIT_SUCCESS; } if (opts.options & OBE_CLI_OPT_VERSION) { obe_cli_print_version(); return EXIT_SUCCESS; } if (opts.options & OBE_CLI_OPT_OUTPUT) { return EXIT_SUCCESS; } obe_cli_print_usage(stderr); return EXIT_FAILURE; }