public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
e5199a76fad28200b7ed31856dde627d98555f04 blob 3385 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
 
/*
 * 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 <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lexer.h"
#include "string_view.h"

typedef struct cli_args
{
    int argc;
    char **argv;
} cli_args_t;

char *
cli_args_shift(cli_args_t *args);

typedef struct cli_opts
{
    // TODO: create man page instruction for --dump-tokens option
    bool dump_tokens;
    char *file_path;
} cli_opts_t;

void
print_usage(FILE *stream, char *prog);

string_view_t
read_entire_file(char *file_path);

int
main(int argc, char **argv)
{
    cli_args_t args = { .argc = argc, .argv = argv };
    cli_opts_t opts = { 0 };

    char *prog = cli_args_shift(&args);

    if (argc != 3) {
        print_usage(stderr, prog);
        return EXIT_FAILURE;
    }

    for (char *arg = cli_args_shift(&args); arg != NULL; arg = cli_args_shift(&args)) {
        if (strcmp(arg, "--dump-tokens") == 0) {
            opts.dump_tokens = true;
        } else {
            opts.file_path = arg;
        }
    }

    if (!opts.dump_tokens) {
        print_usage(stderr, prog);
        return EXIT_FAILURE;
    }

    string_view_t file_content = read_entire_file(opts.file_path);

    // TODO: missing integration test for lexer tokenizing
    lexer_t lexer = { 0 };
    lexer_init(&lexer, file_content);

    token_t token = { 0 };
    lexer_next_token(&lexer, &token);
    while (token.kind != TOKEN_EOF) {
        printf("%s:%lu:%lu: <%s>\n",
               opts.file_path,
               token.location.row + 1,
               (token.location.offset - token.location.bol) + 1,
               token_kind_to_cstr(token.kind));
        lexer_next_token(&lexer, &token);
    }

    free(file_content.chars);

    return EXIT_SUCCESS;
}

char *
cli_args_shift(cli_args_t *args)
{
    if (args->argc == 0)
        return NULL;
    --(args->argc);
    return *(args->argv)++;
}

void
print_usage(FILE *stream, char *prog)
{
    fprintf(stream, "usage: %s <source.0> --dump-tokens\n", prog);
}

string_view_t
read_entire_file(char *file_path)
{
    FILE *stream = fopen(file_path, "rb");

    if (stream == NULL) {
        fprintf(stderr, "Could not open file %s: %s\n", file_path, strerror(errno));
        exit(EXIT_FAILURE);
    }

    string_view_t file_content = { 0 };

    fseek(stream, 0, SEEK_END);
    file_content.size = ftell(stream);
    fseek(stream, 0, SEEK_SET);

    file_content.chars = (char *)malloc(file_content.size);

    if (file_content.chars == NULL) {
        fprintf(stderr, "Could not read file %s: %s\n", file_path, strerror(errno));
        exit(EXIT_FAILURE);
    }

    fread(file_content.chars, 1, file_content.size, stream);
    fclose(stream);

    return file_content;
}
debug log:

solving e5199a7 ...
found e5199a7 in http://lists.johnnyrichard.com/olang/20240219013843.15707-4-johnny@johnnyrichard.com/ ||
	http://lists.johnnyrichard.com/olang/20240219011835.14769-3-johnny@johnnyrichard.com/ ||
	http://lists.johnnyrichard.com/olang/20240219012457.15107-4-johnny@johnnyrichard.com/
found 33ac945 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 33ac94580d086b4006dd3cb818fc238157b6f971	src/0c.c

applying [1/3] http://lists.johnnyrichard.com/olang/20240219013843.15707-4-johnny@johnnyrichard.com/
diff --git a/src/0c.c b/src/0c.c
index 33ac945..e5199a7 100644

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

skipping http://lists.johnnyrichard.com/olang/20240219011835.14769-3-johnny@johnnyrichard.com/ for e5199a7
skipping http://lists.johnnyrichard.com/olang/20240219012457.15107-4-johnny@johnnyrichard.com/ for e5199a7
index at:
100644 e5199a76fad28200b7ed31856dde627d98555f04	src/0c.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