diff options
| author | Johnny Richard <johnny@johnnyrichard.com> | 2024-10-30 22:58:03 +0100 |
|---|---|---|
| committer | Johnny Richard <johnny@johnnyrichard.com> | 2025-12-14 09:53:52 +0100 |
| commit | 10bb8a05088f1d3bb24f7167f609b5f6fb0ba026 (patch) | |
| tree | 7a4b3f69a461301c45204ed856b61f92a7d42233 /src/obe.c | |
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src/obe.c')
| -rw-r--r-- | src/obe.c | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/obe.c b/src/obe.c new file mode 100644 index 0000000..38af876 --- /dev/null +++ b/src/obe.c @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2024 Johnny Richard <johnny@johnnyrichard.com> + * + * 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 <https://www.gnu.org/licenses/>. + */ +#include <config.h> +#include <obe/parser.h> +#include <obe/x86_64/codegen.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +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 <johnny@johnnyrichard.com>\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; +} |
