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
|
#include "../core/state.h"
#include "../core/interpret.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
int read_file(const char *file_path, char **out_buffer, size_t *out_length) {
assert(out_buffer != NULL && *out_buffer == NULL);
assert(file_path != NULL);
const size_t CHUNK_SIZE = 1024;
FILE *fp = fopen(file_path, "r");
if (fp == NULL) {
return -1;
}
char *buffer = NULL;
size_t allocated = 0;
size_t used = 0;
while (true) {
// Grow buffer, if needed.
if (used + CHUNK_SIZE > allocated) {
// Grow exponentially to guarantee O(log(n)) performance.
allocated = (allocated == 0) ? CHUNK_SIZE : allocated * 2;
// Overflow check. Some ANSI C compilers may optimize this away, though.
if (allocated <= used) {
free(buffer);
fclose(fp);
errno = EOVERFLOW;
return -1;
}
char *temp = realloc(buffer, allocated);
if (temp == NULL) {
int old_errno = errno;
free(buffer); // free() may not set errno
fclose(fp); // fclose() may set errno
errno = old_errno;
return -1;
}
buffer = temp;
}
size_t nread = fread(buffer + used, 1, CHUNK_SIZE, fp);
if (nread == 0) {
// End-of-file or errnor has occured.
// FIXME: Should we be checking (nread < CHUNK_SIZE)?
// https://stackoverflow.com/a/39322170
break;
}
used += nread;
}
if (ferror(fp)) {
int old_errno = errno;
free(buffer); // free() may not set errno
fclose(fp); // fclose() may set errno
errno = old_errno;
return -1;
}
// Reallocate to optimal size.
char *temp = realloc(buffer, used + 1);
if (temp == NULL) {
int old_errno = errno;
free(buffer); // free() may not set errno
fclose(fp); // fclose() may set errno
errno = old_errno;
return -1;
}
buffer = temp;
// Null-terminate the buffer. Note that buffers may still contain \0,
// so strlen(buffer) == length may not be true.
buffer[used] = '\0';
// Return buffer.
*out_buffer = buffer;
if (out_length != NULL) {
*out_length = used;
}
fclose(fp);
return 0;
}
static void run_file(SandState *S, const char *filename) {
char *source = NULL;
size_t source_length = 0;
if (read_file(filename, &source, &source_length) < 0) {
fprintf(stderr, "Error reading %s: %s\n", filename, strerror(errno));
exit(74);
}
SandInterpretResult result = sand_interpret(S, filename, source, source_length);
free(source); // At this point it should be okay to invalidate all references to the source code.
if (result == SAND_INTERPRET_COMPILE_ERROR) {
exit(65);
} else if (result == SAND_INTERPRET_RUNTIME_ERROR) {
exit(70);
}
}
static void repl(SandState *S) {
char *line = NULL;
size_t line_capacity = 0;
ssize_t line_length = 0;
unsigned prompt_nr = 1;
while (true) {
printf("> ");
fflush(stdout);
// At this point the memory at `line` is reused, invalidating all existing references.
if ((line_length = getline(&line, &line_capacity, stdin)) < 0) {
fputc('\n', stdout);
break; // No more input - leave REPL loop.
}
// We need a dummy filename.
char filename[64];
snprintf(filename, sizeof(filename), "<repl %u>", prompt_nr);
prompt_nr += 1;
sand_interpret(S, filename, line, line_length);
}
free(line);
}
static void print_help(FILE *stream, const char *argv0) {
fprintf(stream, "Usage: %s <filename>\n", argv0);
fprintf(stream, " %s --help\n", argv0);
fprintf(stream, " %s\n\n", argv0);
fprintf(stream, "When called without arguments, runs a REPL.\n");
}
static void print_handler(const char *message, size_t message_length) {
fprintf(stdout, message, message_length);
}
void diagnostic_handler(const SandLocation *location, SandDiagnosticLevel level, const char *message, size_t message_length) {
sand_print_location(stderr, location);
switch (level) {
case SAND_DIAGNOSTIC_INFO: fprintf(stderr, ": info"); break;
case SAND_DIAGNOSTIC_WARNING: fprintf(stderr, ": warning"); break;
case SAND_DIAGNOSTIC_ERROR: fprintf(stderr, ": error"); break;
}
fprintf(stderr, ": %.*s\n", (int)message_length, message);
}
int main(int argc, char *argv[]) {
SandConfig config = {
.print_handler = print_handler,
.diagnostic_handler = diagnostic_handler,
};
SandState state = sand_create_state(config);
// TEMP
SandLocation loc = {
.filename = "fake.sand",
.start_line = 0,
.start_column = 0,
.end_line = 0,
.end_column = 5,
};
sand_print_diagnostic(&state, &loc, SAND_DIAGNOSTIC_INFO, "This is a test info message");
sand_print_diagnostic(&state, &loc, SAND_DIAGNOSTIC_WARNING, "oh no the number is %d", 5);
sand_print_diagnostic(&state, &loc, SAND_DIAGNOSTIC_ERROR, "gaide gad error here %d", 5);
if (argc == 1) {
repl(&state);
} else if (argc == 2 && strcmp(argv[1], "--help") == 0) {
print_help(stdout, argv[0]);
exit(0);
} else if (argc == 2) {
run_file(&state, argv[1]);
} else {
print_help(stderr, argv[0]);
exit(64);
}
sand_destroy_state(&state);
return 0;
}
|