-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrcc.c
89 lines (88 loc) · 2.1 KB
/
rcc.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
#include <stdio.h>
#include <string.h>
char *outdata; char *outcode;
#define apd(n, q, ...) sprintf(n, "%s" q, n, __VA_ARGS__)
char* dtrs(char *in) {
char ret[512];
long long tmp = time(NULL);
// find if its a symbol, or whatever
if ( in[0] == '\'' ) {
sprintf(ret, "#%d", in[1]);
return ret;
}
if ( in[0] == '"' ) {
in[strlen(in)-1]=0;
apd(outdata, ".label %lld\n.ds %s\n.db #0\n", tmp, &in[1]);
sprintf(ret, "@%d", tmp);
return ret;
}
if ( isdigit(in[0]) ) {
sprintf(ret, "#%d", atoi(in));
return ret;
}
sprintf(ret, "@%s", in);
return ret;
}
int main(int argc, char **argv) {
char cmd[64];
char nsn[64];
char val[512];
char t = 0;
outdata = malloc(16384 + atoi(argc > 2 ? argv[2] : "16384"));
outcode = malloc(16384 + atoi(argc > 2 ? argv[2] : "16384"));
outcode[0] = 0;
outdata[0] = 0;
FILE *fp = fopen(argv[1], "r");
while(!feof(fp)) {
fscanf(fp, "%s ", cmd);
if (!strcmp(cmd, "//" )) {
fscanf(fp, "%[^\n]", cmd);
} else
if (!strcmp(cmd, "int")||!strcmp(cmd, "char*")) {
fscanf(fp, "%s ", nsn);
//printf("%s\n", nsn);
fscanf(fp, "%c ", &t);
if ( t == '=' || t == ';' ) {
apd(outdata, ".label %s\n", nsn);
if ( t != ';' ) {
fscanf(fp, "%[^;]; ", val);
if ( val[0] == '"' ) {
val[strlen(val)-1] = '\0';
apd(outdata, ".ds %s\n.db #0\n", &val[1]);
} else if ( val[0] == '\'' ) {
apd(outdata, ".ds %c\n", val[1]);
} else {
apd(outdata, ".db #%d\n", atoi(val));
}
} else {
apd(outdata, ".db #0\n", NULL);
}
} else {
if ( t == '(' ) {
fgetc(fp);
apd(outcode, ".label %s\n", nsn);
}
}
} else if (!strcmp(cmd, "{")) {
} else
if (!strcmp(cmd, "}") ) {
apd(outcode, "ret\n", NULL);
} else
if (!strcmp(cmd, "return") ) {
fscanf(fp, "%[^;]; ", nsn);
apd(outcode, "mov #%s #0\n", nsn);
} else if (!strcmp(cmd, "//_asm")) {
fscanf(fp, "%[^;]; ", nsn);
apd(outcode, "%s\n", nsn);
} else {
nsn[0] = 0;
fscanf(fp, "(%[^)]); ", nsn);
if ( nsn[0] != 0 ) {
apd(outcode, "mov %s #10\n", dtrs(nsn));
}
apd(outcode, "call @%s\n", cmd);
}
}
puts(outcode);
puts(outdata);
}