public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 1/3] cli: refactor: move cli parsing to a separted file
Date: Tue, 19 Mar 2024 20:57:18 +0100	[thread overview]
Message-ID: <20240319195947.202414-2-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240319195947.202414-1-johnny@johnnyrichard.com>

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 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 <https://www.gnu.org/licenses/>.
+ */
+#include "cli.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 <file>\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 <https://www.gnu.org/licenses/>.
+ */
+#ifndef CLI_H
+#define CLI_H
+#include "string_view.h"
+#include <stdint.h>
+#include <stdio.h>
+
+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 <string.h>
 
 #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 <file>\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


  reply	other threads:[~2024-03-19 18:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-19 19:57 [PATCH olang v1 0/3] fe: add compiler support to linux aarch64 Johnny Richard
2024-03-19 19:57 ` Johnny Richard [this message]
2024-03-23 20:18   ` [PATCH olang v1 1/3] cli: refactor: move cli parsing to a separted file Carlos Maniero
2024-03-24 16:22     ` Johnny Richard
2024-03-19 19:57 ` [PATCH olang v1 2/3] cli: add --sysroot and --arch options Johnny Richard
2024-03-19 19:57 ` [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch Johnny Richard
2024-03-19 19:00   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-03-23 20:18   ` [PATCH olang v1 3/3] codegen: add compiler support to linux aarch64 arch Carlos Maniero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240319195947.202414-2-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox