diff --git a/boot.c b/boot.c index db14a1c..bfab8ac 100644 --- a/boot.c +++ b/boot.c @@ -230,25 +230,15 @@ int parse_int(int ch) { int get_escaped_char() { int ch = getchar(); - if (ch == 'n') { - ch = '\n'; - } else if (ch == 't') { - ch = '\t'; - } else if (ch == 'r') { - ch = '\r'; - } else if (ch == '0') { - ch = '\0'; - } else if (ch == '\\') { - ch = '\\'; - } else if (ch == '\'') { - ch = '\''; - } else if (ch == '\"') { - ch = '\"'; - } else { - fprintf(stderr, "unexpected escaped character: '\\%c'\n", ch); - exit(1); - } - return ch; + if (ch == 'n') return '\n'; + if (ch == 't') return '\t'; + if (ch == 'r') return '\r'; + if (ch == '0') return '\0'; + if (ch == '\\') return '\\'; + if (ch == '\'') return '\''; + if (ch == '\"') return '\"'; + fprintf(stderr, "unexpected escaped character: '\\%c'\n", ch); + exit(1); } int token_state; diff --git a/demo/queen.c b/demo/queen.c index 855793e..66463b5 100644 --- a/demo/queen.c +++ b/demo/queen.c @@ -30,6 +30,7 @@ void queen(int x) { if (x > 8) { output(); a[0]++; + return; } for (int y = 1; y <= 8; ++y) { if (ok(x, y)) { diff --git a/demo/sort.c b/demo/sort.c new file mode 100644 index 0000000..21ac5a6 --- /dev/null +++ b/demo/sort.c @@ -0,0 +1,36 @@ +int printf(char* format, ...); +int scanf(char* format, ...); +int exit(int status); + +void swap(int* a, int* b) { + int t = *a; + *a = *b; + *b = t; +} + +void sort(int a[], int n) { + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (a[i] > a[j]) { + swap(&a[i], &a[j]); + } + } + } +} + +int a[100]; +int n; + +int main() { + printf("Enter the number of elements: "); + scanf("%d", &n); + printf("Enter %d integers: ", n); + for (int i = 0; i < n; i++) { + scanf("%d", &a[i]); + } + sort(a, n); + for (int i = 0; i < n; i++) { + printf("%d ", a[i]); + } + printf("\n"); +} \ No newline at end of file diff --git a/run.sh b/run.sh index e07d810..1481d3d 100644 --- a/run.sh +++ b/run.sh @@ -14,4 +14,4 @@ gcc boot.c -o boot.elf && ./boot.elf < $1 > $1.s && compile_and_run $1 echo $? -rm $1.s $1.s.elf boot.elf 2> /dev/null \ No newline at end of file +rm $1.s $1.elf boot.elf 2> /dev/null \ No newline at end of file diff --git a/test/function/hanoi.c b/test/function/hanoi.c new file mode 100644 index 0000000..8f9382f --- /dev/null +++ b/test/function/hanoi.c @@ -0,0 +1,20 @@ +int printf(char* format, ...); + +void move(char from_rod, char to_rod) { + printf("%c --> %c\n", from_rod, to_rod); +} + +void hanoi(int n, char from_rod, char to_rod, char aux_rod) { + if (n == 1) { + move(from_rod, to_rod); + return; + } + hanoi(n - 1, from_rod, aux_rod, to_rod); + move(from_rod, to_rod); + hanoi(n - 1, aux_rod, to_rod, from_rod); +} + +int main() { + int n = 3; + hanoi(n, 'A', 'C', 'B'); +} \ No newline at end of file diff --git a/test/function/hanoi.out b/test/function/hanoi.out new file mode 100644 index 0000000..4fe7fc8 --- /dev/null +++ b/test/function/hanoi.out @@ -0,0 +1,8 @@ +A --> C +A --> B +C --> B +A --> C +B --> A +B --> C +A --> C +0 diff --git a/test/function/queen.c b/test/function/queen.c new file mode 100644 index 0000000..2bac9b0 --- /dev/null +++ b/test/function/queen.c @@ -0,0 +1,32 @@ +int printf(char* format, ...); +int putchar(int ch); + +int a[9]; + +int ok(int x, int y) { + for (int i = 1; i <= x - 1; ++i) { + if (a[i] == y || a[i] - i == y - x || a[i] + i == y + x) { + return 0; + } + } + return 1; +} + +void queen(int x) { + if (x > 8) { + a[0]++; + return; + } + for (int y = 1; y <= 8; ++y) { + if (ok(x, y)) { + a[x] = y; + queen(x + 1); + a[x] = 0; + } + } +} + +int main() { + queen(1); + printf("%d\n", a[0]); +} \ No newline at end of file diff --git a/test/function/queen.out b/test/function/queen.out new file mode 100644 index 0000000..0af0bb6 --- /dev/null +++ b/test/function/queen.out @@ -0,0 +1,2 @@ +92 +0 diff --git a/test/io/echo.c b/test/io/echo.c new file mode 100644 index 0000000..b850415 --- /dev/null +++ b/test/io/echo.c @@ -0,0 +1,7 @@ +int getchar(); +int putchar(int ch); + +int main() { + for (char ch; (ch = getchar()) != -1; putchar(ch)); + putchar('\n'); +} \ No newline at end of file diff --git a/test/io/echo.in b/test/io/echo.in new file mode 100644 index 0000000..95d09f2 --- /dev/null +++ b/test/io/echo.in @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/test/io/echo.out b/test/io/echo.out new file mode 100644 index 0000000..ff8fbcb --- /dev/null +++ b/test/io/echo.out @@ -0,0 +1,2 @@ +hello world +0 diff --git a/test/misc/escape.c b/test/io/escape.c similarity index 64% rename from test/misc/escape.c rename to test/io/escape.c index 92cf816..f04ce79 100644 --- a/test/misc/escape.c +++ b/test/io/escape.c @@ -1,14 +1,16 @@ -int getchar(); -int putchar(int ch); -int fprintf(void* file, char* format, ...); -extern void* stdout; +int offset = 0; +int get() { + return "hello\\n\\tworld\\\\\\'\\\"\\0\r\t\0"[offset++]; +} + +int putchar(int ch); int main() { - char ch = 0["\t\r\""];; - while ((ch = getchar()) != -1) { + char ch; + while ((ch = get()) != 0) { if (ch == '\\') { - ch = getchar(); + ch = get(); if (ch == 'n') { ch = '\n'; } else if (ch == 't') { @@ -16,7 +18,7 @@ int main() { } else if (ch == 'r') { ch = '\r'; } else if (ch == '0') { - ch = '\0'; + break; } else if (ch == '\\') { ch = '\\'; } else if (ch == '\'') { @@ -24,8 +26,7 @@ int main() { } else if (ch == '\"') { ch = '\"'; } else { - fprintf(stdout, "unexpected escaped character: '\\%c'\n", ch); - return 1; + break; } } putchar(ch); diff --git a/test/io/escape.out b/test/io/escape.out new file mode 100644 index 0000000..6354775 --- /dev/null +++ b/test/io/escape.out @@ -0,0 +1,2 @@ +hello + world\'"0 diff --git a/test/io/extern.c b/test/io/extern.c new file mode 100644 index 0000000..c9fa893 --- /dev/null +++ b/test/io/extern.c @@ -0,0 +1,16 @@ +int fprintf(void* stream, char* format, ...); +int fscanf(void* stream, char* format, ...); +int sprintf(char* str, char* format, ...); + +extern void* stdin; +extern void* stdout; + +int main() { + char buffer[4096]; + int a; + int b; + fscanf(stdin, "%d %d", &a, &b); + sprintf(buffer, "The sum of %d and %d is %d\n", a, b, a + b); + fprintf(stdout, buffer); + return 0; +} \ No newline at end of file diff --git a/test/io/extern.in b/test/io/extern.in new file mode 100644 index 0000000..e7e7d8e --- /dev/null +++ b/test/io/extern.in @@ -0,0 +1 @@ +3 4 \ No newline at end of file diff --git a/test/io/extern.out b/test/io/extern.out new file mode 100644 index 0000000..e64d8a1 --- /dev/null +++ b/test/io/extern.out @@ -0,0 +1,2 @@ +The sum of 3 and 4 is 7 +0 diff --git a/test/loop/diamond.c b/test/loop/diamond.c new file mode 100644 index 0000000..4d9bf15 --- /dev/null +++ b/test/loop/diamond.c @@ -0,0 +1,30 @@ +int printf(char* format, ...); + + +int main() { + int n = 5; // height of the diamond + int i; + int j; + + // upper half of the diamond + for (i = 1; i <= n; i++) { + for (j = i; j < n; j++) { + printf(" "); + } + for (j = 1; j <= (2 * i - 1); j++) { + printf("*"); + } + printf("\n"); + } + + // lower half of the diamond + for (i = n - 1; i >= 1; i--) { + for (j = n; j > i; j--) { + printf(" "); + } + for (j = 1; j <= (2 * i - 1); j++) { + printf("*"); + } + printf("\n"); + } +} \ No newline at end of file diff --git a/test/loop/diamond.out b/test/loop/diamond.out new file mode 100644 index 0000000..d369f75 --- /dev/null +++ b/test/loop/diamond.out @@ -0,0 +1,10 @@ + * + *** + ***** + ******* +********* + ******* + ***** + *** + * +0 diff --git a/test/loop/nested.c b/test/loop/nested.c deleted file mode 100644 index 6232e27..0000000 --- a/test/loop/nested.c +++ /dev/null @@ -1,20 +0,0 @@ -int printf(char* format, ...); - -void test() { - int i; - int j; - - for (i = 0; i < 5; i++) { - for (j = 0; j < 5; j++) { - if (i >= 2 && j >= 2 && i + j >= 5) { - return; // Exit nested loop via return - } - printf("(%d, %d)%c", i, j, " \n"[j == 4]); - } - } -} - -int main() { - test(); - printf("\n"); -} \ No newline at end of file diff --git a/test/loop/nested.out b/test/loop/nested.out deleted file mode 100644 index bea6ce1..0000000 --- a/test/loop/nested.out +++ /dev/null @@ -1,4 +0,0 @@ -(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) -(1, 0) (1, 1) (1, 2) (1, 3) (1, 4) -(2, 0) (2, 1) (2, 2) -0 diff --git a/test/misc/escape.in b/test/misc/escape.in deleted file mode 100644 index 0ee2bcc..0000000 --- a/test/misc/escape.in +++ /dev/null @@ -1 +0,0 @@ -hello\n\tworld\u \ No newline at end of file diff --git a/test/misc/escape.out b/test/misc/escape.out deleted file mode 100644 index 3230064..0000000 --- a/test/misc/escape.out +++ /dev/null @@ -1,3 +0,0 @@ -hello - worldunexpected escaped character: '\u' -1