mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-14 22:38:46 +00:00
Various quality-of-life improvements (ideas from @33577 )
* Very basic `hello_main.c` * Big starting hint to `write_c` * Better error messages on failed test * TODO: also improve it for asmgen, but not sure how to do it in a good way
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
int sum(int len, int *p) {
|
||||
int sum(int len, int* p) {
|
||||
int result = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
result += p[i];
|
||||
@@ -10,7 +10,7 @@ int sum(int len, int *p) {
|
||||
int main() {
|
||||
int a[5];
|
||||
int len = 5;
|
||||
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
a[i] = i;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
int* foo(int a[10]){
|
||||
int* foo(int a[10]) {
|
||||
return a;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a[10];
|
||||
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
(foo(a))[i] = i;
|
||||
(foo(a))[i] = i;
|
||||
}
|
||||
|
||||
return a[5] == 5;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
int main() {
|
||||
int a[10];
|
||||
int *p = a;
|
||||
|
||||
int* p = a;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
*(p++) = i;
|
||||
*(p++) = i;
|
||||
}
|
||||
|
||||
return a[5] == 5;
|
||||
|
||||
@@ -5,10 +5,10 @@ int main() {
|
||||
int a[5] = {init, 2, 3, 4, -5};
|
||||
int sum = 0;
|
||||
|
||||
for(int i = 0; i < 5; i++) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
sum += a[i];
|
||||
sum += g_a[i];
|
||||
}
|
||||
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
int bar(int x, int y, int z){
|
||||
int bar(int x, int y, int z) {
|
||||
int arith_mean = (x + y + z) / 3;
|
||||
int ugly_mean = (((x + y) / 2) * 2 + z) / 3;
|
||||
if (x == y) { return y; }
|
||||
else { return z; }
|
||||
if (x == y) {
|
||||
return y;
|
||||
} else {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -2,12 +2,12 @@ int main() {
|
||||
unsigned char a = -1;
|
||||
unsigned char b = -128;
|
||||
unsigned char c = 127;
|
||||
unsigned char d = b | a; // -1 (255)
|
||||
unsigned char e = b & a; // -128 (128)
|
||||
unsigned char f = b & c; // 0 (0)
|
||||
unsigned char g = b | c; // -1 (255)
|
||||
unsigned char h = -1 ^ -1; // 0 (0)
|
||||
unsigned char i = -1 ^ 0; // -1 (255)
|
||||
|
||||
unsigned char d = b | a; // -1 (255)
|
||||
unsigned char e = b & a; // -128 (128)
|
||||
unsigned char f = b & c; // 0 (0)
|
||||
unsigned char g = b | c; // -1 (255)
|
||||
unsigned char h = -1 ^ -1; // 0 (0)
|
||||
unsigned char i = -1 ^ 0; // -1 (255)
|
||||
|
||||
return d == 255 && e == 128 && f == 0 && g == 255 && h == 0 && i == 255;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
int int_greater_than(int i, unsigned int j) {
|
||||
if (i > j) return 1;
|
||||
else return 0;
|
||||
if (i > j)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int char_greater_than(char i, unsigned char j) {
|
||||
if (i > j) return 1;
|
||||
else return 0;
|
||||
if (i > j)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int y = 2;
|
||||
int x = (y += 2, 2, y + 3);
|
||||
return x == 7;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
|
||||
int main() {
|
||||
int a = ~nonce;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int y = 1;
|
||||
int x = 0;
|
||||
return ((x == y) ? 2 : 5) == 5;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
|
||||
int main() {
|
||||
int i;
|
||||
@@ -8,8 +8,11 @@ int main() {
|
||||
int loop_num = nonce % 100;
|
||||
|
||||
for (i = 0; i < loop_num; ((i % 2) ? (i += 2) : ++i)) {
|
||||
if (i % 2) { p += q; }
|
||||
else { p += r; }
|
||||
if (i % 2) {
|
||||
p += q;
|
||||
} else {
|
||||
p += r;
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
int (fibonacci)(int n) {
|
||||
int fibonacci(int n) {
|
||||
if (n < 2) {
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
|
||||
int fibonacci(int n) {
|
||||
if (n < 2) {
|
||||
|
||||
@@ -14,22 +14,22 @@ double average(int len, int a[10]) {
|
||||
int sum = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0; i < len; i++) {
|
||||
for (i = 0; i < len; i++) {
|
||||
sum += a[i];
|
||||
}
|
||||
|
||||
return (double) sum / len;
|
||||
return (double)sum / len;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a[10];
|
||||
int len = 10;
|
||||
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
a[i] = i;
|
||||
}
|
||||
|
||||
float avg = average(len, a);
|
||||
|
||||
|
||||
return is_close(avg, 4.5, 1e-09, 0.1);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
int foo(int x, int y, int z){
|
||||
if (x == y) { return y; }
|
||||
else { return z; }
|
||||
int foo(int x, int y, int z) {
|
||||
if (x == y) {
|
||||
return y;
|
||||
} else {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -4,6 +4,6 @@ int main() {
|
||||
int i = 0;
|
||||
int k = 0;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
int g = 10;
|
||||
|
||||
int foo(int, int k);
|
||||
|
||||
int main() {
|
||||
int i = g;
|
||||
|
||||
|
||||
return foo(i, i);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
int foo(int i, int j, int k) {
|
||||
return i + j + k;
|
||||
return i + j + k;
|
||||
}
|
||||
|
||||
int (* foo2())(int, int, int){
|
||||
return foo;
|
||||
int (*foo2())(int, int, int) {
|
||||
return foo;
|
||||
}
|
||||
|
||||
int (* (* foo3())())(int, int, int){
|
||||
return foo2;
|
||||
int (*(*foo3())())(int, int, int) {
|
||||
return foo2;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return foo3()()(2, 2, 2) == 6;
|
||||
return foo3()()(2, 2, 2) == 6;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
int foo() {
|
||||
int sum = 0;
|
||||
|
||||
for(int i = 0; ;) {
|
||||
if(i == 5) break;
|
||||
if(i == 3) {
|
||||
for (int i = 0;;) {
|
||||
if (i == 5)
|
||||
break;
|
||||
if (i == 3) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ int gcd(int a, int b) {
|
||||
a = (a > 0) ? a : -a;
|
||||
b = (b > 0) ? b : -b;
|
||||
|
||||
while(a != b) {
|
||||
if(a > b) { a -= b; }
|
||||
else { b -= a; }
|
||||
while (a != b) {
|
||||
if (a > b) {
|
||||
a -= b;
|
||||
} else {
|
||||
b -= a;
|
||||
}
|
||||
}
|
||||
|
||||
return a;
|
||||
|
||||
3
examples/c/hello_main.c
Normal file
3
examples/c/hello_main.c
Normal file
@@ -0,0 +1,3 @@
|
||||
int main() {
|
||||
return 1;
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
int main() {
|
||||
int i = 0;
|
||||
int result = 0;
|
||||
do {
|
||||
result = i;
|
||||
i++;
|
||||
} while (i < 4);
|
||||
return result;
|
||||
int i = 0;
|
||||
int result = 0;
|
||||
do {
|
||||
result = i;
|
||||
i++;
|
||||
} while (i < 4);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
int foo(int x, int y, int z){
|
||||
if (!(x == y)) { return y; }
|
||||
else { return z; }
|
||||
int foo(int x, int y, int z) {
|
||||
if (!(x == y)) {
|
||||
return y;
|
||||
} else {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
int* foo(int *a){
|
||||
return a;
|
||||
int* foo(int* a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int main() {
|
||||
int a = 1;
|
||||
int *p = &a;
|
||||
int **p2 = &*&p;
|
||||
int *p3 = *&p;
|
||||
int* p = &a;
|
||||
int** p2 = &*&p;
|
||||
int* p3 = *&p;
|
||||
|
||||
*&*foo(*p2) += 1;
|
||||
*foo(p3) += 1;
|
||||
|
||||
|
||||
return a == 3;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int x = nonce;
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int i;
|
||||
int sum = 0;
|
||||
for (i = 0; i < 11; ++i) {
|
||||
sum += i;
|
||||
for (i = 0; i < 11; ++i) {
|
||||
sum += i;
|
||||
}
|
||||
return sum == 55;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
int (fibonacci)(int n) {
|
||||
int fibonacci(int n) {
|
||||
if (n < 2) {
|
||||
n += 2;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int a = 3;
|
||||
int b = sizeof(!(a++));
|
||||
return a + b;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
typedef struct {
|
||||
char a;
|
||||
struct {
|
||||
int b[4][5];
|
||||
};
|
||||
double c;
|
||||
char a;
|
||||
struct {
|
||||
int b[4][5];
|
||||
};
|
||||
double c;
|
||||
} Temp;
|
||||
|
||||
void init(int row, int col, int arr[4][5]) {
|
||||
@@ -21,6 +21,6 @@ int main() {
|
||||
|
||||
Temp temp2;
|
||||
temp2 = temp;
|
||||
|
||||
|
||||
return temp2.b[2][3] == 6;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
typedef struct {
|
||||
char a;
|
||||
struct {
|
||||
int b[4];
|
||||
};
|
||||
long c;
|
||||
char a;
|
||||
struct {
|
||||
int b[4];
|
||||
};
|
||||
long c;
|
||||
} Temp;
|
||||
|
||||
int main() {
|
||||
const Temp temp = {1, {{2, 3, 4, 5}}, 6};
|
||||
|
||||
|
||||
Temp temp2;
|
||||
temp2 = temp;
|
||||
|
||||
int sum = temp2.a + temp2.b[2] + temp2.c;
|
||||
|
||||
|
||||
return sum == 11;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
|
||||
struct Foo
|
||||
{
|
||||
struct Foo {
|
||||
int x;
|
||||
};
|
||||
|
||||
struct Foo f()
|
||||
{
|
||||
struct Foo f() {
|
||||
struct Foo x;
|
||||
x.x = nonce;
|
||||
return x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int x = f().x;
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
int main() {
|
||||
int a = 1;
|
||||
int b = 2;
|
||||
do {
|
||||
int t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
} while (b == 1);
|
||||
return a * 10 + b;
|
||||
int a = 1;
|
||||
int b = 2;
|
||||
do {
|
||||
int t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
} while (b == 1);
|
||||
return a * 10 + b;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
int main() {
|
||||
int i = 0;
|
||||
int c = 0;
|
||||
while (i < 10) {
|
||||
i++;
|
||||
switch (i) {
|
||||
case (1): {
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
int i = 0;
|
||||
int c = 0;
|
||||
while (i < 10) {
|
||||
i++;
|
||||
switch (i) {
|
||||
case (1): {
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
c++;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
return c;
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,33 @@
|
||||
struct color { int number; char name; };
|
||||
struct color {
|
||||
int number;
|
||||
char name;
|
||||
};
|
||||
|
||||
int main() {
|
||||
int temp = 0;
|
||||
temp += sizeof(unsigned char);
|
||||
temp += _Alignof(unsigned char);
|
||||
int temp = 0;
|
||||
temp += sizeof(unsigned char);
|
||||
temp += _Alignof(unsigned char);
|
||||
|
||||
struct color c = {1, 2};
|
||||
temp += c.name;
|
||||
struct color *cp = &c;
|
||||
temp += cp->name;
|
||||
struct color c = {1, 2};
|
||||
temp += c.name;
|
||||
struct color* cp = &c;
|
||||
temp += cp->name;
|
||||
|
||||
for(int i = 0, j = 0; i < 10; ++i) {
|
||||
if ( i == 2 && j == 0) break;
|
||||
temp += i;
|
||||
}
|
||||
|
||||
switch(temp) {
|
||||
case 1: {
|
||||
temp = 0;
|
||||
break;
|
||||
for (int i = 0, j = 0; i < 10; ++i) {
|
||||
if (i == 2 && j == 0)
|
||||
break;
|
||||
temp += i;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return temp;
|
||||
switch (temp) {
|
||||
case 1: {
|
||||
temp = 0;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
char temp = 0x00L;
|
||||
|
||||
int main(){
|
||||
int main() {
|
||||
return (temp = 0xEF36L) >= (2L);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
int nonce = 1; // For random input
|
||||
int nonce = 1; // For random input
|
||||
|
||||
int foo() {
|
||||
int sum = 0;
|
||||
int i = 0;
|
||||
int continue_num = nonce % 98;
|
||||
|
||||
while(i < 100) {
|
||||
if(i == continue_num) {
|
||||
while (i < 100) {
|
||||
if (i == continue_num) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
sum += i;
|
||||
i++;
|
||||
|
||||
if(i == continue_num + 2) break;
|
||||
if (i == continue_num + 2)
|
||||
break;
|
||||
}
|
||||
|
||||
return sum;
|
||||
|
||||
13
examples/ir0/hello_main.ir
Normal file
13
examples/ir0/hello_main.ir
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
fun i32 @main () {
|
||||
init:
|
||||
bid: b0
|
||||
allocations:
|
||||
|
||||
|
||||
block b0:
|
||||
ret 1:i32
|
||||
|
||||
block b1:
|
||||
ret 1:i32
|
||||
}
|
||||
10
examples/ir1/hello_main.ir
Normal file
10
examples/ir1/hello_main.ir
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
fun i32 @main () {
|
||||
init:
|
||||
bid: b0
|
||||
allocations:
|
||||
|
||||
|
||||
block b0:
|
||||
ret 0:i32
|
||||
}
|
||||
10
examples/ir2/hello_main.ir
Normal file
10
examples/ir2/hello_main.ir
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
fun i32 @main () {
|
||||
init:
|
||||
bid: b0
|
||||
allocations:
|
||||
|
||||
|
||||
block b0:
|
||||
ret 1:i32
|
||||
}
|
||||
10
examples/ir3/hello_main.ir
Normal file
10
examples/ir3/hello_main.ir
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
fun i32 @main () {
|
||||
init:
|
||||
bid: b0
|
||||
allocations:
|
||||
|
||||
|
||||
block b0:
|
||||
ret 1:i32
|
||||
}
|
||||
10
examples/ir4/hello_main.ir
Normal file
10
examples/ir4/hello_main.ir
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
fun i32 @main () {
|
||||
init:
|
||||
bid: b0
|
||||
allocations:
|
||||
|
||||
|
||||
block b0:
|
||||
ret 1:i32
|
||||
}
|
||||
10
examples/opt/hello_main.ir
Normal file
10
examples/opt/hello_main.ir
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
fun i32 @main () {
|
||||
init:
|
||||
bid: b0
|
||||
allocations:
|
||||
|
||||
|
||||
block b0:
|
||||
ret 1:i32
|
||||
}
|
||||
Reference in New Issue
Block a user