summaryrefslogtreecommitdiff
path: root/bootstrap/main.c
blob: cfb72a473dc2d5067fd745b2db905cc514edc1eb (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

/*

  main.c -- Entry point for LRL bootstrap transpiler

  Copyright © 2020 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "bootstrap.h"
#include <stdlib.h>
#include <string.h>

static FILE *output_file;
static const char *progname;
static const char *current_file;

void error_tok(const struct Token *tok, const char *s)
{
    error_linecol(tok->line, tok->column, s);
}

void error_linecol(int line, int column, const char *s)
{
    fprintf(stderr, "%s:%d:%d: %s\n", current_file, line, column, s);
}

NORETURN void out_of_memory(void)
{
    fprintf(stderr, "%s: Out of memory.\n", progname);
    abort();
}

NORETURN void internal_error(int error_id)
{
    fprintf(stderr, "Internal Compiler Error %d\n", error_id);
    abort();
}

static void show_help(void)
{
    printf("usage: %s [OPTION]... FILE... -o OUTPUT\n"
"\n"
"LRL Bootstrap Transpiler. Outputs C or LLVM code (TODO decide which).\n"
"\n", progname);
}

static void invalid_option(const char *arg)
{
    fprintf(stderr, "%s: invalid option: %s\n", progname, arg);
}

static void missing_argument(const char *arg)
{
    fprintf(stderr, "%s: option '%s' requires an argument\n", progname, arg);
}

static int parse_files(int argc, char **argv, int decls_only)
{
    int optparse = 1;
    int has_files = 0;
    int ok = 1;
    int argleft;
    char **argp;

    /* Loop through all filename arguments */
    optparse = 1;
    for (argleft = argc-1, argp = argv+1; argleft; argleft--, argp++) {
        FILE *file;
        const char *arg = *argp;
        if (arg[0] == '-') {
            if (arg[1] == '\0') {
                /*ok &= parse_file(stdin, decls_only);*/
                fprintf(stderr, "%s: parsing from standard in is currently not supported\n", progname);
                ok = 0;
                has_files = 1;
                continue;
            } else if (optparse) {
                if (arg[1] == '-') {
                    if (arg[2] == '\0') optparse = 0;
                } else if (arg[1] == 'o') {
                    argleft--;
                    argp++;
                }
                continue;
            }
        }
        file = fopen(arg, "r");
        if (!file) {
            perror(arg);
            return 0;
        }
        current_file = arg;
        ok &= parse_file(file, decls_only);
        has_files = 1;
        fclose(file);
    }

    if (!has_files) {
        fprintf(stderr, "%s: no input files\n", progname);
        return 0;
    }
    return ok;
}

int main(int argc, char **argv)
{
    int optparse = 1;
    int argleft;
    char **argp;
    /*const char *output_filename = "a.out";*/
    const char *output_filename = "a_out.c";
    progname = argv[0];

    /* Parse option arguments (and skip filename arguments) */
    for (argleft = argc-1, argp = argv+1; argleft; argleft--, argp++) {
        const char *arg = *argp;
        if (arg[0] == '-') {
            if (arg[1] == '\0' || !optparse) continue; /* stdin/filename argument */
            if (arg[1] == '-') {
                /* Long option */
                if (arg[2] == '\0') { optparse = 0; }
                else if (!strcmp(arg+2, "help")) {
                    show_help();
                    return EXIT_SUCCESS;
                } else {
                    invalid_option(arg);
                    return 2;
                }
            } else if (arg[2] != '\0') {
                /* Long option with only one dash (or a combination of
                   multiple short options, but that is not supported) */
                invalid_option(arg);
                return 2;
            } else {
                /* Short option */
                switch (arg[1]) {
                case 'o':
                    /* Output file */
                    if (!--argleft) {
                        missing_argument(arg);
                        return 2;
                    }
                    output_filename = *(++argp);
                    break;
                default:
                    invalid_option(arg);
                    return 2;
                }
            }
        }
    }

    /* First, parse all declarations */
    init_builtins();
fprintf(stderr, "-- stage1\n");
    if (!parse_files(argc, argv, 1)) {
        return EXIT_FAILURE;
    }

    /* Second, output the declarations, taking
       dependencies into account */
fprintf(stderr, "-- stage2\n");
    if (strcmp(output_filename, "-")) {
        output_file = fopen(output_filename, "w");
        if (!output_file) {
            perror(output_filename);
            return EXIT_FAILURE;
        }
    } else {
        output_file = stdout;
    }
    output_set_file(output_file);

    output_dependencies();

    /* Finally, parse all source files again and output C source / LLVM IR (or an executable using LLVM?) */
fprintf(stderr, "-- stage3\n");
    if (!parse_files(argc, argv, 0)) {
        fclose(output_file);
        return EXIT_FAILURE;
    }
    if (output_file != stdout) {
        fclose(output_file);
    }
    return EXIT_SUCCESS;
}