diff --git a/README.md b/README.md index 56f9a02..9923c86 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ $ sh boot.sh ### 其它支持与不支持 +- 不允许在一个声明中声明多个变量或函数(如 `int a, b;`)请写成多个声明。 - 支持全局变量和局部变量,局部变量遮挡全局变量。 - 不支持局部变量之间的遮挡,重名的局部变量为同一变量。 - 函数只支持最多八个参数。函数声明中支持可变参数,仅用于兼容 C 语言库。 diff --git a/test.sh b/test.sh index 7025834..5a82fef 100644 --- a/test.sh +++ b/test.sh @@ -15,6 +15,7 @@ for i in *.c; do rm $i.ans else echo "Test '$i' failed" + exit 1 fi done echo "Passed $succ/$all tests" diff --git a/test/enum.c b/test/enum.c index 145b98b..7b53339 100644 --- a/test/enum.c +++ b/test/enum.c @@ -1,5 +1,5 @@ enum { - A, B, C = 1, D, E = A, F + A, B, C = 1, D, E = A, F, }; int main() { diff --git a/test/escape.c b/test/escape.c index 8a0d33e..92cf816 100644 --- a/test/escape.c +++ b/test/escape.c @@ -1,9 +1,11 @@ int getchar(); int putchar(int ch); -int printf(char* format, ...); +int fprintf(void* file, char* format, ...); +extern void* stdout; + int main() { - int ch; + char ch = 0["\t\r\""];; while ((ch = getchar()) != -1) { if (ch == '\\') { ch = getchar(); @@ -22,7 +24,7 @@ int main() { } else if (ch == '\"') { ch = '\"'; } else { - printf("unexpected escaped character: '\\%c'\n", ch); + fprintf(stdout, "unexpected escaped character: '\\%c'\n", ch); return 1; } } diff --git a/test/inc.c b/test/inc.c new file mode 100644 index 0000000..d39c5d3 --- /dev/null +++ b/test/inc.c @@ -0,0 +1,14 @@ +int printf(char* format, ...); + +int main() { + int a = 4; + printf("a = %d\n", a); + printf("a++ = %d\n", a++); + printf("a = %d\n", a); + printf("++a = %d\n", ++a); + printf("a = %d\n", a); + printf("a-- = %d\n", a--); + printf("a = %d\n", a); + printf("--a = %d\n", --a); + printf("a = %d\n", a); +} \ No newline at end of file diff --git a/test/inc.in b/test/inc.in new file mode 100644 index 0000000..e69de29 diff --git a/test/inc.out b/test/inc.out new file mode 100644 index 0000000..33429d9 --- /dev/null +++ b/test/inc.out @@ -0,0 +1,10 @@ +a = 4 +a++ = 4 +a = 5 +++a = 6 +a = 6 +a-- = 6 +a = 5 +--a = 4 +a = 4 +0 diff --git a/test/loop.c b/test/loop.c index 98c3e21..6ef9347 100644 --- a/test/loop.c +++ b/test/loop.c @@ -81,7 +81,25 @@ void test_break() { printf("\n"); } +void test_nested() { + int i; + int j; + + // For loop + printf("For loop:\n"); + 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) ", i, j); + } + printf("\n"); + } +} + int main() { test_continue(); test_break(); + test_nested(); } \ No newline at end of file diff --git a/test/loop.out b/test/loop.out index 460695d..1c18926 100644 --- a/test/loop.out +++ b/test/loop.out @@ -10,4 +10,7 @@ While loop: 0 1 2 Do-while loop: 0 1 2 -0 +For loop: +(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/overflow.c b/test/overflow.c index a580076..1d12c64 100644 --- a/test/overflow.c +++ b/test/overflow.c @@ -1,5 +1,41 @@ -int main() { - int placeholder[1024]; - int a = 1; - return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(0)))))))))))))))))))))), (a = a); +int printf(char* format, ...); + + +enum { + a = 1 +}; + +int get_20() { + return (a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(- -0))))))))))))))))))))); +} + +void dummy(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7) { + a0 += a1; + printf("%d\n", a0); + a0 -= a1; + printf("%d\n", a0); + a0 *= a1; + printf("%d\n", a0); + a0 /= a1; + printf("%d\n", a0); + a0 %= a1; + printf("%d\n", a0); + a0 &= a1; + printf("%d\n", a0); + a0 |= a1; + printf("%d\n", a0); + a0 ^= a2; + printf("%d\n", a0); + a0 <<= a1; + printf("%d\n", a0); + a0 >>= a1; + printf("%d\n", a0); +} + + +int main() { + char placeholder[4096]; + int a = 1; + dummy((a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(! !0))))))))))))))))))))), 3, a, a, a, a, a, a); + return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(~ ~0)))))))))))))))))))))), (a = a); } diff --git a/test/overflow.out b/test/overflow.out index 209e3ef..1c9bef7 100644 --- a/test/overflow.out +++ b/test/overflow.out @@ -1 +1,11 @@ +23 +20 +60 +20 +2 +2 +3 +2 +16 +2 20 diff --git a/test/short.c b/test/short.c new file mode 100644 index 0000000..6602908 --- /dev/null +++ b/test/short.c @@ -0,0 +1,17 @@ +int printf(char* format, ...); + +int f(int i) { + printf("f(%d)\n", i); + return i % 2; +} + +int main() { + 1 && f(1); + 0 && f(2); + 1 || f(3); + 0 || f(4); + 1 ? f(5) : f(6); + 0 ? f(7) : f(8); + 1 && f(9) || f(10); + 0 && f(11) || f(12); +} \ No newline at end of file diff --git a/test/short.in b/test/short.in new file mode 100644 index 0000000..e69de29 diff --git a/test/short.out b/test/short.out new file mode 100644 index 0000000..68ecb28 --- /dev/null +++ b/test/short.out @@ -0,0 +1,7 @@ +f(1) +f(4) +f(5) +f(8) +f(9) +f(12) +0 diff --git a/test/sort.c b/test/sort.c index b0793f0..970571f 100644 --- a/test/sort.c +++ b/test/sort.c @@ -1,21 +1,40 @@ int printf(char* format, ...); int scanf(char* format, ...); +int exit(int status); + +void assert_eq(int expected, int actual) { + if (expected != actual) { + printf("expected: %d, actual: %d\n", expected, actual); + exit(1); + } +} + +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]) { - int t = a[i]; - a[i] = a[j]; - a[j] = t; + swap(&a[i], &a[j]); + assert_eq(i - j, &a[i] - &a[j]); + assert_eq(j - i, &a[j] - &a[i]); + assert_eq(a[i], *(a + i)); + assert_eq(i[a], *(i + a)); + assert_eq(a[j - i], *(a + (j - i))); + assert_eq(j[a - i], *(j + (a - i))); } } } } +int a[100]; +int n; + int main() { - int n; - int a[100]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]);