feat: add return_flag and params_start for proper function return handling and parameter scanning
Implement a dedicated return_flag variable to replace the magic number -999 for return statements, enabling correct early exit from nested blocks. Add params_start field to Func struct to track parameter declaration positions, allowing the interpreter to properly map function arguments to local variables during function calls. Extend the test suite with new test files for arrays, comparison operators, logical operators, pointers, functions, trigonometry, and file seek operations, along with corresponding Makefile targets to execute them.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
int main() {
|
||||
printf("=== Array Tests ===\n");
|
||||
|
||||
printf("Test 1: Array declaration and initialization\n");
|
||||
int arr[5];
|
||||
arr[0] = 10;
|
||||
arr[1] = 20;
|
||||
arr[2] = 30;
|
||||
arr[3] = 40;
|
||||
arr[4] = 50;
|
||||
printf("arr[0] = %d\n", arr[0]);
|
||||
printf("arr[1] = %d\n", arr[1]);
|
||||
printf("arr[2] = %d\n", arr[2]);
|
||||
printf("arr[3] = %d\n", arr[3]);
|
||||
printf("arr[4] = %d\n", arr[4]);
|
||||
printf("PASS: Array indexing works\n");
|
||||
|
||||
printf("Test 2: Array modification\n");
|
||||
arr[2] = 100;
|
||||
printf("After arr[2] = 100: arr[2] = %d\n", arr[2]);
|
||||
printf("PASS: Array element modification works\n");
|
||||
|
||||
printf("Test 3: Loop through array\n");
|
||||
int i = 0;
|
||||
int sum = 0;
|
||||
while (i < 5) {
|
||||
sum = sum + arr[i];
|
||||
i = i + 1;
|
||||
}
|
||||
printf("Sum of array elements: %d\n", sum);
|
||||
printf("PASS: Array iteration works\n");
|
||||
|
||||
printf("Test 4: Array with calculations\n");
|
||||
int nums[3];
|
||||
nums[0] = 5;
|
||||
nums[1] = 10;
|
||||
nums[2] = 15;
|
||||
int total = 0;
|
||||
total = total + nums[0];
|
||||
total = total + nums[1];
|
||||
total = total + nums[2];
|
||||
printf("nums[0] + nums[1] + nums[2] = %d\n", total);
|
||||
printf("PASS: Array arithmetic works\n");
|
||||
|
||||
printf("Test 5: Larger array\n");
|
||||
int big[10];
|
||||
int j = 0;
|
||||
while (j < 10) {
|
||||
big[j] = j * j;
|
||||
j = j + 1;
|
||||
}
|
||||
printf("Squares (0-9): ");
|
||||
int k = 0;
|
||||
while (k < 10) {
|
||||
printf("%d ", big[k]);
|
||||
k = k + 1;
|
||||
}
|
||||
printf("\n");
|
||||
printf("PASS: Larger arrays work\n");
|
||||
|
||||
printf("\n=== All Array Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
int main() {
|
||||
printf("=== Comparison Operators Tests ===\n");
|
||||
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int c = 10;
|
||||
|
||||
printf("Test 1: Less than (<)\n");
|
||||
if (a < b) {
|
||||
printf("10 < 20 = true\n");
|
||||
}
|
||||
if (b < a) {
|
||||
printf("20 < 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("20 < 10 = false\n");
|
||||
}
|
||||
if (a < c) {
|
||||
printf("10 < 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 < 10 = false\n");
|
||||
}
|
||||
printf("PASS: Less than operator works\n");
|
||||
|
||||
printf("Test 2: Greater than (>)\n");
|
||||
if (b > a) {
|
||||
printf("20 > 10 = true\n");
|
||||
}
|
||||
if (a > b) {
|
||||
printf("10 > 20 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 > 20 = false\n");
|
||||
}
|
||||
if (a > c) {
|
||||
printf("10 > 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 > 10 = false\n");
|
||||
}
|
||||
printf("PASS: Greater than operator works\n");
|
||||
|
||||
printf("Test 3: Less than or equal (<=)\n");
|
||||
if (a <= b) {
|
||||
printf("10 <= 20 = true\n");
|
||||
}
|
||||
if (a <= c) {
|
||||
printf("10 <= 10 = true\n");
|
||||
}
|
||||
if (b <= a) {
|
||||
printf("20 <= 10 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("20 <= 10 = false\n");
|
||||
}
|
||||
printf("PASS: Less than or equal operator works\n");
|
||||
|
||||
printf("Test 4: Greater than or equal (>=)\n");
|
||||
if (b >= a) {
|
||||
printf("20 >= 10 = true\n");
|
||||
}
|
||||
if (a >= c) {
|
||||
printf("10 >= 10 = true\n");
|
||||
}
|
||||
if (a >= b) {
|
||||
printf("10 >= 20 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("10 >= 20 = false\n");
|
||||
}
|
||||
printf("PASS: Greater than or equal operator works\n");
|
||||
|
||||
printf("Test 5: Negative number comparisons\n");
|
||||
int neg1 = -5;
|
||||
int neg2 = -10;
|
||||
if (neg1 > neg2) {
|
||||
printf("-5 > -10 = true\n");
|
||||
}
|
||||
if (neg2 < neg1) {
|
||||
printf("-10 < -5 = true\n");
|
||||
}
|
||||
if (neg1 < 0) {
|
||||
printf("-5 < 0 = true\n");
|
||||
}
|
||||
printf("PASS: Negative number comparisons work\n");
|
||||
|
||||
printf("Test 6: Comparisons in expressions\n");
|
||||
int result = a < b;
|
||||
printf("result of (10 < 20) = %d\n", result);
|
||||
int result2 = a > b;
|
||||
printf("result of (10 > 20) = %d\n", result2);
|
||||
printf("PASS: Comparisons as expressions work\n");
|
||||
|
||||
printf("\n=== All Comparison Operators Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
int main() {
|
||||
printf("=== File Seek Operations Tests ===\n");
|
||||
|
||||
printf("Test 1: Create test file\n");
|
||||
int f = fopen("seek_test.txt", "w");
|
||||
if (f == 0) {
|
||||
printf("ERROR: Could not create file\n");
|
||||
return 1;
|
||||
}
|
||||
fputs(f, "0123456789ABCDEFGHIJ");
|
||||
fclose(f);
|
||||
printf("PASS: File created with content\n");
|
||||
|
||||
printf("Test 2: ftell() - get current position\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
int pos = ftell(f);
|
||||
printf("Initial position: %d\n", pos);
|
||||
char *line = fgets(f, 5);
|
||||
printf("Read: '%s'\n", line);
|
||||
pos = ftell(f);
|
||||
printf("Position after reading 5 bytes: %d\n", pos);
|
||||
fclose(f);
|
||||
printf("PASS: ftell() works\n");
|
||||
|
||||
printf("Test 3: fseek() with SEEK_SET\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
fseek(f, 10, SEEK_SET());
|
||||
pos = ftell(f);
|
||||
printf("Position after fseek(10, SEEK_SET): %d\n", pos);
|
||||
line = fgets(f, 5);
|
||||
printf("Read from position 10: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: fseek with SEEK_SET works\n");
|
||||
|
||||
printf("Test 4: fseek() with SEEK_CUR\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
line = fgets(f, 5);
|
||||
printf("First read: '%s'\n", line);
|
||||
fseek(f, 3, SEEK_CUR());
|
||||
pos = ftell(f);
|
||||
printf("Position after fseek(3, SEEK_CUR): %d\n", pos);
|
||||
line = fgets(f, 5);
|
||||
printf("Read after seek: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: fseek with SEEK_CUR works\n");
|
||||
|
||||
printf("Test 5: fseek() with SEEK_END\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
fseek(f, -5, SEEK_END());
|
||||
pos = ftell(f);
|
||||
printf("Position after fseek(-5, SEEK_END): %d\n", pos);
|
||||
line = fgets(f, 10);
|
||||
printf("Read from near end: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: fseek with SEEK_END works\n");
|
||||
|
||||
printf("Test 6: Random access pattern\n");
|
||||
f = fopen("seek_test.txt", "r");
|
||||
fseek(f, 5, SEEK_SET());
|
||||
line = fgets(f, 3);
|
||||
printf("Position 5: '%s'\n", line);
|
||||
fseek(f, 0, SEEK_SET());
|
||||
line = fgets(f, 3);
|
||||
printf("Position 0: '%s'\n", line);
|
||||
fseek(f, 15, SEEK_SET());
|
||||
line = fgets(f, 3);
|
||||
printf("Position 15: '%s'\n", line);
|
||||
fclose(f);
|
||||
printf("PASS: Random access works\n");
|
||||
|
||||
printf("Test 7: Cleanup\n");
|
||||
fremove("seek_test.txt");
|
||||
printf("PASS: File removed\n");
|
||||
|
||||
printf("\n=== All File Seek Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
int getValue() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int square(int x) {
|
||||
int result = x * x;
|
||||
return result;
|
||||
}
|
||||
|
||||
int max(int a, int b) {
|
||||
if (a > b) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Functions Tests ===\n");
|
||||
printf("Testing no-parameter functions...\n");
|
||||
int val = getValue();
|
||||
printf("getValue() = %d\n", val);
|
||||
|
||||
printf("Testing two-parameter functions...\n");
|
||||
int sum = add(5, 3);
|
||||
printf("add(5, 3) = %d\n", sum);
|
||||
|
||||
printf("Testing functions with local variables...\n");
|
||||
int sq = square(7);
|
||||
printf("square(7) = %d\n", sq);
|
||||
|
||||
printf("Testing conditional functions...\n");
|
||||
int m1 = max(15, 20);
|
||||
printf("max(15, 20) = %d\n", m1);
|
||||
|
||||
printf("Testing nested function calls...\n");
|
||||
int nested = add(square(3), getValue());
|
||||
printf("add(square(3), getValue()) = %d\n", nested);
|
||||
|
||||
printf("\n=== All Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
int main() {
|
||||
printf("=== Logical Operators Tests ===\n");
|
||||
|
||||
printf("Test 1: AND operator (&&)\n");
|
||||
int a = 1;
|
||||
int b = 1;
|
||||
if (a && b) {
|
||||
printf("1 && 1 = true\n");
|
||||
}
|
||||
int c = 1;
|
||||
int d = 0;
|
||||
if (c && d) {
|
||||
printf("1 && 0 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("1 && 0 = false\n");
|
||||
}
|
||||
int e = 0;
|
||||
int f = 0;
|
||||
if (e && f) {
|
||||
printf("0 && 0 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("0 && 0 = false\n");
|
||||
}
|
||||
printf("PASS: AND operator works\n");
|
||||
|
||||
printf("Test 2: OR operator (||)\n");
|
||||
if (a || b) {
|
||||
printf("1 || 1 = true\n");
|
||||
}
|
||||
if (c || d) {
|
||||
printf("1 || 0 = true\n");
|
||||
}
|
||||
if (e || f) {
|
||||
printf("0 || 0 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("0 || 0 = false\n");
|
||||
}
|
||||
printf("PASS: OR operator works\n");
|
||||
|
||||
printf("Test 3: Combined logical operations\n");
|
||||
int x = 5;
|
||||
int y = 10;
|
||||
int z = 15;
|
||||
if (x < y && y < z) {
|
||||
printf("5 < 10 && 10 < 15 = true\n");
|
||||
}
|
||||
if (x > y || y < z) {
|
||||
printf("5 > 10 || 10 < 15 = true\n");
|
||||
}
|
||||
if (x > y && y > z) {
|
||||
printf("5 > 10 && 10 > 15 = true (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("5 > 10 && 10 > 15 = false\n");
|
||||
}
|
||||
printf("PASS: Combined logical operations work\n");
|
||||
|
||||
printf("Test 4: Logical operators in while loops\n");
|
||||
int i = 0;
|
||||
int j = 10;
|
||||
while (i < 5 && j > 5) {
|
||||
i = i + 1;
|
||||
j = j - 1;
|
||||
}
|
||||
printf("After loop with &&: i = %d, j = %d\n", i, j);
|
||||
printf("PASS: Logical operators in loops work\n");
|
||||
|
||||
printf("Test 5: Complex conditions\n");
|
||||
int age = 25;
|
||||
int hasLicense = 1;
|
||||
int hasInsurance = 1;
|
||||
if ((age >= 18 && hasLicense) && hasInsurance) {
|
||||
printf("Can drive: age >= 18, has license and insurance\n");
|
||||
}
|
||||
int temp = 30;
|
||||
if (temp < 0 || temp > 35) {
|
||||
printf("Extreme temperature (SHOULD NOT PRINT)\n");
|
||||
} else {
|
||||
printf("Normal temperature range\n");
|
||||
}
|
||||
printf("PASS: Complex conditions work\n");
|
||||
|
||||
printf("\n=== All Logical Operators Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
int main() {
|
||||
printf("=== Pointer Tests ===\n");
|
||||
|
||||
printf("Test 1: Address-of operator (&)\n");
|
||||
int x = 42;
|
||||
int addr = &x;
|
||||
printf("x = %d\n", x);
|
||||
printf("&x = %d\n", addr);
|
||||
printf("PASS: Address-of operator works\n");
|
||||
|
||||
printf("Test 2: Dereference operator (*)\n");
|
||||
int y = 100;
|
||||
int *ptr = &y;
|
||||
int val = *ptr;
|
||||
printf("y = %d\n", y);
|
||||
printf("*ptr = %d\n", val);
|
||||
printf("PASS: Dereference operator works\n");
|
||||
|
||||
printf("Test 3: Modify through pointer\n");
|
||||
int z = 50;
|
||||
int *p = &z;
|
||||
*p = 75;
|
||||
printf("After *p = 75, z = %d\n", z);
|
||||
printf("PASS: Pointer modification works\n");
|
||||
|
||||
printf("Test 4: Multiple pointers\n");
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int *pa = &a;
|
||||
int *pb = &b;
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
printf("*pa = %d, *pb = %d\n", *pa, *pb);
|
||||
int sum = *pa + *pb;
|
||||
printf("*pa + *pb = %d\n", sum);
|
||||
printf("PASS: Multiple pointers work\n");
|
||||
|
||||
printf("Test 5: Pointer with arrays\n");
|
||||
int arr[3];
|
||||
arr[0] = 1;
|
||||
arr[1] = 2;
|
||||
arr[2] = 3;
|
||||
printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n", arr[0], arr[1], arr[2]);
|
||||
int *arrptr = &arr;
|
||||
printf("Array base address: %d\n", arrptr);
|
||||
printf("PASS: Pointers with arrays work\n");
|
||||
|
||||
printf("\n=== All Pointer Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
int main() {
|
||||
printf("=== Trigonometric Functions Tests ===\n");
|
||||
printf("Note: Results are scaled by 1,000,000\n");
|
||||
|
||||
printf("Test 1: sin() function\n");
|
||||
int sin0 = sin(0);
|
||||
printf("sin(0) * 1000000 = %d\n", sin0);
|
||||
int sin1 = sin(1);
|
||||
printf("sin(1) * 1000000 = %d\n", sin1);
|
||||
int sin2 = sin(2);
|
||||
printf("sin(2) * 1000000 = %d\n", sin2);
|
||||
printf("PASS: sin() function works\n");
|
||||
|
||||
printf("Test 2: cos() function\n");
|
||||
int cos0 = cos(0);
|
||||
printf("cos(0) * 1000000 = %d\n", cos0);
|
||||
int cos1 = cos(1);
|
||||
printf("cos(1) * 1000000 = %d\n", cos1);
|
||||
int cos2 = cos(2);
|
||||
printf("cos(2) * 1000000 = %d\n", cos2);
|
||||
printf("PASS: cos() function works\n");
|
||||
|
||||
printf("Test 3: tan() function\n");
|
||||
int tan0 = tan(0);
|
||||
printf("tan(0) * 1000000 = %d\n", tan0);
|
||||
int tan1 = tan(1);
|
||||
printf("tan(1) * 1000000 = %d\n", tan1);
|
||||
printf("PASS: tan() function works\n");
|
||||
|
||||
printf("Test 4: Multiple angles\n");
|
||||
int i = 0;
|
||||
while (i < 4) {
|
||||
int s = sin(i);
|
||||
int c = cos(i);
|
||||
printf("Angle %d: sin = %d, cos = %d\n", i, s, c);
|
||||
i = i + 1;
|
||||
}
|
||||
printf("PASS: Multiple angle calculations work\n");
|
||||
|
||||
printf("Test 5: Trig with negative values\n");
|
||||
int sinNeg = sin(-1);
|
||||
printf("sin(-1) * 1000000 = %d\n", sinNeg);
|
||||
int cosNeg = cos(-1);
|
||||
printf("cos(-1) * 1000000 = %d\n", cosNeg);
|
||||
printf("PASS: Negative angle calculations work\n");
|
||||
|
||||
printf("\n=== All Trigonometric Tests Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user