diff --git a/demo/add.c b/demo/add.c new file mode 100644 index 0000000..aa3f2f6 --- /dev/null +++ b/demo/add.c @@ -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))))))))))); +} \ No newline at end of file diff --git a/demo/lut.c b/demo/lut.c new file mode 100644 index 0000000..f5b23ba --- /dev/null +++ b/demo/lut.c @@ -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; +} \ No newline at end of file diff --git a/demo/parse.c b/demo/parse.c new file mode 100644 index 0000000..98b7e9d --- /dev/null +++ b/demo/parse.c @@ -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()); +} \ No newline at end of file diff --git a/demo/strcmp.c b/demo/strcmp.c new file mode 100644 index 0000000..6d4793e --- /dev/null +++ b/demo/strcmp.c @@ -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; +} \ No newline at end of file