forked from advancingdragon/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.h
245 lines (219 loc) · 4.47 KB
/
support.h
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *output_name = NULL;
FILE *output = NULL;
char *source = NULL;
int pos = 0;
int line = 1;
int test_flag = 0;
char *token = NULL;
void skip_whitespace(void)
{
while (source[pos] == '\x20' || source[pos] == '\t' ||
source[pos] == '\r' || source[pos] == '\n') {
// increment line counter when new line reached
if (source[pos] == '\n') {
line++;
}
pos++;
}
}
void make_token(int start_pos)
{
int length = pos - start_pos;
free(token);
token = malloc(length + 1);
token[length] = '\0';
memcpy(token, &source[start_pos], length);
}
// emits the currently recognized token
void emit_token(void)
{
int i;
// strings are converted to C format
if (token[0] == '\'') {
fprintf(output, "\"");
for (i = 1; token[i] != '\0' && token[i] != '\''; i++) {
switch (token[i]) {
case '\"':
fprintf(output, "\\\"");
break;
case '\\':
fprintf(output, "\\\\");
break;
default:
fprintf(output, "%c", token[i]);
break;
}
}
fprintf(output, "\"");
return;
}
// if token is not a string, emit as-is
fprintf(output, "%s", token);
}
void emit(const char *str)
{
fprintf(output, "%s", str);
}
void emit_nl(void)
{
fprintf(output, "\n");
}
void read_literal(const char *literal)
{
int entry_pos;
int i;
skip_whitespace();
// compare source with the literal
entry_pos = pos;
i = 0;
while (source[pos] != '\0' && literal[i] != '\0' &&
source[pos] == literal[i]) {
pos++;
i++;
}
// if the end of the literal has been reached, comparison successful
if (literal[i] == '\0') {
test_flag = 1;
make_token(entry_pos);
} else {
// reset position
pos = entry_pos;
test_flag = 0;
}
}
void read_id(void)
{
int entry_pos;
skip_whitespace();
// recognize initial alphabetic character
entry_pos = pos;
if (('A' <= source[pos] && source[pos] <= 'Z') ||
('a' <= source[pos] && source[pos] <= 'z')) {
pos++;
test_flag = 1;
} else {
test_flag = 0;
return;
}
// recognize alphanumeric characters
while (('A' <= source[pos] && source[pos] <= 'Z') ||
('a' <= source[pos] && source[pos] <= 'z') ||
('0' <= source[pos] && source[pos] <= '9')) {
pos++;
}
// recognition successful, copy into token
make_token(entry_pos);
}
void read_number(void)
{
int entry_pos;
skip_whitespace();
// recognize optional negative sign
entry_pos = pos;
if (source[pos] == '-') {
pos++;
}
// recognize initial numeric character
if ('0' <= source[pos] && source[pos] <= '9') {
pos++;
test_flag = 1;
} else {
test_flag = 0;
return;
}
// recognize subsequent numeric characters
while ('0' <= source[pos] && source[pos] <= '9') {
pos++;
}
// recognition successful, copy into token
make_token(entry_pos);
}
void read_string(void)
{
int entry_pos;
skip_whitespace();
// recognize initial single quote
entry_pos = pos;
if (source[pos] == '\'') {
pos++;
// test_flag is not set as recognition can still fail
} else {
test_flag = 0;
return;
}
// recognize contents
while (source[pos] != '\0' && source[pos] != '\'') {
// increment line counter when new line reached
if (source[pos] == '\n') {
line++;
}
pos++;
}
// recognize final single quote
if (source[pos] == '\'') {
pos++;
test_flag = 1;
make_token(entry_pos);
} else if (source[pos] == '\0') {
// reset position
pos = entry_pos;
test_flag = 0;
}
}
void error_if_false(void)
{
if (!test_flag) {
fprintf(stderr, "error in line %i at token %s\n", line, token);
fclose(output);
// delete the output file
remove(output_name);
free(source);
free(token);
exit(1);
}
}
void meta_program(void);
void meta_exp1(void);
int main(int argc, char *argv[])
{
FILE *input;
int length;
if (argc < 3) {
fprintf(stderr, "usage: meta <input> <output>\n");
exit(1);
}
// open input and output
input = fopen(argv[1], "r");
if (input == NULL) {
fprintf(stderr, "invalid input file\n");
exit(1);
}
output_name = argv[2];
output = fopen(output_name, "w");
if (output == NULL) {
fprintf(stderr, "invalid output file\n");
exit(1);
}
// read entire input into source
fseek(input, 0, SEEK_END);
length = (int)ftell(input);
fseek(input, 0, SEEK_SET);
source = malloc(length + 1);
fread(source, 1, length, input);
source[length] = '\0';
fclose(input);
// initially we have empty token; token is never NULL
token = malloc(1);
token[0] = '\0';
// run meta
meta_program();
fprintf(stderr, "%d %d\n", pos, strlen(source));
fclose(output);
free(source);
free(token);
return 0;
}
/* end */