summaryrefslogtreecommitdiff
path: root/src/obe.c
blob: 38af87669624f7728908421ffc9996ff93b13d0c (plain)
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
/*
 * 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;
}