-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.c
214 lines (192 loc) · 7.63 KB
/
parse.c
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
#include <string.h>
#include <stdint.h>
#include "common.h"
uint16_t Q;
static int8_t next(FILE * input) {
char c;
while((c = getc(input)) != EOF) {
Q++;
if(c == ' ' || c == '\n' || c == '\t')
return c;
}
return 0;
}
static int32_t getnum(FILE * input) {
uint8_t sign = next(input), c;
int32_t n = 0, q;
if(sign != '\t' && sign != ' ')
return 0;
q = sign == '\t' ? -1 : 1;
while((c = next(input)) != '\n') {
n <<= 1;
if(c == '\t') n++;
if(!c) break;
}
return n * q;
}
static vector(char) getlab(FILE * input) {
vector(char) label = NULL;
uint8_t c;
while((c = next(input)) != '\n')
vector_push_back(label, c == '\t' ? 'T' : 'S');
vector_push_back(label, 0);
return label;
}
static struct instruction_t parse_heap(FILE * input, void (*fatal)(char * s)) {
switch(next(input)) {
case ' ': return (struct instruction_t) { STO, 0 };
case '\t': return (struct instruction_t) { RCL, 0 };
default: fatal("<lf>|e, IMP heap"); return (struct instruction_t) { ERR, 0 };
}
}
static struct instruction_t parse_arith(FILE * input, void (*fatal)(char * s)) {
switch(next(input)) {
case ' ': switch(next(input)) {
case ' ': return (struct instruction_t) { ADD, 0 };
case '\t': return (struct instruction_t) { SUB, 0 };
case '\n': return (struct instruction_t) { MUL, 0 };
default: fatal("e, IMP arith"); return (struct instruction_t) { ERR, 0 };
}
case '\t': switch(next(input)) {
case ' ': return (struct instruction_t) { DIV, 0 };
case '\t': return (struct instruction_t) { MOD, 0 };
default: fatal("<lf>|e, IMP arith"); return (struct instruction_t) { ERR, 0 };
}
default: fatal("<lf>|e, IMP arith"); return (struct instruction_t) { ERR, 0 };
}
}
static struct instruction_t parse_io(FILE * input, void (*fatal)(char * s)) {
switch(next(input)) {
case ' ': switch(next(input)) {
case ' ': return (struct instruction_t) { PUTC, 0 };
case '\t': return (struct instruction_t) { PUTN, 0 };
default: fatal("<lf>|e, IMP io"); return (struct instruction_t) { ERR, 0 };
}
case '\t': switch(next(input)) {
case ' ': return (struct instruction_t) { GETC, 0 };
case '\t': return (struct instruction_t) { GETN, 0 };
default: fatal("<lf>|e, IMP io"); return (struct instruction_t) { ERR, 0 };
}
default: fatal("<lf>|e, IMP io"); return (struct instruction_t) { ERR, 0 };
}
}
static struct instruction_t parse_stack(FILE * input, void (*fatal)(char * s)) {
switch(next(input)) {
case ' ': return (struct instruction_t) { PSH, getnum(input) };
case '\n': switch(next(input)) {
case ' ': return (struct instruction_t) { DUP, 0 };
case '\t': return (struct instruction_t) { XCHG, 0 };
case '\n': return (struct instruction_t) { DROP, 0 };
default: fatal("e, IMP stk"); return (struct instruction_t) { ERR, 0 };
}
case '\t': switch(next(input)) {
case ' ': return (struct instruction_t) { COPY, .data = getnum(input) };
case '\n': return (struct instruction_t) { SLIDE, .data = getnum(input) };
default: fatal("<tab>|e, IMP stk"); return (struct instruction_t) { ERR, 0 };
}
default: fatal("e, IMP stk"); return (struct instruction_t) { ERR, 0 };
}
}
static struct instruction_t parse_flow(FILE * input, void (*fatal)(char * s)) {
switch(next(input)) {
case ' ': switch(next(input)) {
case ' ':return (struct instruction_t) { LBL, .label = getlab(input) };
case '\t': return (struct instruction_t) { CALL, .label = getlab(input) };
case '\n': return (struct instruction_t) { JMP, .label = getlab(input) };
default: fatal("e, IMP flow"); return (struct instruction_t) { ERR, 0 };
}
case '\t': switch(next(input)) {
case ' ': return (struct instruction_t) { BZ, .label = getlab(input) };
case '\t': return (struct instruction_t) { BLTZ, .label = getlab(input) };
case '\n': return (struct instruction_t) { RET, 0 };
default: fatal("e, IMP flow"); return (struct instruction_t) { ERR, 0 };
}
case '\n':
if(next(input) == '\n')
return (struct instruction_t) { STOP, 0 };
else
{ fatal("bad end, IMP flow"); return (struct instruction_t) { ERR, 0 }; }
default: fatal("e, IMP flow"); return (struct instruction_t) { ERR, 0 };
}
}
struct _label_t {
int32_t id;
char * name;
struct instruction_t parent;
};
static struct _label_t * getlabel(vector(struct _label_t) vec, char * label_text) {
vector_foreach(struct _label_t, it, vec)
if(!strcmp(label_text, it->name))
return it;
return NULL;
}
static vector(struct label_t) fixup_labels(vector(struct instruction_t) program, void(*warn)(char * s)) {
vector(struct _label_t) labels = NULL;
vector(struct label_t) labs = NULL;
int32_t labid = 1;
vector_foreach(struct instruction_t, it, program)
if(it->type == LBL) {
struct _label_t * l;
if((l = getlabel(labels, it->label)) != NULL) {
warn("duplicated label.");
vector_free(it->label);
it->data = l->id;
continue;
}
struct _label_t lab = {.id = labid, .parent = *it, .name = it->label};
struct label_t public_label = {.id = labid, .parent = it};
it->data = labid++;
vector_push_back(labels, lab);
vector_push_back(labs, public_label);
}
vector_foreach(struct instruction_t, it, program)
switch(it->type) {
case CALL: case JMP: case BZ: case BLTZ: {
struct _label_t * l;
if((l = getlabel(labels, it->label)) != NULL) {
vector_free(it->label);
it->data = l->id;
} else {
vector_free(it->label);
warn("dead label.");
it->data = 0;
}
}
}
vector_foreach(struct _label_t, it, labels)
vector_free(it->name);
vector_free(labels);
return labs;
}
struct parse_result_t parse(FILE * input, void (*fatal)(char * s), void (*warn)(char * s)) {
vector(struct instruction_t) q = NULL;
struct instruction_t cur;
while(!feof(input)) {
switch(next(input)) {
case '\t':
switch(next(input)) {
case '\t':
vector_push_back(q, cur = parse_heap(input, fatal));
break;
case ' ':
vector_push_back(q, cur = parse_arith(input, fatal));
break;
case '\n':
vector_push_back(q, cur = parse_io(input, fatal));
break;
default:
fatal("? <tab>, E, IMP N/A"); return (struct parse_result_t) { NULL, NULL };
}
break;
case ' ':
vector_push_back(q, cur = parse_stack(input, fatal));
break;
case '\n':
vector_push_back(q, cur = parse_flow(input, fatal));
break;
}
if(cur.type == ERR)
return (struct parse_result_t) { NULL, NULL };
}
return (struct parse_result_t) { q, fixup_labels(q, warn) };
}