-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2f.c
473 lines (399 loc) · 9.34 KB
/
c2f.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Minimal C Compiler
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
char buf[255]= {0};
int bufn= 0;
FILE *f= NULL;
int c= 0, lineno= 0;
void skipspc();
int is(const char *i);
int ensure() {
if (bufn>0) return 1;
bufn= 0;
char *ln= fgets(buf, sizeof(buf), f);
if (!ln) return 0;
bufn= strlen(ln);
lineno++;
if (ln[strlen(ln)-1]=='\n') {
ln[strlen(ln)-1]= 0;
bufn--;
}
printf("\e[32m%d: %s\e[37m\n", lineno, buf);
skipspc();
if (!*buf || is("//") || is("#include")) {
// force another call
bufn= 0;
}
return ensure();
}
int peek() {
return ensure()? buf[0]: 0;
}
int step() {
int c= peek();
memcpy(buf, buf+1, sizeof(buf)-1);
if (--bufn<0) bufn= 0;
return c;
}
void skipspc() {
while(ensure() && isspace(peek()))
step();
}
int is(const char *i) {
return ensure() && strncmp(buf, i, strlen(i))==0;
}
int got(const char *i) {
skipspc();
if (is(i)) {
int n= strlen(i);
while(n--) step();
return 1;
}
return 0;
}
void expect(const char *e) {
if (!got(e)) {
printf("ERROR AT %d '%s'\n", bufn, buf);
printf("Expected '%s'\n", e);
assert(!"unexpected char");
}
}
void NIY(char *s) {
printf("%s\n", s);
assert(!"error");
}
char *getname() {
skipspc();
// TODO: may not always work? (nl/comement?)
char *s= buf;
while (*s && isalnum(*s)) s++;
char *r = strndup(buf, s-buf);
int n= strlen(r);
while(n--) step();
return r;
}
const char *types[]= {
"_DYMMUTYPE", "void", "char", "int", "long", "float", "double", "struct"};
// returns type index, -index if pointer
// (However: "char *a, b" - b is char!)
int gettype() {
int _static= got("static");
// TODO: handle static (scoped globals!)
// (how? may need move locals?)
assert(!_static);
int _signed= got("signed");
int _unsigned= got("unsigned");
// long long? long double/float?
for(int i=0; i<sizeof(types)/sizeof(*types); i++) {
if (got(types[i])) {
// TODO: structs/union
assert(types[i]!="struct");
int pointer= got("*");
return pointer?-i:i;
}
}
// no type recognized
// TODO: typedefs?
if (_signed || _unsigned)
assert(!"unexpected signed/unsigned");
return 0;
}
#define MAXVAR 1024
#define GLOBAL -1
struct variable {
int type; // 0==new scope
char *name;
int rel;
} variables[MAXVAR]= {0};
int nvar= 0, nglob= 0, nloc= 0;
int lrel= 0;
void addGlobal(int type, char *name) {
assert(nvar<MAXVAR);
// TODO: handle statics which are "scoped" globals
if (name) // not scope
assert(lrel==0); // can't add globals inside function
struct variable *v= &variables[nvar];
v->type= type;
v->name= name?strdup(name):NULL;
v->rel= GLOBAL;
nvar++;
nglob++;
}
void addLocal(int type, char *name) {
assert(nvar<MAXVAR);
struct variable *v= &variables[nvar];
v->type= type;
v->name= name?strdup(name):NULL;
// there is no reuse inside one function
v->rel= ++lrel;
nvar++;
}
void dumpVars() {
// comment out for debug
return;
// dump variables
fflush(stdout);
fprintf(stderr, "\n");
for(int i=0; i<nvar; i++) {
struct variable *v= &variables[i];
fprintf(stderr, "VAR %s : %s %s %d\n", v->name,
!v->type? "SCOPE" : types[v->type<0?-v->type:v->type],
v->type<0? "*": " ",
v->rel);
}
}
// TODO: this doesn NOT handle nested functions, not needed for standard C
void beginScope() {
addGlobal(0, NULL);
dumpVars();
}
void endScope() {
int i= nvar;
// delete all locals added since beginScope
while(i-->0) {
struct variable *v= &variables[i];
if (!v->type) { // is beginscope
nvar= i;
dumpVars();
return;
}
}
// reach top scope, reset relative
lrel= 0;
dumpVars();
}
struct variable *findVar(char* name) {
// search backwards to handle shadowing
for(int i=nvar-1; i>=0; i--) {
struct variable *v= &variables[i];
if (v->name && 0==strcmp(v->name, name)) {
// found
return v;
}
}
return NULL;
}
void deref(struct variable *v) {
if (!v) return;
if (v->rel==GLOBAL) {
printf(" @ ");
} else {
printf(" local@ ");
}
}
void takeexpression();
void takevardef(int type, char *name, const char *scope) {
int array= got("[");
// TODO: handle array decl/assign
assert(!array);
if (scope=="global")
printf(" %s _%s\n", scope, name);
if (got("=")) {
takeexpression();
//printf(" _%s !\n", name);
struct variable *v= findVar(name);
printf(" %d local!\n", v->rel);
}
}
const char *ops[]= {
"++", "+", "--", "-", "*", "/", "%", "&&", "&", "||", "|", "^", "<<", "<", ">>", ">", "==", "="};
const char *getop() {
for(int i=0; i<sizeof(ops)/sizeof(*ops); i++) {
if (got(ops[i])) return ops[i];
}
return NULL;
}
void takeblock();
void takeexpression() {
int paren= got("(");
const char *prefixop= getop();
struct variable *var= NULL;
if (isalpha(peek())) {
char *name= getname();
// function call? ( , )
if (got("(")) {
// TODO: this is PASCAL calling style
// C-calling style would be reversed!
// (for argv/printf it's needed!)
do {
takeexpression();
} while (got(","));
expect(")");
printf(" _%s\n", name);
} else {
// variable (value)
var= findVar(name);
if (!var) {
printf("%% variable '%s' not found\n", name);
exit(1);
}
if (var->rel==GLOBAL) {
printf(" _%s", name);
} else {
printf(" %d ", var->rel, name);
}
}
if (name) free(name);
} else if (isdigit(peek())) {
char *s= getname();
int d= 0, r, n= 0;
if (1== (r= sscanf(s, "%d %n", &d, &n))) {
printf(" %d", d);
} else {
printf("scanf=>r=%d\n", r);
printf("ERROR AT %d '%s'\n", bufn, buf);
assert(!"bad char in number");
}
if (s) free(s);
} else if (got("\"")) {
// string
printf(" \"");
while(peek() && peek()!='"')
putchar(step());
step();
printf("\"\n");
} else if (got("\'")) {
// char
printf(" %d\n", step());
expect("\'");
} else {
printf("ERROR AT %d '%s'\n", bufn, buf);
assert(!"unexpected char");
}
// handle assignment/reference
const char *op= getop();
if (op) {
// assignment
assert("="=="=");
if (op=="=") { // EQ, lol
// "lvalue"
if (!var) NIY("Can't handle nonvar assignements yet");
if (var->rel==GLOBAL) {
op= ":=";
} else {
op= ":frame=";
}
} else {
// if have ref
deref(var);
}
// TODO: too deep: 1+2+3 1 2 3 + + + => 1 2 + 3 + (same prio)
// TODO: handle priorities?
// (takexp) could return prio, if current prio <= takeexpr (?) switch order
takeexpression();
printf(" %s", op);
} else {
// just reference
deref(var);
}
if (prefixop)
printf(" PREFIX_%s\n", prefixop);
if (paren) expect(")");
}
int takestatement() {
if (is("{")) {
takeblock();
} else if (got("return")) {
takeexpression();
printf(" exit\n");
} else if (got("if")) {
takeexpression();
printf(" if\n");
takestatement();
if (got("else")) {
printf(" else");
takestatement();
}
printf(" endif\n");
} else if (got("while")) {
takeexpression();
// TODO:
} else if (got("do")) {
takeexpression();
// TODO:
} else if (got(";")) {
; // haha!
} else {
int type= gettype();
if (type) {
// local var?
char *name= getname();
assert(name);
addLocal(type, name);
takevardef(type, name, "local");
if (name) free(name);
return 1;
}
// proc/func call/assignment
if (isalnum(peek())) {
takeexpression();
// assume all fun/assign return value
printf(" drop\n");
// TODO: don't drop if first parameter is same value/var! (do at byte coder?)
return 1;
} else {
// not recognized
return 0;
}
}
return 1;
}
void takeblock() {
expect("{");
beginScope();
while(takestatement());
endScope();
expect("}");
}
void takeprogram() {
while(peek()) {
// definition func/var
int type= gettype();
if (type) {
char *name= getname();
if (got("(")) { // function
printf(" : _%s ", name);
beginScope();
// params
// printf(" {");
int type, npar= 0;
while((type= gettype())) {
char *param= getname();
if (!param || !*param) break;
// printf(" _%s", param);
npar++;
addLocal(type, param);
free(param);
got(",");
}
// printf(" } %d %d parloc\n", npar, 0);
printf(" %d %d parloc\n", npar, 0);
// TODO: 0 may be modifed later by 'setlocals'
expect(")");
// body
takeblock();
endScope();
// 'setlocals' is like 'immediate' - modifies word defined
printf(" ; %d setlocals\n", lrel-npar);
// one extra to reset
endScope();
assert(lrel==0);
} else { // variable
// TODO: store type... (maybe no need for int and char* lol?)
// TODO: this is fine for globals, but scopped, local vars in functions?
addGlobal(type, name);
takevardef(type, name, "variable");
}
if (name) free(name);
} else {
// over-simplified
takestatement();
}
}
}
int main(void) {
f= stdin;
takeprogram();
dumpVars();
}