-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.c
More file actions
278 lines (231 loc) · 6.47 KB
/
interpreter.c
File metadata and controls
278 lines (231 loc) · 6.47 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "false.h"
#include "utils/file.h"
#include <errno.h>
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct position {
size_t line;
size_t ch;
const char *bol;
const char *eol;
};
/// Determine line and character for @c pos.
static struct position position_of(const struct config config, const char *pos)
{
struct position position;
bool advance;
position.line = 0;
position.ch = 0;
position.bol = NULL;
position.eol = NULL;
if (!pos) {
return position;
}
// line n: ".....X....."
// ^ ^ ^
// BOL POS EOL/EOF
advance = true;
for (const char *p = config.str; p <= pos; ++p) {
if (advance) {
advance = false;
position.line++;
position.ch = 0;
// Beginning of line that contains symbol at position "pos".
position.bol = p;
}
advance = *p == '\n';
position.ch++;
}
// End of line that contains symbol at position "pos".
for (position.eol = pos; position.eol < strchr(config.str, 0) && *position.eol != '\n'; ++position.eol) {
}
return position;
}
static void fatal(const struct config config, const char *pos, const char *msg)
{
if (!pos) {
// Usage error.
fprintf(stderr
, "%s: error: %s\n"
, config.argv[0]
, msg);
} else {
struct position position = position_of(config, pos);
int len = (int)(position.eol - position.bol);
int off = (int)(pos - position.bol + 1);
fprintf(stderr
, "%s:%zu:%zu: error: %s\n"
"%*.*s\n"
"%*.*s\n"
, config.argv[0]
, position.line
, position.ch
, msg
, len, len, position.bol
, off, off, "^");
}
}
static void log_trace(const struct config config, wchar_t wc, const char *pos)
{
(void)config;
(void)pos;
printf("# symbol %lc\n", wc);
}
static void log_stack(const struct config config, const char *op, const char *dump)
{
(void)config;
printf("# %6s %s\n", op, dump);
}
static void emit_number(int number)
{
printf("%d", number);
}
static void emit_wchar(wchar_t wc)
{
putwchar(wc);
}
static void emit_char(char c)
{
putchar(c);
}
static const char *buffered_input;
static int input(void)
{
if (buffered_input) {
if (*buffered_input) {
return *buffered_input++;
}
buffered_input = NULL;
return '\n';
}
return getchar();
}
static void flush(void)
{
if (buffered_input) {
buffered_input = NULL;
} else {
fflush(stdin);
}
}
/// Skip magic header.
static const char * skip_magic(const char * str)
{
if (str[0] == '#' && str[1] == '!') {
const char *nl = strchr(str, '\n');
if (nl) {
return nl;
}
}
return str;
}
static void usage(void)
{
printf(
"false_int [OPTIONS...] FILE [ARGUMENTS...]\n"
"\n"
"Interpret a 'FALSE' program.\n"
"\n"
"Options:\n"
" -e, --extensions Enable extensions.\n"
" -h, --help Print this message and exit.\n"
" -i, --input STRING Input string.\n"
" -v, --verbose Print debug messages.\n"
"\n"
"Upto 25 numeric arguments may be given. These are passed to the program\n"
"using the variables `b..z' while `a' holds the count of arguments. This\n"
"feature is documented in the False1.2b portable interpreter.\n"
"\n"
"Input may be given on the command line (in a similar manner as the Amiga\n"
"implementation) with a single `--input' string, which is read before any\n"
"further input from stdin.\n"
"\n"
"Extended (Unicode) characters are processed according to current locale.\n"
"However, 'B' and 'O' are supported for 'flush' and 'pick' operations, as\n"
"implemented in the False1.2b portable interpreter.\n"
);
}
static int drop(int i, int argc, char **argv)
{
argc--;
memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(char *));
return argc;
}
int main(int argc, char **argv)
{
const char *filename = NULL;
struct config config;
char *buf;
int r;
config.extensions = false;
config.fatal = fatal;
config.log_trace = NULL;
config.log_stack = NULL;
config.emit_number = emit_number;
config.emit_wchar = emit_wchar;
config.emit_char = emit_char;
config.input = input;
config.flush = flush;
// Skip this executable name.
argc--;
argv++;
// Filter out arguments.
for (int i = 0; i < argc; ) {
const char *arg = argv[i];
if (!strcmp(arg, "--")) {
argc = drop(i, argc, argv);
break;
} else if (!strcmp(arg, "-e") || !strcmp(arg, "--extensions")) {
argc = drop(i, argc, argv);
config.extensions = true;
} else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
usage();
return EXIT_SUCCESS;
} else if (!strcmp(arg, "-i") || !strcmp(arg, "--input")) {
argc = drop(i, argc, argv);
if (!buffered_input && i < argc) {
buffered_input = argv[i];
argc = drop(i, argc, argv);
} else {
usage();
return EXIT_FAILURE;
}
} else if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose")) {
argc = drop(i, argc, argv);
config.log_trace = log_trace;
config.log_stack = log_stack;
} else if (arg[0] == '-') {
usage();
return EXIT_FAILURE;
} else if (filename) {
break;
} else {
filename = arg;
i++;
}
}
if (!filename) {
usage();
return EXIT_FAILURE;
}
r = file_read_fully(filename, &buf);
if (r < 0) {
errno = -r;
perror(filename);
return EXIT_FAILURE;
}
// Use locales specified in the environment.
setlocale(LC_ALL, "");
config.argc = argc;
config.argv = argv;
config.str = skip_magic(buf);
r = interpret(config);
free(buf);
if (r) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}