46 lines
1018 B
C
46 lines
1018 B
C
int printf(const char format[], ...);
|
|
int getchar();
|
|
|
|
char string_table[65536];
|
|
int string_offset;
|
|
int string_lut[4096];
|
|
int string_lut_size;
|
|
|
|
int parse_string() {
|
|
int offset = string_offset;
|
|
int ch;
|
|
while ((ch = getchar()) != '"') {
|
|
if (ch == -1 || ch == '\n') {
|
|
printf("expecting '\"'\n");
|
|
return 1;
|
|
}
|
|
string_table[string_offset++] = ch;
|
|
}
|
|
string_table[string_offset++] = 0;
|
|
string_lut[string_lut_size] = offset;
|
|
return string_lut_size++;
|
|
}
|
|
|
|
|
|
int streq(const char* s1, const char* s2) {
|
|
while (*s1 && *s2 && *s1 == *s2) {
|
|
s1++;
|
|
s2++;
|
|
}
|
|
return *s1 == *s2;
|
|
}
|
|
|
|
void dump_string_table() {
|
|
printf(".data\n");
|
|
for (int i = 0; i < string_lut_size; ++i) {
|
|
char* id = string_table + string_lut[i];
|
|
printf(".LC%d: .string \"%s\", const: %d\n",
|
|
i, id, streq(id, "const"));
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
char ch;
|
|
while ((ch = getchar()) == '"') parse_string();
|
|
dump_string_table();
|
|
} |