public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
1765ecf01e3db8c179321833caa42720242fe43c blob 4599 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
 
/*
 * 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);

static void
cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args);

static void
cli_opts_parse_sysroot(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, "--dump-ast") == 0) {
            opts.options |= CLI_OPT_DUMP_AST;
        } 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 if (strcmp(arg, "--arch") == 0) {
            opts.options |= CLI_OPT_ARCH;
            cli_opts_parse_arch(&opts, &args);
        } else if (strcmp(arg, "--sysroot") == 0) {
            opts.options |= CLI_OPT_SYSROOT;
            cli_opts_parse_sysroot(&opts, &args);
        } else {
            opts.filepath = arg;
        }
        arg = cli_args_shift(&args);
    }

    if (opts.options & CLI_OPT_OUTPUT || opts.options & CLI_OPT_DUMP_TOKENS ||
        opts.options & CLI_OPT_DUMP_AST) {
        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);
}

static void
cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args) {
    assert(opts && "opts is required");
    assert(args && "args is required");

    char *arch = cli_args_shift(args);

    if (arch == NULL) {
        fprintf(
            stderr,
            "error: missing architecture for arg '--arch': "
            "available options (x86_64 | aarch64)\n"
        );
        cli_print_usage(stderr, opts->compiler_path);
        exit(EXIT_FAILURE);
    }

    opts->options |= CLI_OPT_ARCH;
    opts->arch = arch;
}

static void
cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args) {
    assert(opts && "opts is required");
    assert(args && "args is required");

    char *sysroot = cli_args_shift(args);

    if (sysroot == NULL) {
        fprintf(stderr, "error: missing sysroot arg '--sysroot'\n");
        cli_print_usage(stderr, opts->compiler_path);
        exit(EXIT_FAILURE);
    }

    opts->options |= CLI_OPT_SYSROOT;
    opts->sysroot = sysroot;
}

void
cli_print_usage(FILE *stream, char *compiler_path) {
    fprintf(
        stream,
        "Usage: %s [options] file...\n"
        "Options:\n"
        "  --dump-tokens    Display lexer token stream\n"
        "  --dump-ast       Display ast tree to stdout\n"
        "  --arch <arch>    Binary arch: default to x86_64 (x86_64 | aarch64)\n"
        "  --sysroot <dir>  System root dir where the GNU Assembler and GNU "
        "Linker are located: default to '/'\n"
        "  -o <file>        Compile program into a binary file\n"
        "  --save-temps     Keep temp files used to compile program\n",
        compiler_path
    );
}
debug log:

solving 1765ecf ...
found 1765ecf in http://lists.johnnyrichard.com/olang/20241010013318.222905-7-carlos@maniero.me/
found 1f30ea3 in http://lists.johnnyrichard.com/olang/20241010013318.222905-6-carlos@maniero.me/
found d57945a in http://lists.johnnyrichard.com/olang/20241010013318.222905-3-carlos@maniero.me/
found 2faf6ce in http://lists.johnnyrichard.com/olang/20241010013318.222905-2-carlos@maniero.me/
found 9d0f875 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 9d0f875a2236d45f7d3de7f6ff62aad9d51e7bf5	src/cli.c

applying [1/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-2-carlos@maniero.me/
diff --git a/src/cli.c b/src/cli.c
index 9d0f875..2faf6ce 100644


applying [2/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-3-carlos@maniero.me/
diff --git a/src/cli.c b/src/cli.c
index 2faf6ce..d57945a 100644


applying [3/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-6-carlos@maniero.me/
diff --git a/src/cli.c b/src/cli.c
index d57945a..1f30ea3 100644


applying [4/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-7-carlos@maniero.me/
diff --git a/src/cli.c b/src/cli.c
index 1f30ea3..1765ecf 100644

Checking patch src/cli.c...
Applied patch src/cli.c cleanly.
Checking patch src/cli.c...
Applied patch src/cli.c cleanly.
Checking patch src/cli.c...
Applied patch src/cli.c cleanly.
Checking patch src/cli.c...
Applied patch src/cli.c cleanly.

index at:
100644 1765ecf01e3db8c179321833caa42720242fe43c	src/cli.c

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