more demo
This commit is contained in:
parent
40eb2ce96f
commit
2097d9fd34
4 changed files with 92 additions and 0 deletions
9
demo/add.c
Normal file
9
demo/add.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
int printf(const char format[], ...);
|
||||
int scanf(const char format[], ...);
|
||||
int putchar(int ch);
|
||||
|
||||
|
||||
int main() {
|
||||
int a = 1;
|
||||
return (a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a)))))))))));
|
||||
}
|
47
demo/lut.c
Normal file
47
demo/lut.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
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();
|
||||
return 0;
|
||||
}
|
19
demo/parse.c
Normal file
19
demo/parse.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
int getchar();
|
||||
|
||||
|
||||
int is_digit(int ch) {
|
||||
return '0' <= ch && ch <= '9';
|
||||
}
|
||||
|
||||
int parse_int(int ch) {
|
||||
int num = ch - '0';
|
||||
while (is_digit(ch = getchar())) {
|
||||
num = num * 10;
|
||||
num = num + ch - '0';
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return parse_int(getchar());
|
||||
}
|
17
demo/strcmp.c
Normal file
17
demo/strcmp.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
int printf(const char* format, ...);
|
||||
|
||||
int strcmp(const char* s1, const char* s2) {
|
||||
while (*s1 && *s2 && *s1 == *s2) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
return *s1 - *s2;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const char* s1 = "helloworld";
|
||||
const char* s2 = "world";
|
||||
printf("%d\n", strcmp(s1, s2));
|
||||
printf("%d\n", strcmp(s1 + 5, s2));
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue