From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 2N+KCK3g+WV96gAAqHPOHw:P1 (envelope-from ) for ; Tue, 19 Mar 2024 19:59:57 +0100 Received: from aspmx1.migadu.com ([2001:41d0:403:4876::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id 2N+KCK3g+WV96gAAqHPOHw (envelope-from ) for ; Tue, 19 Mar 2024 19:59:57 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; none Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 0BF857BF5F for ; Tue, 19 Mar 2024 19:59:57 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=sZJdJvoGC38/+k2CdjC3kRAhMwIbul0YO5oK+dtZBfc=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1710874796; v=1; b=hXlzVRriOZEOJrWQr+IknnDV8mV7DcfKGj2SkpIBtwDLYFke6DklFVuRdOWprymK3NtAeoCK b4GRvSjJ23lIuhcPIr4MCFBNh/i/1TBM20VpRWN8B2u9CuFcR4YRotnEhksqyAYsXU4TRfbWmqh MeGD1GHmfHd3Y1pKMCS4wqqHLXuxtJRTIsTNspDPnvPYYlzDjP2mkB6X9cPyFcnUn0AEK2MW972 POVLLbnqrKhbbZ9o8ol7BLSwWy8AJW3vN/4xIAMEF/1q49/XNmVEitA62Hy3glBf5g/jwC9bmdR QRi50iGdC/j+/EHZIkCIZDOxw6ukaj5uU4DnCOl7wPicw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id CBA9E20106 for ; Tue, 19 Mar 2024 18:59:56 +0000 (UTC) Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [IPv6:2001:41d0:203:375::b6]) by mail-a.sr.ht (Postfix) with ESMTPS id 2A8C8200CD for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 19 Mar 2024 18:59:56 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1710874795; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PfSoQ46w65eFm+I2nBbcR8zyEak7uT8GDDn7KtE/E+U=; b=ytdgVpBuTzdIp5xb+ZaheJex9AX1Hp38mV+d/c/1dinHJOiEzVIVYNNuKvX/ZYo75Am1cL THHY2ngFELv7Lss0x6CVIc5nlrRVsaipP+nmsCpEo2GACnGmuqZT81KezucLgWlNK5T0br UV+GzTdEQGTTtYAEw2xgT1qCV5FGZUD4SiTmZCDDcB89dttpXT+9Sseo6noJQ9cVWtb99I 11UCg0R1IxndZTS2dznVlQHYI/5spKODNae0dGa42ChrCZ+JOaJHWYxvd/OR5PznFWkcze wsbxqQgDmDG0iAYUYr9WuxyHwP3Sej1FuY2YHQi+VdNZh7Z0uTT3qOOoVs6n6g== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Johnny Richard Subject: [PATCH olang v1 1/3] cli: refactor: move cli parsing to a separted file Date: Tue, 19 Mar 2024 20:57:18 +0100 Message-ID: <20240319195947.202414-2-johnny@johnnyrichard.com> In-Reply-To: <20240319195947.202414-1-johnny@johnnyrichard.com> References: <20240319195947.202414-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Sourcehut-Patchset-Status: UNKNOWN List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Scanner: mx10.migadu.com X-Migadu-Spam-Score: -4.00 X-Spam-Score: -4.00 X-Migadu-Queue-Id: 0BF857BF5F X-TUID: tOFJWfJlQ8Yw Signed-off-by: Johnny Richard --- src/cli.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cli.h | 50 ++++++++++++++++++++++++++++ src/main.c | 97 +++++------------------------------------------------- 3 files changed, 155 insertions(+), 89 deletions(-) create mode 100644 src/cli.c create mode 100644 src/cli.h diff --git a/src/cli.c b/src/cli.c new file mode 100644 index 0000000..3d1c936 --- /dev/null +++ b/src/cli.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "cli.h" +#include +#include +#include +#include + +static char * +cli_args_shift(cli_args_t *args); + +static void +cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args); + +cli_opts_t +cli_parse_args(int argc, char **argv) +{ + cli_args_t args = { .argc = argc, .argv = argv }; + cli_opts_t opts = { 0 }; + + opts.compiler_path = cli_args_shift(&args); + + char *arg = cli_args_shift(&args); + while (arg != NULL) { + if (strcmp(arg, "--dump-tokens") == 0) { + opts.options |= CLI_OPT_DUMP_TOKENS; + } else if (strcmp(arg, "--save-temps") == 0) { + opts.options |= CLI_OPT_SAVE_TEMPS; + } else if (strcmp(arg, "-o") == 0) { + cli_opts_parse_output(&opts, &args); + } else { + opts.file_path = arg; + } + arg = cli_args_shift(&args); + } + + if (opts.options & CLI_OPT_OUTPUT || opts.options & CLI_OPT_DUMP_TOKENS) { + return opts; + } + + cli_print_usage(stderr, opts.compiler_path); + exit(EXIT_FAILURE); + return opts; +} + +static char * +cli_args_shift(cli_args_t *args) +{ + if (args->argc == 0) + return NULL; + --(args->argc); + return *(args->argv)++; +} + +static void +cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args) +{ + assert(opts && "opts is required"); + assert(args && "args is required"); + + char *output_bin = cli_args_shift(args); + + if (output_bin == NULL) { + fprintf(stderr, "error: missing filename after '-o'\n"); + cli_print_usage(stderr, opts->compiler_path); + exit(EXIT_FAILURE); + } + + opts->options |= CLI_OPT_OUTPUT; + opts->output_bin = string_view_from_cstr(output_bin); +} + +void +cli_print_usage(FILE *stream, char *compiler_path) +{ + fprintf(stream, + "Usage: %s [options] file...\n" + "Options:\n" + " --dump-tokens\t\tDisplay lexer token stream\n" + " -o \t\tCompile program into a binary file\n" + " --save-temps\t\tKeep temp files used to compile program\n", + compiler_path); +} diff --git a/src/cli.h b/src/cli.h new file mode 100644 index 0000000..bd6d6a7 --- /dev/null +++ b/src/cli.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2024 olang maintainers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef CLI_H +#define CLI_H +#include "string_view.h" +#include +#include + +typedef struct cli_args +{ + int argc; + char **argv; +} cli_args_t; + +typedef struct cli_opts +{ + uint32_t options; + char *compiler_path; + char *file_path; + string_view_t output_bin; +} cli_opts_t; + +typedef enum +{ + CLI_OPT_DUMP_TOKENS = 1 << 0, + CLI_OPT_OUTPUT = 1 << 1, + CLI_OPT_SAVE_TEMPS = 1 << 2 +} cli_opt_t; + +cli_opts_t +cli_parse_args(int argc, char **argv); + +void +cli_print_usage(FILE *stream, char *compiler_path); + +#endif /* CLI_H */ diff --git a/src/main.c b/src/main.c index f6d49f0..f5b12e4 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,7 @@ #include #include "arena.h" +#include "cli.h" #include "codegen_linux_x86_64.h" #include "lexer.h" #include "parser.h" @@ -30,33 +31,6 @@ // TODO: find a better solution to define the arena capacity #define ARENA_CAPACITY (1024 * 1024) -typedef struct cli_args -{ - int argc; - char **argv; -} cli_args_t; - -char * -cli_args_shift(cli_args_t *args); - -typedef enum -{ - CLI_OPT_DUMP_TOKENS = 1 << 0, - CLI_OPT_OUTPUT = 1 << 1, - CLI_OPT_SAVE_TEMPS = 1 << 2 -} cli_opt_t; - -typedef struct cli_opts -{ - uint32_t options; - char *compiler_path; - char *file_path; - string_view_t output_bin; -} cli_opts_t; - -void -print_usage(FILE *stream, char *compiler_path); - void handle_dump_tokens(cli_opts_t *opts); @@ -69,29 +43,14 @@ print_token(char *file_path, token_t *token); string_view_t read_entire_file(char *file_path, arena_t *arena); -void -cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args); - int main(int argc, char **argv) { - cli_args_t args = { .argc = argc, .argv = argv }; - cli_opts_t opts = { 0 }; - - opts.compiler_path = cli_args_shift(&args); - - char *arg = cli_args_shift(&args); - while (arg != NULL) { - if (strcmp(arg, "--dump-tokens") == 0) { - opts.options |= CLI_OPT_DUMP_TOKENS; - } else if (strcmp(arg, "--save-temps") == 0) { - opts.options |= CLI_OPT_SAVE_TEMPS; - } else if (strcmp(arg, "-o") == 0) { - cli_opts_parse_output(&opts, &args); - } else { - opts.file_path = arg; - } - arg = cli_args_shift(&args); + cli_opts_t opts = cli_parse_args(argc, argv); + + if (opts.options & CLI_OPT_DUMP_TOKENS) { + handle_dump_tokens(&opts); + return EXIT_SUCCESS; } if (opts.options & CLI_OPT_OUTPUT) { @@ -104,24 +63,14 @@ main(int argc, char **argv) return EXIT_SUCCESS; } - print_usage(stderr, opts.compiler_path); return EXIT_FAILURE; } -char * -cli_args_shift(cli_args_t *args) -{ - if (args->argc == 0) - return NULL; - --(args->argc); - return *(args->argv)++; -} - void handle_dump_tokens(cli_opts_t *opts) { if (opts->file_path == NULL) { - print_usage(stderr, opts->compiler_path); + cli_print_usage(stderr, opts->compiler_path); exit(EXIT_FAILURE); } @@ -146,7 +95,7 @@ void handle_codegen_linux_x86_64(cli_opts_t *opts) { if (opts->file_path == NULL) { - print_usage(stderr, opts->compiler_path); + cli_print_usage(stderr, opts->compiler_path); exit(EXIT_FAILURE); } @@ -188,18 +137,6 @@ handle_codegen_linux_x86_64(cli_opts_t *opts) arena_free(&arena); } -void -print_usage(FILE *stream, char *compiler_path) -{ - fprintf(stream, - "Usage: %s [options] file...\n" - "Options:\n" - " --dump-tokens\t\tDisplay lexer token stream\n" - " -o \t\tCompile program into a binary file\n" - " --save-temps\t\tKeep temp files used to compile program\n", - compiler_path); -} - string_view_t read_entire_file(char *file_path, arena_t *arena) { @@ -231,24 +168,6 @@ read_entire_file(char *file_path, arena_t *arena) return file_content; } -void -cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args) -{ - assert(opts && "opts is required"); - assert(args && "args is required"); - - char *output_bin = cli_args_shift(args); - - if (output_bin == NULL) { - fprintf(stderr, "error: missing filename after '-o'\n"); - print_usage(stderr, opts->compiler_path); - exit(EXIT_FAILURE); - } - - opts->options |= CLI_OPT_OUTPUT; - opts->output_bin = string_view_from_cstr(output_bin); -} - static void print_token(char *file_path, token_t *token) { -- 2.44.0