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
205
206
207
208
|
/*
* Main entry point of the bootstrap compiler.
*
* Copyright © 2025 Samuel Lidén Borell <samuel@kodafritt.se>
*
* SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compiler.h"
#include "token.h"
#if defined(_WIN32) || defined(_WIN64)
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif
#define PATH_LIMIT 512
static size_t ifacedir_len = 0;
static const char *ifacedir = NULL;
static size_t rootdir_len = 0;
static const char *rootdir;
static FILE *f = NULL;
static const char *output_filename = NULL;
const char *current_filename = NULL;
static char *last_alloced_filename = NULL;
unsigned current_line = 0;
NORETURN void error(const char *s)
{
error_len(s, NULL, 0);
}
NORETURN void error_str(const char *s, const char *arg)
{
error_len(s, arg, arg ? strlen(arg) : 0);
}
NORETURN void error_len(const char *s, const char *arg, size_t arglen)
{
assert(arglen <= INT_MAX);
fprintf(stderr, "%s:%u: %s%s%.*s\n", current_filename, current_line,
s,
arg ? ": " : "",
(int)arglen, arg);
if (output_filename) {
remove(output_filename);
}
exit(EXIT_FAILURE);
}
NORETURN void error_token(const char *s, const struct LexemeInfo *li)
{
error_len(s, li->string, li->len);
}
NORETURN void error_ident(const char *s, const struct Ident *ident)
{
error_len(s, ident->node.name, ident->node.length);
}
NORETURN void error_var(const char *s, const struct Var *var)
{
error_ident(s, &var->ident);
}
void warning(const char *s)
{
fprintf(stderr, "%s:%u: warning: %s\n",
current_filename, current_line, s);
}
static void open_file(const char *dir, size_t dirlen, const char *filename)
{
size_t filename_len, total_len;
char *fullpath;
filename_len = strlen(filename);
total_len = dirlen + 1 + filename_len + 1;
if (total_len > PATH_LIMIT) {
FAIL("filename too long");
}
fullpath = malloc(total_len);
NO_NULL(fullpath);
/* Root dir */
memcpy(fullpath, dir, dirlen);
fullpath[dirlen] = DIRSEP;
/* Source file */
memcpy(fullpath+dirlen+1, filename, filename_len);
replacechar(fullpath+dirlen+1, '/', DIRSEP, filename_len);
fullpath[dirlen+1+filename_len] = '\0';
f = fopen(fullpath, "rb");
if (!f) {
perror(fullpath);
exit(EXIT_FAILURE);
}
last_alloced_filename = fullpath;
current_filename = fullpath;
current_line = 0;
}
static void close_file(void)
{
assert(f != NULL);
NO_NEG(fclose(f));
f = NULL;
current_filename = NULL;
current_line = 0;
}
static void parse_core_interface(void)
{
/* TODO rename core_wip.slul to core.slul once it's API-stable */
open_file(ifacedir, ifacedir_len, "core_wip.slul");
/* TODO forbid Uppercase initial letter in interfaces?
or at least don't create an implicit class. */
parse_file(f, "core_wip.slul");
builtins_bind();
close_file();
}
static void parse(void)
{
unsigned i;
module_start();
/* TODO Decide on a file name here. In particular,
check that "sources.index" is not used for anything else. */
open_file(rootdir, rootdir_len, "sources.index");
parse_source_index(f);
close_file();
free(last_alloced_filename);
parse_core_interface();
interfaces_done = true;
for (i = 0; i < num_sources; i++) {
open_file(rootdir, rootdir_len, sources[i]);
parse_file(f, path_basename(sources[i]));
close_file();
}
num_sources = 0;
interfaces_done = false;
}
static void set_rootdir(const char *rootdir_arg)
{
rootdir_len = strlen(rootdir_arg);
if (rootdir_len+2 >= PATH_LIMIT) FAIL("source path too long");
rootdir = rootdir_arg;
}
static void reset_sources_index(void)
{
num_sources = 0;
}
int main(int argc, char **argv)
{
enum {
IFACEDIR_ARG = 1,
C_OUTPUT_ARG = 2,
SOURCE_DIR_ARG = 3
};
int i;
if (argc < 3) {
fprintf(stderr, "usage: "
"stage1 <interface-dir> <_gen.c-file> <source-dir>...\n");
return EXIT_FAILURE;
}
ifacedir = argv[IFACEDIR_ARG];
ifacedir_len = strlen(ifacedir);
/* Parse each module. This is a hack, that just throws everything
together in a single namespace (which is incorrect, but works
for compiling the SLUL compiler). */
modules = NULL;
for (i = SOURCE_DIR_ARG; i < argc; i++) {
set_rootdir(argv[i]);
parse();
reset_sources_index();
}
/* TODO should the bootstrap compiler support interfaces?
- that way, the interface/module can be tested early
- that way, the backend and common functions can be
separate modules, that can be re-used.
*/
/* TODO should each file be parsed in a separate namespace?
- that way, each file can have different dependencies.
- that way, each file can have local functions.
*/
output_filename = argv[C_OUTPUT_ARG];
emit_c_code(output_filename);
return EXIT_SUCCESS;
}
|