├── testcases ├── step1 │ ├── no_newlines.c │ ├── return_0.c │ ├── return_2.c │ ├── multi_digit.c │ ├── spaces.c │ └── newlines.c ├── step2 │ ├── neg.c │ ├── not_five.c │ ├── not_zero.c │ ├── bitwise.c │ ├── bitwise_zero.c │ ├── nested_ops.c │ └── nested_ops_2.c ├── step5 │ ├── missing_return.c │ ├── refer.c │ ├── initialize.c │ ├── missing_return_2.c │ ├── no_initialize.c │ ├── unused_exp.c │ ├── main_var.c │ ├── assign.c │ ├── assign_init.c │ ├── assign_val.c │ ├── multiple_vars.c │ ├── missing_return_3.c │ ├── exp_return_val.c │ ├── initialize_logic_and.c │ └── multiple_returns.c ├── step3 │ ├── add.c │ ├── mult.c │ ├── sub.c │ ├── div.c │ ├── mod.c │ ├── sub_neg.c │ ├── unop_add.c │ ├── div_neg.c │ ├── mod_neg.c │ ├── mod_neg_1.c │ ├── mod_neg_2.c │ ├── parens.c │ ├── precedence.c │ ├── unop_parens.c │ ├── associativity.c │ ├── associativity_2.c │ ├── div_neg_1.c │ └── div_neg_2.c ├── step4 │ ├── and_false.c │ ├── and_true.c │ ├── eq_false.c │ ├── eq_true.c │ ├── ge_false.c │ ├── ge_true.c │ ├── gt_false.c │ ├── gt_true.c │ ├── le_false.c │ ├── le_true.c │ ├── lt_false.c │ ├── lt_true.c │ ├── ne_false.c │ ├── ne_true.c │ ├── or_false.c │ ├── or_true.c │ ├── logic_and.c │ ├── logic_or.c │ ├── lt_lt_true.c │ ├── and_logic_true.c │ ├── precedence.c │ ├── precedence_1.c │ ├── precedence_2.c │ ├── precedence_3.c │ └── precedence_4.c ├── step10 │ ├── global.c │ ├── multiple_global.c │ ├── global_not_initialized.c │ ├── global_assign.c │ ├── global_shadowed.c │ └── global_shadowed_2.c ├── step7 │ ├── ternary.c │ ├── assign_ternary.c │ ├── declare_block.c │ ├── ternary_assign.c │ ├── else.c │ ├── if_not_taken.c │ ├── if_taken.c │ ├── multiple_ternary.c │ ├── if_assign.c │ ├── rh_assignment.c │ ├── nested_ternary_2.c │ ├── ternary_short_circuit.c │ ├── ternary_short_circuit_2.c │ ├── nested_ternary.c │ ├── if_nested.c │ ├── if_nested_2.c │ ├── if_nested_3.c │ ├── if_nested_4.c │ ├── multi_nesting.c │ ├── if_nested_5.c │ ├── multiple_if.c │ ├── nested_if.c │ └── many_if.c ├── step11 │ ├── arr_1sz.c │ ├── arr_decl.c │ ├── arr_glob.c │ ├── arr_nestidx.c │ ├── arr_wr.c │ ├── arr_nlv.c │ ├── arr_big.c │ ├── arr_1sz_2.c │ ├── arr_rd.c │ ├── arr_2lv.c │ ├── arr_nlv_2.c │ ├── binomial.c │ ├── bubbsort.c │ ├── quicksort.c │ ├── fib.c │ └── matmul.c ├── step8 │ ├── return_in_while.c │ ├── continue_as_body.c │ ├── for.c │ ├── for_decl.c │ ├── while_single_statement.c │ ├── empty_expression.c │ ├── break.c │ ├── while_multi_statement.c │ ├── continue.c │ ├── break_multi_loops.c │ ├── nested_while.c │ ├── continue_empty_post.c │ ├── continue_multi_loops.c │ ├── for_variable_shadow.c │ └── nested_break.c ├── step9 │ ├── no_arg.c │ ├── precedence.c │ ├── single_arg.c │ ├── fun_shadowed_by_variable.c │ ├── multi_arg.c │ ├── variable_as_arg.c │ ├── assign_param.c │ ├── arg_fn_same_name.c │ ├── expression_args.c │ ├── arg_swap.c │ ├── fun_in_expr.c │ └── fib.c ├── step12 │ ├── global_init.c │ ├── array_init.c │ ├── pass_array.c │ ├── array_stack.c │ ├── bubbsort.c │ └── quicksort.c └── step6 │ ├── assign_int_block.c │ ├── declare_late.c │ ├── return_old.c │ ├── consecutive_blocks.c │ ├── declare_after_block.c │ ├── nested_scope.c │ ├── consecutive_declarations.c │ ├── modify_old_by_new.c │ ├── scaled_scope.c │ ├── nested_scope_10.c │ ├── nested_return_2.c │ └── nested_return.c ├── failcases ├── step5 │ ├── no_main.c │ ├── var_undefined.c │ ├── missing_semi.c │ ├── assign_to_rval.c │ ├── forward_assign.c │ ├── var_redefined.c │ └── assign_not_lvalue.c ├── step1 │ ├── badlex.c │ ├── badparse_2.c │ ├── no_main.c │ ├── badint_2.c │ └── tailing_trash.c ├── step11 │ ├── zero_array.c │ ├── vla.c │ ├── array_arith.c │ ├── array_arith2.c │ ├── array_assign.c │ ├── array_assign3.c │ ├── index_not_array.c │ ├── bad_type.c │ ├── array_assign2.c │ ├── array_cmp.c │ └── index_not_array2.c ├── step2 │ └── badparse.c ├── step3 │ ├── single_op.c │ ├── single_op2.c │ └── single_paren.c ├── step7 │ ├── badelse.c │ ├── badif.c │ ├── badparse.c │ ├── badquestion.c │ ├── decl_is_not_stmt.c │ ├── var_out_of_scope.c │ └── var_redefined.c ├── step9 │ ├── func_as_var.c │ ├── func_undefined.c │ ├── call_not_func.c │ ├── same_param_in_func_def.c │ ├── func_redefined.c │ ├── conflict_param.c │ ├── wrong_args_2.c │ ├── local_var_shadow_func_desg.c │ ├── wrong_args.c │ ├── var_shadows_param.c │ └── conflict_defs.c ├── step8 │ ├── illegal_break.c │ ├── bad_while_cond.c │ ├── illegal_continue.c │ └── bad_for_init.c ├── step12 │ ├── array_assign.c │ ├── array_assign3.c │ ├── array_assign4.c │ └── array_assign2.c ├── step10 │ ├── var_redefined.c │ ├── conflict_func_global.c │ ├── conflict_global_func.c │ └── bad_global_init2.c └── step6 │ ├── use_dropped_var.c │ ├── redifined_in_scope.c │ ├── return_a_block.c │ └── endless_scope.c ├── .gitignore ├── README-EN.md ├── README.md └── check.sh /testcases/step1/no_newlines.c: -------------------------------------------------------------------------------- 1 | int main(){return 0;} -------------------------------------------------------------------------------- /failcases/step5/no_main.c: -------------------------------------------------------------------------------- 1 | int f() { 2 | return 0; 3 | } -------------------------------------------------------------------------------- /testcases/step2/neg.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return -5; 3 | } -------------------------------------------------------------------------------- /testcases/step5/missing_return.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | 3 | } -------------------------------------------------------------------------------- /testcases/step1/return_0.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0; 3 | } -------------------------------------------------------------------------------- /testcases/step1/return_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2; 3 | } -------------------------------------------------------------------------------- /testcases/step2/not_five.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return !5; 3 | } -------------------------------------------------------------------------------- /testcases/step2/not_zero.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return !0; 3 | } -------------------------------------------------------------------------------- /testcases/step3/add.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 + 2; 3 | } -------------------------------------------------------------------------------- /testcases/step3/mult.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2 * 3; 3 | } -------------------------------------------------------------------------------- /testcases/step3/sub.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 - 2; 3 | } -------------------------------------------------------------------------------- /failcases/step1/badlex.c: -------------------------------------------------------------------------------- 1 | int 主函数() { 2 | return 0; 3 | } 4 | -------------------------------------------------------------------------------- /failcases/step1/badparse_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return; 3 | } 4 | -------------------------------------------------------------------------------- /failcases/step1/no_main.c: -------------------------------------------------------------------------------- 1 | int f() { 2 | return 0; 3 | } 4 | 5 | -------------------------------------------------------------------------------- /failcases/step11/zero_array.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[0]; 3 | } -------------------------------------------------------------------------------- /testcases/step1/multi_digit.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 100; 3 | } -------------------------------------------------------------------------------- /testcases/step1/spaces.c: -------------------------------------------------------------------------------- 1 | int main ( ) { return 0 ; } -------------------------------------------------------------------------------- /testcases/step2/bitwise.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return ~12; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step2/bitwise_zero.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return ~0; 3 | } -------------------------------------------------------------------------------- /testcases/step2/nested_ops.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return !-3; 3 | } -------------------------------------------------------------------------------- /testcases/step2/nested_ops_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return -~0; 3 | } -------------------------------------------------------------------------------- /testcases/step3/div.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 4 / 2; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/mod.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 5 % 3; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/sub_neg.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2- -1; 3 | } -------------------------------------------------------------------------------- /testcases/step3/unop_add.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return ~2 + 3; 3 | } -------------------------------------------------------------------------------- /testcases/step4/and_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 && 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/and_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 && -1; 3 | } -------------------------------------------------------------------------------- /testcases/step4/eq_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 == 2; 3 | } -------------------------------------------------------------------------------- /testcases/step4/eq_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 == 1; 3 | } -------------------------------------------------------------------------------- /testcases/step4/ge_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 >= 2; 3 | } -------------------------------------------------------------------------------- /testcases/step4/ge_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 >= 1; 3 | } -------------------------------------------------------------------------------- /testcases/step4/gt_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 > 1; 3 | } -------------------------------------------------------------------------------- /testcases/step4/gt_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 > 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/le_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 <= 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/le_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0 <= 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/lt_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 < 1; 3 | } -------------------------------------------------------------------------------- /testcases/step4/lt_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0 < 1; 3 | } -------------------------------------------------------------------------------- /testcases/step4/ne_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0 != 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/ne_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return -1 != -2; 3 | } -------------------------------------------------------------------------------- /testcases/step4/or_false.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0 || 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/or_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 || 0; 3 | } -------------------------------------------------------------------------------- /failcases/step2/badparse.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0!; 3 | } 4 | 5 | -------------------------------------------------------------------------------- /failcases/step3/single_op.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return % 5; 3 | } 4 | -------------------------------------------------------------------------------- /failcases/step3/single_op2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 10 /; 3 | } 4 | -------------------------------------------------------------------------------- /failcases/step3/single_paren.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return (2 + 3; 3 | } -------------------------------------------------------------------------------- /failcases/step7/badelse.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | else ; 3 | return 0; 4 | } -------------------------------------------------------------------------------- /testcases/step3/div_neg.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return -12 / -5; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/mod_neg.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return -5 % -3; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/mod_neg_1.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return -5 % 3; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/mod_neg_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 5 % -3; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/parens.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2 * (3 + 4); 3 | } -------------------------------------------------------------------------------- /testcases/step3/precedence.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2 + 3 * 4; 3 | } -------------------------------------------------------------------------------- /testcases/step3/unop_parens.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return ~(1 + 1); 3 | } -------------------------------------------------------------------------------- /testcases/step4/logic_and.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 7 && 3; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step4/logic_or.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 7 || 3; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step4/lt_lt_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0 < 1 < 2; 3 | } -------------------------------------------------------------------------------- /failcases/step1/badint_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2147483648; 3 | } 4 | -------------------------------------------------------------------------------- /failcases/step11/vla.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int n = 10; 3 | int a[n]; 4 | } -------------------------------------------------------------------------------- /failcases/step5/var_undefined.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return a; 3 | } 4 | 5 | -------------------------------------------------------------------------------- /failcases/step9/func_as_var.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return main + 1; 3 | } 4 | -------------------------------------------------------------------------------- /failcases/step9/func_undefined.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return f(); 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/associativity.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 - 2 - 3; 3 | } -------------------------------------------------------------------------------- /testcases/step3/associativity_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 6 / 3 / 2; 3 | } -------------------------------------------------------------------------------- /testcases/step3/div_neg_1.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return (-12) / 5; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step3/div_neg_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 12 / -5; 3 | } 4 | -------------------------------------------------------------------------------- /testcases/step4/and_logic_true.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 && 2; 3 | } -------------------------------------------------------------------------------- /testcases/step4/precedence.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 || 0 && 2; 3 | } -------------------------------------------------------------------------------- /testcases/step4/precedence_1.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 || 0 && 2; 3 | } -------------------------------------------------------------------------------- /testcases/step4/precedence_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return (1 || 0) && 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/precedence_3.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2 == 2 > 0; 3 | } -------------------------------------------------------------------------------- /testcases/step4/precedence_4.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 2 == 2 || 0; 3 | } -------------------------------------------------------------------------------- /testcases/step5/refer.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 2; 3 | return a; 4 | } -------------------------------------------------------------------------------- /failcases/step11/array_arith.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[10]; 3 | a + 10; 4 | } -------------------------------------------------------------------------------- /failcases/step7/badif.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | if(int a = 1) 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /testcases/step5/initialize.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 2; 3 | return 0; 4 | } -------------------------------------------------------------------------------- /testcases/step5/missing_return_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | 5; 3 | 6; 4 | } 5 | -------------------------------------------------------------------------------- /testcases/step5/no_initialize.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | return 0; 4 | } -------------------------------------------------------------------------------- /testcases/step5/unused_exp.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | 2 + 2; 3 | return 0; 4 | } -------------------------------------------------------------------------------- /failcases/step1/tailing_trash.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0; 3 | } 4 | 5 | trash 6 | -------------------------------------------------------------------------------- /failcases/step11/array_arith2.c: -------------------------------------------------------------------------------- 1 | int a[10]; 2 | int main() { 3 | return ~a; 4 | } 5 | -------------------------------------------------------------------------------- /failcases/step11/array_assign.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2]; 3 | a = {1, 2}; 4 | } -------------------------------------------------------------------------------- /failcases/step5/missing_semi.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 500 3 | return a; 4 | } -------------------------------------------------------------------------------- /failcases/step7/badparse.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 1 ? 2 : { 3; }; 3 | } 4 | 5 | -------------------------------------------------------------------------------- /failcases/step7/badquestion.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a = 1 ? 2; 3 | return a; 4 | } 5 | -------------------------------------------------------------------------------- /testcases/step5/main_var.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int main = 1; 3 | return main; 4 | } -------------------------------------------------------------------------------- /failcases/step11/array_assign3.c: -------------------------------------------------------------------------------- 1 | int a[5][5][5]; 2 | int main() { 3 | a[2] = 1; 4 | } 5 | -------------------------------------------------------------------------------- /failcases/step11/index_not_array.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | return a[1]; 4 | } 5 | -------------------------------------------------------------------------------- /failcases/step5/assign_to_rval.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | 1 = 1; 3 | return 0; 4 | } 5 | 6 | -------------------------------------------------------------------------------- /failcases/step8/illegal_break.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | break; 3 | return 0; 4 | } 5 | 6 | -------------------------------------------------------------------------------- /failcases/step9/call_not_func.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int x; 3 | return x(); 4 | } 5 | -------------------------------------------------------------------------------- /testcases/step10/global.c: -------------------------------------------------------------------------------- 1 | int foo = 4; 2 | 3 | int main() { 4 | return foo + 3; 5 | } -------------------------------------------------------------------------------- /testcases/step5/assign.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | a = 2; 4 | return a; 5 | } -------------------------------------------------------------------------------- /testcases/step5/assign_init.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = (a = 1); 3 | return a; 4 | } -------------------------------------------------------------------------------- /testcases/step7/ternary.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | return a > -1 ? 4 : 5; 4 | } -------------------------------------------------------------------------------- /failcases/step11/bad_type.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2][3][3]; 3 | return a[0][0]; 4 | } 5 | -------------------------------------------------------------------------------- /failcases/step5/forward_assign.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | a = 2; 3 | int a; 4 | return a; 5 | } 6 | -------------------------------------------------------------------------------- /failcases/step8/bad_while_cond.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | while (a) { 3 | int a = 0; 4 | } 5 | } -------------------------------------------------------------------------------- /failcases/step8/illegal_continue.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | continue; 3 | return 0; 4 | } 5 | 6 | -------------------------------------------------------------------------------- /failcases/step11/array_assign2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2]; 3 | int b[2]; 4 | a = b; 5 | } 6 | -------------------------------------------------------------------------------- /testcases/step11/arr_1sz.c: -------------------------------------------------------------------------------- 1 | int a[1]; 2 | int main() { 3 | a[0] =24; 4 | return a[0]; 5 | } 6 | -------------------------------------------------------------------------------- /testcases/step5/assign_val.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | int b = a = 0; 4 | return b; 5 | } -------------------------------------------------------------------------------- /testcases/step8/return_in_while.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | while (1) { 3 | return 2; 4 | } 5 | } -------------------------------------------------------------------------------- /failcases/step11/array_cmp.c: -------------------------------------------------------------------------------- 1 | int a[10]; 2 | int main() { 3 | int b[10]; 4 | return a != b; 5 | } 6 | -------------------------------------------------------------------------------- /failcases/step12/array_assign.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int b[10] = {}; 3 | b = 10; 4 | return b[0]; 5 | } -------------------------------------------------------------------------------- /failcases/step5/var_redefined.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | int a; 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /failcases/step8/bad_for_init.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | for (for (;;) { 3 | 4 | };;) { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /testcases/step1/newlines.c: -------------------------------------------------------------------------------- 1 | 2 | int 3 | main 4 | ( 5 | ) 6 | { 7 | return 8 | 0 9 | ; 10 | } -------------------------------------------------------------------------------- /testcases/step11/arr_decl.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2]; 3 | int b[2]; 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /failcases/step10/var_redefined.c: -------------------------------------------------------------------------------- 1 | int a = 1; 2 | int a = 2; 3 | 4 | int main() { 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /failcases/step12/array_assign3.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int b[10] = {}; 3 | int c = b; 4 | return b[0]; 5 | } -------------------------------------------------------------------------------- /testcases/step10/multiple_global.c: -------------------------------------------------------------------------------- 1 | int a = 3; 2 | int b = 4; 3 | 4 | int main() { 5 | return a * b; 6 | } -------------------------------------------------------------------------------- /testcases/step11/arr_glob.c: -------------------------------------------------------------------------------- 1 | int a[2]; 2 | int main() { 3 | a[0]=a[1]=5; 4 | return a[1]*a[0]; 5 | } 6 | -------------------------------------------------------------------------------- /testcases/step5/multiple_vars.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 2; 4 | return a + b; 5 | } -------------------------------------------------------------------------------- /testcases/step7/assign_ternary.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | a = 1 ? 2 : 3; 4 | return a; 5 | } -------------------------------------------------------------------------------- /testcases/step9/no_arg.c: -------------------------------------------------------------------------------- 1 | int three(){ 2 | return 3; 3 | } 4 | 5 | int main() { 6 | return three(); 7 | } -------------------------------------------------------------------------------- /failcases/step11/index_not_array2.c: -------------------------------------------------------------------------------- 1 | int a[2][3][3][3]; 2 | int main() { 3 | return a[1][0][0][1][0]; 4 | } 5 | -------------------------------------------------------------------------------- /failcases/step7/decl_is_not_stmt.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | if (1) 3 | int a; 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /failcases/step9/same_param_in_func_def.c: -------------------------------------------------------------------------------- 1 | int f(int x, int x) { return x + x; } 2 | int main() { return f(1, 2); } 3 | -------------------------------------------------------------------------------- /testcases/step5/missing_return_3.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int x = 5; 3 | x=6; 4 | int y; 5 | 6; 6 | } 7 | -------------------------------------------------------------------------------- /testcases/step9/precedence.c: -------------------------------------------------------------------------------- 1 | int three() { 2 | return 3; 3 | } 4 | 5 | int main() { 6 | return !three(); 7 | } -------------------------------------------------------------------------------- /failcases/step10/conflict_func_global.c: -------------------------------------------------------------------------------- 1 | int f; 2 | 3 | int f() {} 4 | 5 | int main() 6 | { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /failcases/step10/conflict_global_func.c: -------------------------------------------------------------------------------- 1 | int f() {} 2 | 3 | int f; 4 | 5 | int main() 6 | { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /failcases/step12/array_assign4.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int b[10] = {}; 3 | int c; 4 | c = b; 5 | return b[0]; 6 | } -------------------------------------------------------------------------------- /failcases/step5/assign_not_lvalue.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | a = ((a = 2) = (a = 1)); 4 | return a; 5 | } -------------------------------------------------------------------------------- /testcases/step5/exp_return_val.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | int b; 4 | a = b = 4; 5 | return a - b; 6 | } -------------------------------------------------------------------------------- /testcases/step5/initialize_logic_and.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 2; 3 | int b = a && 1; 4 | return a; 5 | } -------------------------------------------------------------------------------- /testcases/step7/declare_block.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | if (5) { 3 | int i = 0; 4 | return i; 5 | } 6 | } -------------------------------------------------------------------------------- /testcases/step7/ternary_assign.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | a = (a = 1 ? (a = 0): (a = 2)); 4 | return a; 5 | } -------------------------------------------------------------------------------- /failcases/step7/var_out_of_scope.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | if (1) { 3 | int a; 4 | } 5 | return a; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /failcases/step9/func_redefined.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0; 3 | } 4 | 5 | int main() { 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /testcases/step11/arr_nestidx.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2]; 3 | a[0]=1; 4 | a[1]=0; 5 | return a[a[0]]; 6 | } 7 | -------------------------------------------------------------------------------- /testcases/step5/multiple_returns.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 4; 3 | return 5; 4 | return 3+(1-2)*10 && 14*2; 5 | } 6 | -------------------------------------------------------------------------------- /testcases/step9/single_arg.c: -------------------------------------------------------------------------------- 1 | int twice(int x){ 2 | return 2 * x; 3 | } 4 | 5 | int main() { 6 | return twice(3); 7 | } -------------------------------------------------------------------------------- /testcases/step11/arr_wr.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2]; 3 | a[0] = 1; 4 | a[1] = a[0] + 5; 5 | return a[1]; 6 | } 7 | -------------------------------------------------------------------------------- /testcases/step12/global_init.c: -------------------------------------------------------------------------------- 1 | int arr[4] = {3, 2, 1}; 2 | 3 | int main(){ 4 | return arr[0] * arr[3] + arr[1] / arr[2]; 5 | } -------------------------------------------------------------------------------- /testcases/step7/else.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | if (a) 4 | return 1; 5 | else 6 | return 2; 7 | } -------------------------------------------------------------------------------- /failcases/step10/bad_global_init2.c: -------------------------------------------------------------------------------- 1 | int A() { 2 | return 1; 3 | } 4 | 5 | int B = A(); 6 | 7 | int main() { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /failcases/step6/use_dropped_var.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | { 4 | int b = -1; 5 | } 6 | return a + b; 7 | } -------------------------------------------------------------------------------- /failcases/step9/conflict_param.c: -------------------------------------------------------------------------------- 1 | int f(int x, int x) { 2 | return x; 3 | } 4 | 5 | int main() { 6 | return f(1, 2); 7 | } 8 | -------------------------------------------------------------------------------- /failcases/step9/wrong_args_2.c: -------------------------------------------------------------------------------- 1 | int f(int x, int y) { 2 | return x + y; 3 | } 4 | 5 | int main() { 6 | return f(1); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /testcases/step7/if_not_taken.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | int b = 0; 4 | if (a) 5 | b = 1; 6 | return b; 7 | } -------------------------------------------------------------------------------- /testcases/step7/if_taken.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 0; 4 | if (a) 5 | b = 1; 6 | return b; 7 | } -------------------------------------------------------------------------------- /testcases/step7/multiple_ternary.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1 > 2 ? 3 : 4; 3 | int b = 1 > 2 ? 5 : 6; 4 | return a + b; 5 | } -------------------------------------------------------------------------------- /testcases/step8/continue_as_body.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | for (int i = 0; i < 10; i = i + 1) 3 | continue; 4 | return 0; 5 | } -------------------------------------------------------------------------------- /failcases/step9/local_var_shadow_func_desg.c: -------------------------------------------------------------------------------- 1 | int test() { return 2; } 2 | int main() { 3 | int test = 4; 4 | return test(); 5 | } 6 | -------------------------------------------------------------------------------- /failcases/step9/wrong_args.c: -------------------------------------------------------------------------------- 1 | int f(int x, int y) { 2 | return x + y; 3 | } 4 | 5 | int main() { 6 | return f(1, 2, 3); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /testcases/step7/if_assign.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a = 0; 3 | int c = 1; 4 | if(c == (a = 1)) 5 | c = -10; 6 | return a + c; 7 | } 8 | -------------------------------------------------------------------------------- /testcases/step7/rh_assignment.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int flag = 1; 3 | int a = 0; 4 | flag ? a = 1 : (a = 0); 5 | return a; 6 | } -------------------------------------------------------------------------------- /testcases/step8/for.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | 4 | for (a = 0; a < 3; a = a + 1) 5 | a = a * 2; 6 | return a; 7 | } -------------------------------------------------------------------------------- /failcases/step9/var_shadows_param.c: -------------------------------------------------------------------------------- 1 | int f(int x) { 2 | int x = 1; 3 | return x; 4 | } 5 | 6 | int main() { 7 | return f(1); 8 | } 9 | -------------------------------------------------------------------------------- /testcases/step6/assign_int_block.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 233; 3 | { 4 | int a = (a = 1); 5 | return a; 6 | } 7 | } -------------------------------------------------------------------------------- /testcases/step6/declare_late.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 2; 3 | { 4 | a = 3; 5 | int a = 0; 6 | } 7 | return a; 8 | } -------------------------------------------------------------------------------- /testcases/step7/nested_ternary_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1 ? 2 ? 3 : 4 : 5; 3 | int b = 0 ? 2 ? 3 : 4 : 5; 4 | return a * b; 5 | } -------------------------------------------------------------------------------- /testcases/step7/ternary_short_circuit.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 0; 4 | a ? (b = 1) : (b = 2); 5 | return b; 6 | } -------------------------------------------------------------------------------- /testcases/step7/ternary_short_circuit_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | int b = 0; 4 | a ? (b = 1) : (b = 2); 5 | return b; 6 | } -------------------------------------------------------------------------------- /testcases/step9/fun_shadowed_by_variable.c: -------------------------------------------------------------------------------- 1 | int foo() { 2 | return 3; 3 | } 4 | 5 | int main() { 6 | int foo = 5; 7 | return foo; 8 | } -------------------------------------------------------------------------------- /testcases/step6/return_old.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 3; 4 | { 5 | int b = 5; 6 | } 7 | return a + b; 8 | } -------------------------------------------------------------------------------- /testcases/step8/for_decl.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | 4 | for (int i = 0; i < 3; i = i + 1) 5 | a = a + 1; 6 | return a; 7 | } -------------------------------------------------------------------------------- /testcases/step8/while_single_statement.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | 4 | while (a < 5) 5 | a = a + 2; 6 | 7 | return a; 8 | } -------------------------------------------------------------------------------- /testcases/step9/multi_arg.c: -------------------------------------------------------------------------------- 1 | int sub_3(int x, int y, int z) { 2 | return x - y - z; 3 | } 4 | 5 | int main() { 6 | return sub_3(10, 4, 2); 7 | } -------------------------------------------------------------------------------- /testcases/step9/variable_as_arg.c: -------------------------------------------------------------------------------- 1 | int foo(int x) { 2 | return x + 1; 3 | } 4 | 5 | int main() { 6 | int a = 1; 7 | return foo(a); 8 | } 9 | -------------------------------------------------------------------------------- /testcases/step6/consecutive_blocks.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | { 4 | int a = 2; 5 | } 6 | { 7 | return a; 8 | } 9 | } -------------------------------------------------------------------------------- /testcases/step6/declare_after_block.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int i = 0; 3 | { 4 | int a = 2; 5 | } 6 | int b = 3; 7 | return b; 8 | } -------------------------------------------------------------------------------- /testcases/step7/nested_ternary.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 2; 4 | int flag = 0; 5 | 6 | return a > b ? 5 : flag ? 6 : 7; 7 | } -------------------------------------------------------------------------------- /testcases/step8/empty_expression.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int i = 3; 3 | ; 4 | for (int i = 0; i < 10; i = i + 1) 5 | ; 6 | return i; 7 | } -------------------------------------------------------------------------------- /testcases/step9/assign_param.c: -------------------------------------------------------------------------------- 1 | int fun(int x) { 2 | return x = 5; 3 | } 4 | 5 | int main() { 6 | int x = 0; 7 | fun(5); 8 | return x; 9 | } -------------------------------------------------------------------------------- /failcases/step12/array_assign2.c: -------------------------------------------------------------------------------- 1 | int fun(int a){ 2 | return a; 3 | } 4 | 5 | int main(){ 6 | int b[10] = {}; 7 | fun(b); 8 | return b[0]; 9 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # generated files 2 | 3 | *.s 4 | *.actual 5 | *.expected 6 | *.gcc 7 | *.my 8 | *.err 9 | no_compiler_set 10 | .vscode/ 11 | 12 | !runtime.s 13 | -------------------------------------------------------------------------------- /failcases/step7/var_redefined.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | if (1) { 4 | int a; 5 | int a; 6 | } 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /testcases/step10/global_not_initialized.c: -------------------------------------------------------------------------------- 1 | int foo; 2 | 3 | int main() { 4 | for (int i = 0; i < 3; i = i + 1) 5 | foo = foo + 1; 6 | return foo; 7 | } -------------------------------------------------------------------------------- /testcases/step11/arr_nlv.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a[2][3][4]; 3 | a[1][2][3] = 5; 4 | a[0][0][2] = a[1][2][3]; 5 | return a[1][2][3] + a[0][0][2]; 6 | } 7 | -------------------------------------------------------------------------------- /testcases/step6/nested_scope.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 2; 3 | int b = 3; 4 | { 5 | int a = 1; 6 | b = b + a; 7 | } 8 | return b; 9 | } -------------------------------------------------------------------------------- /failcases/step6/redifined_in_scope.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | { 4 | a = 2; 5 | int a = 1; 6 | int a = 2; 7 | } 8 | return a; 9 | } -------------------------------------------------------------------------------- /testcases/step11/arr_big.c: -------------------------------------------------------------------------------- 1 | int a[1048576]; 2 | int main() { 3 | for (int i =0;i<1048576;i=1+i) 4 | a[i] = 1048576-i; 5 | return a[142123] + a[564432]; 6 | } 7 | -------------------------------------------------------------------------------- /testcases/step7/if_nested.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 0; 4 | if (a) 5 | b = 1; 6 | else if (b) 7 | b = 2; 8 | return b; 9 | } -------------------------------------------------------------------------------- /testcases/step7/if_nested_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | int b = 1; 4 | if (a) 5 | b = 1; 6 | else if (b) 7 | b = 2; 8 | return b; 9 | } -------------------------------------------------------------------------------- /testcases/step9/arg_fn_same_name.c: -------------------------------------------------------------------------------- 1 | int f(int f) { 2 | return f; 3 | } 4 | 5 | int main() { 6 | if (f(6) != 6) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /testcases/step9/expression_args.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a + b; 3 | } 4 | 5 | int main() { 6 | int sum = add(1 + 2, 4); 7 | return sum + sum; 8 | } 9 | -------------------------------------------------------------------------------- /testcases/step10/global_assign.c: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | int fun() { 4 | return a = 5; 5 | } 6 | 7 | int main() { 8 | int b = a; 9 | fun(); 10 | return a + b; 11 | } -------------------------------------------------------------------------------- /testcases/step11/arr_1sz_2.c: -------------------------------------------------------------------------------- 1 | int a[1]; 2 | int main() { 3 | int b[1][1][1][1][1][1]; 4 | a[0] = b[0][0][0][0][0][0] = 24; 5 | return a[0] + b[0][0][0][0][0][0]; 6 | } 7 | -------------------------------------------------------------------------------- /testcases/step10/global_shadowed.c: -------------------------------------------------------------------------------- 1 | int a = 3; 2 | 3 | int main() { 4 | int ret = 0; 5 | if (a) { 6 | int a = 0; 7 | ret = 4; 8 | } 9 | return ret; 10 | } -------------------------------------------------------------------------------- /testcases/step10/global_shadowed_2.c: -------------------------------------------------------------------------------- 1 | int a = 3; 2 | 3 | int main() { 4 | int ret = 0; 5 | if (a) { 6 | int a = 0; 7 | ret = a; 8 | } 9 | return ret; 10 | } -------------------------------------------------------------------------------- /testcases/step12/array_init.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int b[10] = {3, 4, 5}; 3 | int ans = 0; 4 | for(int i = 0; i < 10; i = i + 1){ 5 | ans = ans + b[i]; 6 | } 7 | return ans; 8 | } -------------------------------------------------------------------------------- /testcases/step7/if_nested_3.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | if (1) 4 | if (2) 5 | a = 3; 6 | else 7 | a = 4; 8 | 9 | return a; 10 | } -------------------------------------------------------------------------------- /testcases/step7/if_nested_4.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | if (1) 4 | if (0) 5 | a = 3; 6 | else 7 | a = 4; 8 | 9 | return a; 10 | } -------------------------------------------------------------------------------- /failcases/step9/conflict_defs.c: -------------------------------------------------------------------------------- 1 | int f(int x) { 2 | return x + 1; 3 | } 4 | 5 | int f(int x, int y) { 6 | return x + y; 7 | } 8 | 9 | int main() { 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /testcases/step9/arg_swap.c: -------------------------------------------------------------------------------- 1 | int g(int a, int b) { 2 | return a + b; 3 | } 4 | 5 | int f(int x, int y) { 6 | return g(y, x); 7 | } 8 | 9 | int main() { 10 | return f(2, 3); 11 | } -------------------------------------------------------------------------------- /failcases/step6/return_a_block.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | { 4 | return 5 | { 6 | int b = 2; 7 | b 8 | }; 9 | } 10 | return a; 11 | } -------------------------------------------------------------------------------- /testcases/step7/multi_nesting.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a = 2; 3 | if (a < 3) { 4 | { 5 | int a = 3; 6 | return a; 7 | } 8 | return a; 9 | } 10 | } -------------------------------------------------------------------------------- /testcases/step8/break.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int sum = 0; 3 | for (int i = 0; i < 10; i = i + 1) { 4 | sum = sum + i; 5 | if (sum > 10) 6 | break; 7 | } 8 | return sum; 9 | } -------------------------------------------------------------------------------- /testcases/step8/while_multi_statement.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | int b = 1; 4 | 5 | while (a < 5) { 6 | a = a + 2; 7 | b = b * a; 8 | } 9 | 10 | return a; 11 | } -------------------------------------------------------------------------------- /testcases/step9/fun_in_expr.c: -------------------------------------------------------------------------------- 1 | int sum(int a, int b) { 2 | return a + b; 3 | } 4 | 5 | int main() { 6 | int a = sum(1, 2) - (sum(1, 2) / 2) * 2; 7 | int b = 2*sum(3, 4) + sum(1, 2); 8 | return b - a; 9 | } -------------------------------------------------------------------------------- /testcases/step7/if_nested_5.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | if (0) 4 | if (0) 5 | a = 3; 6 | else 7 | a = 4; 8 | else 9 | a = 1; 10 | 11 | return a; 12 | } -------------------------------------------------------------------------------- /testcases/step8/continue.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int sum = 0; 3 | for (int i = 0; i < 10; i = i + 1) { 4 | if ((sum / 2) * 2 != sum) 5 | continue; 6 | sum = sum + i; 7 | } 8 | return sum; 9 | } -------------------------------------------------------------------------------- /testcases/step6/consecutive_declarations.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | { 4 | int b = 1; 5 | a = b; 6 | } 7 | { 8 | int b = 2; 9 | a = a + b; 10 | } 11 | return a; 12 | } -------------------------------------------------------------------------------- /testcases/step11/arr_rd.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2]; 3 | int b[2]; 4 | b[0] + b[1]; 5 | b[1]; 6 | if (a[0] != a[0]) 7 | return 1; 8 | if (a[1] != a[1]) 9 | return 1; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /testcases/step8/break_multi_loops.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int sum = 0; 3 | for (int i = 0; i < 10; i = i + 1) { 4 | for (int i = 0; i < 10; i = i + 1) 5 | break; 6 | sum = sum + i; 7 | } 8 | return sum; 9 | } -------------------------------------------------------------------------------- /testcases/step8/nested_while.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | 4 | while (a / 3 < 20) { 5 | int b = 1; 6 | while (b < 10) 7 | b = b*2; 8 | a = a + b; 9 | } 10 | 11 | return a; 12 | } -------------------------------------------------------------------------------- /testcases/step6/modify_old_by_new.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 2; 4 | { 5 | int a; 6 | a = 7; 7 | } 8 | { 9 | int b = 222; 10 | a = b + 1; 11 | } 12 | return a + b; 13 | } -------------------------------------------------------------------------------- /testcases/step8/continue_empty_post.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int sum = 0; 3 | for (int i = 0; i < 10; i) { 4 | i = i + 1; 5 | if (i % 2) 6 | continue; 7 | sum = sum + i; 8 | } 9 | return sum; 10 | } -------------------------------------------------------------------------------- /testcases/step8/continue_multi_loops.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int sum = 0; 3 | for (int i = 0; i < 10; i = i + 1) { 4 | for (int i = 0; i < 10; i = i + 1) 5 | continue; 6 | sum = sum + i; 7 | } 8 | return sum; 9 | } -------------------------------------------------------------------------------- /testcases/step9/fib.c: -------------------------------------------------------------------------------- 1 | int fib(int n) { 2 | if (n == 0 || n == 1) { 3 | return n; 4 | } else { 5 | return fib(n - 1) + fib(n - 2); 6 | } 7 | } 8 | 9 | int main() { 10 | int n = 5; 11 | return fib(n); 12 | } -------------------------------------------------------------------------------- /testcases/step7/multiple_if.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | int b = 0; 4 | 5 | if (a) 6 | a = 2; 7 | else 8 | a = 3; 9 | 10 | if (b) 11 | b = 4; 12 | else 13 | b = 5; 14 | 15 | return a + b; 16 | } -------------------------------------------------------------------------------- /testcases/step8/for_variable_shadow.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int i = 0; 3 | int j = 0; 4 | for (i = 0; i < 10; i = i + 1) { 5 | int k = i; 6 | for (int i = k; i < 10; i = i + 1) 7 | j = j + 1; 8 | } 9 | return j + i; 10 | } -------------------------------------------------------------------------------- /testcases/step11/arr_2lv.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a[2][2]; 3 | int k = 0; 4 | for (int i = 0; i < 2; i = i + 1) 5 | for (int j = 0; j < 2; j = j + 1) 6 | a[i][j] = k=k+1; 7 | return a[0][0]*40 + a[0][1] * 20 + a[1][0] * 10 + a[1][1]; 8 | } 9 | -------------------------------------------------------------------------------- /testcases/step12/pass_array.c: -------------------------------------------------------------------------------- 1 | int f(int a[]){ 2 | a[0] = 10; 3 | return a[1] + a[2] * 2 + a[3] * 3; 4 | } 5 | 6 | int main(){ 7 | int b[4]; 8 | b[1] = 2; 9 | b[2] = 3; 10 | b[3] = 4; 11 | int ans = f(b); 12 | ans = ans + b[0] * 2; 13 | return ans; 14 | } -------------------------------------------------------------------------------- /failcases/step6/endless_scope.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | { 4 | int a = 1; 5 | { 6 | int a = 1; 7 | { 8 | int a = 1; 9 | { 10 | 11 | } 12 | } 13 | } 14 | return a; 15 | } -------------------------------------------------------------------------------- /testcases/step12/array_stack.c: -------------------------------------------------------------------------------- 1 | int fun(){ 2 | int array[100] = {100}; 3 | return array[10] + array[0]; 4 | } 5 | 6 | int main(){ 7 | int b[10] = {3, 4, 5}; 8 | int ans = fun(); 9 | for(int i = 0; i < 10; i = i + 1){ 10 | ans = ans + b[i]; 11 | } 12 | return ans; 13 | } -------------------------------------------------------------------------------- /testcases/step8/nested_break.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int ans = 0; 3 | for (int i = 0; i < 10; i = i + 1) 4 | for (int j = 0; j < 10; j = j + 1) 5 | if ((i / 2)*2 == i) 6 | break; 7 | else 8 | ans = ans + i; 9 | return ans; 10 | } -------------------------------------------------------------------------------- /testcases/step7/nested_if.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 0; 3 | if (a) { 4 | int b = 2; 5 | return b; 6 | } else { 7 | int c = 3; 8 | if (a < c) { 9 | return 4; 10 | } else { 11 | return 5; 12 | } 13 | } 14 | return a; 15 | } -------------------------------------------------------------------------------- /testcases/step11/arr_nlv_2.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a[2][3][4]; 3 | int r = 1; 4 | for (int i = 0; i < 2; i = i + 1) 5 | for (int j = 0; j < 3; j=j+1) { 6 | a[i][j][0] = i*6-j*3; 7 | for (int k = 1; k < 4; k=k+ 1) { 8 | r = r * 2 + 1; 9 | a[i][j][k] = a[i][j][k-1] + r; 10 | } 11 | } 12 | return a[1][2][3] + a[0][0][2]; 13 | } 14 | -------------------------------------------------------------------------------- /testcases/step6/scaled_scope.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 0; 4 | { 5 | int a = 2; 6 | { 7 | int b = 98; 8 | int a = 4; 9 | } 10 | { 11 | int a = 6; 12 | int b = 0; 13 | } 14 | b = b + a; 15 | } 16 | { 17 | int a = 5; 18 | { 19 | int a = 7; 20 | b = b + a; 21 | } 22 | 23 | } 24 | return b + a; 25 | } -------------------------------------------------------------------------------- /testcases/step7/many_if.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a = 0; 3 | if(a < 1) 4 | a = 2; 5 | if(a == 2) 6 | if(a != 3) 7 | if(a != 4) 8 | if(a != 5) 9 | if(a != 6) 10 | if(a != 2) 11 | a = 4; 12 | else 13 | a = 1; 14 | else 15 | a = 3; 16 | else 17 | a = 5; 18 | else 19 | a = 6; 20 | else 21 | a = 7; 22 | else 23 | a = 8; 24 | return a; 25 | } 26 | -------------------------------------------------------------------------------- /testcases/step11/binomial.c: -------------------------------------------------------------------------------- 1 | int C[2][10000]; 2 | int P = 10000007; 3 | 4 | int main() { 5 | int n = 5996; 6 | 7 | C[0][0] = 1; 8 | int b = 0; 9 | for (int i = 1; i <= n; i = i + 1) { 10 | for (int j = 0; j <= i; j = j + 1) 11 | C[1 - b][j] = !j ? 1 : (C[b][j] + C[b][j - 1]) % P; 12 | b = 1 - b; 13 | } 14 | 15 | if (C[b][0] != 1 || C[b][n] != 1 || C[1 - b][1] != n - 1) return 1; 16 | if (C[b][1234] != 6188476) return 2; 17 | if (C[b][2333] != 9957662) return 3; 18 | if (C[b][3456] != 9832509) return 4; 19 | if (C[b][5678] != 2436480) return 5; 20 | if (C[b][n / 2] != 7609783) return 6; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /README-EN.md: -------------------------------------------------------------------------------- 1 | # minidecaf-tests 2 | The test suits for minidecaf compiler. 3 | 4 | Recommended: install [GNU parallel](https://www.gnu.org/software/parallel/) to speed up checking by 3x more. 5 | Any version should be fine. 6 | 7 | Usage: just `./check.sh`. For each test case, 8 | - `OK` indicates success; 9 | - `FAIL` indicates divergence between your output and standard answer; 10 | - `ERR` indicates that your compiler crashed, or produced malformed assembly. 11 | 12 | In the same directory as the input files are the diagnostics, e.g. for `xx.c`: 13 | - `xx.err`: in case of `ERR`, the error message 14 | - `xx.expected`: the standard answer (by gcc) 15 | - `xx.actual`: your output which you can diff against `xx.expected` 16 | - `xx.{gcc,my}`: executables 17 | 18 | Reference: 19 | - [Nora Sandler's compiler testsuits](https://github.com/nlsandler/write_a_c_compiler) 20 | -------------------------------------------------------------------------------- /testcases/step11/bubbsort.c: -------------------------------------------------------------------------------- 1 | int sorted_after = 500; 2 | int a[500]; 3 | 4 | int state; 5 | int rand() 6 | { 7 | return state = (state * 64013 + 1531011) % 32768; 8 | } 9 | 10 | int swap(int i, int j) 11 | { 12 | int tmp = a[i]; 13 | a[i] = a[j]; 14 | a[j] = tmp; 15 | return 0; 16 | } 17 | 18 | int bubblesort(int sorted_after) 19 | { 20 | for (int i = 0; i < sorted_after; i = i + 1) 21 | for (int j = i + 1; j < sorted_after; j = j + 1) 22 | if (a[i] > a[j]) 23 | swap(i, j); 24 | return 0; 25 | } 26 | 27 | int main() 28 | { 29 | int state = 218397121; 30 | for (int i = 0; i < sorted_after; i = i + 1) 31 | a[i] = rand(); 32 | 33 | int sorted_before = 1; 34 | for (int i = 0; i < sorted_after - 1; i = i + 1) 35 | if (a[i] > a[i + 1]) 36 | sorted_before = 0; 37 | 38 | bubblesort(sorted_after); 39 | 40 | int sorted_after = 1; 41 | for (int i = 0; i < sorted_after - 1; i = i + 1) 42 | if (a[i] > a[i + 1]) 43 | sorted_after = 0; 44 | return 200 + sorted_before * 10 + sorted_after; 45 | } 46 | -------------------------------------------------------------------------------- /testcases/step12/bubbsort.c: -------------------------------------------------------------------------------- 1 | int sorted_after = 500; 2 | int arr[500]; 3 | 4 | int state; 5 | int rand() 6 | { 7 | return state = (state * 64013 + 1531011) % 32768; 8 | } 9 | 10 | int swap(int a[], int i, int j) 11 | { 12 | int tmp = a[i]; 13 | a[i] = a[j]; 14 | a[j] = tmp; 15 | return 0; 16 | } 17 | 18 | int bubblesort(int a[], int sorted_after) 19 | { 20 | for (int i = 0; i < sorted_after; i = i + 1) 21 | for (int j = i + 1; j < sorted_after; j = j + 1) 22 | if (a[i] > a[j]) 23 | swap(a, i, j); 24 | return 0; 25 | } 26 | 27 | int main() 28 | { 29 | int state = 218397121; 30 | for (int i = 0; i < sorted_after; i = i + 1) 31 | arr[i] = rand(); 32 | 33 | int sorted_before = 1; 34 | for (int i = 0; i < sorted_after - 1; i = i + 1) 35 | if (arr[i] > arr[i + 1]) 36 | sorted_before = 0; 37 | 38 | bubblesort(arr, sorted_after); 39 | 40 | int sorted_after = 1; 41 | for (int i = 0; i < sorted_after - 1; i = i + 1) 42 | if (arr[i] > arr[i + 1]) 43 | sorted_after = 0; 44 | return 200 + sorted_before * 10 + sorted_after; 45 | } 46 | -------------------------------------------------------------------------------- /testcases/step11/quicksort.c: -------------------------------------------------------------------------------- 1 | int n = 1000000; 2 | int a[1000000]; 3 | 4 | int qsort(int l, int r) { 5 | int i = l; 6 | int j = r; 7 | int p = a[(l+r)/2]; 8 | int flag = 1; 9 | while (i <= j) { 10 | while (a[i] < p) i = i + 1; 11 | while (a[j] > p) j = j - 1; 12 | if (i > j) break; 13 | int u = a[i]; a[i] = a[j]; a[j] = u; 14 | i = i + 1; 15 | j = j - 1; 16 | } 17 | if (i < r) qsort(i, r); 18 | if (j > l) qsort(l, j); 19 | return 0; 20 | } 21 | 22 | int state; 23 | int rand() { 24 | state = (state * 64013 + 1531011) % 32768; 25 | return state % 1000; 26 | } 27 | 28 | int initArr(int n) { 29 | int i = 0; 30 | while (i < n) { 31 | a[i] = rand(); 32 | i = i + 1; 33 | } 34 | return 0; 35 | } 36 | 37 | int isSorted(int n) { 38 | int i = 0; 39 | while (i < n-1) { 40 | if ( a[i] > a[i+1] ) 41 | return 0; 42 | i = i + 1; 43 | } 44 | return 1; 45 | } 46 | 47 | int main() { 48 | initArr(n); 49 | int sorted_before = isSorted(n); 50 | qsort(0, n-1); 51 | int sorted_after = isSorted(n); 52 | if (!(sorted_before==0 && sorted_after==1)) 53 | return 1; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /testcases/step6/nested_scope_10.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 1; 4 | { 5 | a = a + 2; 6 | { 7 | int a = 3; 8 | { 9 | int a = 4; 10 | { 11 | int a = 5; 12 | { 13 | a = a + 6; 14 | { 15 | int a = 7; 16 | { 17 | a = a + 8; 18 | { 19 | int a = 9; 20 | { 21 | int a = 10; 22 | { 23 | int b = 1; 24 | } 25 | b = b + a; 26 | } 27 | } 28 | } 29 | } 30 | b = b + a; 31 | } 32 | int b = 4; 33 | } 34 | int b = 77; 35 | } 36 | } 37 | int b = 9; 38 | } 39 | return a + b; 40 | } -------------------------------------------------------------------------------- /testcases/step11/fib.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int fi[30]; 3 | fi[0] = 0; 4 | fi[1] = 1; 5 | for (int i = 2; i < 30; i = i + 1) 6 | fi[i] = fi[i - 2] + fi[i - 1]; 7 | 8 | if (fi[2] != 1) return 2; 9 | if (fi[3] != 2) return 3; 10 | if (fi[4] != 3) return 4; 11 | if (fi[5] != 5) return 5; 12 | if (fi[6] != 8) return 6; 13 | if (fi[7] != 13) return 7; 14 | if (fi[8] != 21) return 8; 15 | if (fi[9] != 34) return 9; 16 | if (fi[10] != 55) return 10; 17 | if (fi[11] != 89) return 11; 18 | if (fi[12] != 144) return 12; 19 | if (fi[13] != 233) return 13; 20 | if (fi[14] != 377) return 14; 21 | if (fi[15] != 610) return 15; 22 | if (fi[16] != 987) return 16; 23 | if (fi[17] != 1597) return 17; 24 | if (fi[18] != 2584) return 18; 25 | if (fi[19] != 4181) return 19; 26 | if (fi[20] != 6765) return 20; 27 | if (fi[21] != 10946) return 21; 28 | if (fi[22] != 17711) return 22; 29 | if (fi[23] != 28657) return 23; 30 | if (fi[24] != 46368) return 24; 31 | if (fi[25] != 75025) return 25; 32 | if (fi[26] != 121393) return 26; 33 | if (fi[27] != 196418) return 27; 34 | if (fi[28] != 317811) return 28; 35 | if (fi[29] != 514229) return 29; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /testcases/step12/quicksort.c: -------------------------------------------------------------------------------- 1 | int n = 1000000; 2 | int arr[1000000]; 3 | 4 | int qsort(int a[1000000], int l, int r) { 5 | int i = l; 6 | int j = r; 7 | int p = a[(l+r)/2]; 8 | int flag = 1; 9 | while (i <= j) { 10 | while (a[i] < p) i = i + 1; 11 | while (a[j] > p) j = j - 1; 12 | if (i > j) break; 13 | int u = a[i]; a[i] = a[j]; a[j] = u; 14 | i = i + 1; 15 | j = j - 1; 16 | } 17 | if (i < r) qsort(a, i, r); 18 | if (j > l) qsort(a, l, j); 19 | return 0; 20 | } 21 | 22 | int state; 23 | int rand() { 24 | state = (state * 64013 + 1531011) % 32768; 25 | return state % 1000; 26 | } 27 | 28 | int initArr(int n) { 29 | int i = 0; 30 | while (i < n) { 31 | arr[i] = rand(); 32 | i = i + 1; 33 | } 34 | return 0; 35 | } 36 | 37 | int isSorted(int n) { 38 | int i = 0; 39 | while (i < n-1) { 40 | if ( arr[i] > arr[i+1] ) 41 | return 0; 42 | i = i + 1; 43 | } 44 | return 1; 45 | } 46 | 47 | int main() { 48 | initArr(n); 49 | int sorted_before = isSorted(n); 50 | qsort(arr, 0, n-1); 51 | int sorted_after = isSorted(n); 52 | if (!(sorted_before==0 && sorted_after==1)) 53 | return 1; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /testcases/step11/matmul.c: -------------------------------------------------------------------------------- 1 | int a[2][2]; int b[2][2]; 2 | 3 | int mulMatrix(int n) { 4 | int i; int j; int k; 5 | i = 0; 6 | while (i < n) { 7 | j = 0; 8 | while (j < n) { 9 | b[i][j] = 0; 10 | k = 0; 11 | while (k < n) { 12 | b[i][j] = b[i][j] + a[i][k] * a[k][j]; 13 | k = k + 1; 14 | } 15 | j = j + 1; 16 | } 17 | i = i + 1; 18 | } 19 | return 0; 20 | } 21 | 22 | int initMatrix(int n) { 23 | int i; int j; int k; 24 | k = 0; 25 | i = 0; 26 | while (i < 2) { 27 | j = 0; 28 | while (j < 2) { 29 | k = k + 1; 30 | a[i][j] = k; 31 | j = j + 1; 32 | } 33 | i = i + 1; 34 | } 35 | return 0; 36 | } 37 | 38 | int main() { 39 | initMatrix(2); 40 | mulMatrix(2); 41 | for (int i = 0; i < 2; i = i + 1) 42 | for (int j = 0; j < 2; j = j + 1) { 43 | a[i][j] = b[i][j]; 44 | b[i][j] = 0; 45 | } 46 | mulMatrix(2); 47 | if (b[0][0] != 199) 48 | return 1; 49 | if (b[0][1] != 290) 50 | return 2; 51 | if (b[1][0] != 435) 52 | return 3; 53 | if (b[1][1] != 634) 54 | return 4; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /testcases/step6/nested_return_2.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 1; 4 | { 5 | a = a + 2; 6 | { 7 | int a = 3; 8 | { 9 | int a = 4; 10 | { 11 | int a = 5; 12 | { 13 | a = a + 6; 14 | { 15 | int a = 7; 16 | { 17 | a = a + 8; 18 | { 19 | int a = 9; 20 | { 21 | int a = 10; 22 | { 23 | int b = 1; 24 | } 25 | b = b + a; 26 | } 27 | } 28 | } 29 | } 30 | return a + b; 31 | b = b + a; 32 | } 33 | int b = 4; 34 | } 35 | int b = 77; 36 | } 37 | } 38 | int b = 9; 39 | } 40 | return a + b; 41 | } -------------------------------------------------------------------------------- /testcases/step6/nested_return.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int b = 1; 4 | { 5 | a = a + 2; 6 | { 7 | int a = 3; 8 | { 9 | int a = 4; 10 | { 11 | int a = 5; 12 | { 13 | a = a + 6; 14 | { 15 | int a = 7; 16 | { 17 | a = a + 8; 18 | { 19 | int a = 9; 20 | { 21 | int a = 10; 22 | { 23 | int b = 1; 24 | } 25 | b = b + a; 26 | } 27 | } 28 | } 29 | } 30 | b = b + a; 31 | } 32 | int b = 4; 33 | } 34 | int b = 77; 35 | { 36 | int c = b; 37 | { 38 | int b = 3; 39 | } 40 | return b + a + a; 41 | } 42 | } 43 | } 44 | int b = 9; 45 | } 46 | return a + b; 47 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # minidecaf-tests 2 | MiniDecaf 测例及测试脚本。 3 | 4 | 我们有两种测例 5 | - 普通测例(在 `testcases/` 下):你的 MiniDecaf 要能成功编译这些测例到合法的汇编,然后正常返回(返回码为 0)。 6 | 我们会将你的编译结果的运行返回值与 GCC 编译结果的运行返回值比较,要求它们必须要完全相同。 7 | - 报错测例(在 `failcases/` 下):你的 MiniDecaf 对于这些测例,要么不能正常返回(返回码非 0),要么不能生成合法汇编(例如输出中夹杂了报错信息)。 8 | 最好能够生成有用的报错信息,但不要求。 9 | 10 | ## 依赖 11 | - [RISCV工具链](https://decaf-lang.github.io/minidecaf-tutorial-deploy/docs/lab0/riscv.html) 12 | - (可选)[GNU parallel](https://www.gnu.org/software/parallel/),用于并行测试,对于多核机器可大幅提高测试速度(3-5 倍)。 13 | 14 | ## 用法 15 | 直接运行 `check.sh` 即可,例如在 `minidecaf-tests/` 目录下 16 | ``` 17 | $ ./check.sh 18 | ``` 19 | 20 | `check.sh` 支持如下参数,参数用法是 `OPT=VALUE ./check.sh`,多个 `OPT`-`VALUE` 对用空格隔开。 21 | 例如 `STEP_UNTIL=1 PROJ_PATH=../minidecaf ./check.sh`。 22 | 23 | | 参数名 | 类型 | 含义 | 默认值 | 24 | | --- | --- | --- | --- | 25 | | `STEP_FROM` | 1 到 12 的整数(不超过`STEP_UNTIL`) | 从哪个 step 开始测 | 1 | 26 | | `STEP_UNTIL` | 1 到 12 的整数 | 测到哪个 step | 12 | 27 | | `PROJ_PATH` | 一个路径 | 你的 minidecaf 仓库的路径 | `..` | 28 | 29 | ## 输出含义 30 | * `OK` 测试点通过 31 | * `ERR` 你的编译器崩了,或者输出的汇编格式不对 32 | * `FAIL` (testcase)编译出的汇编运行结果不对;(failcase)或者对于不合法输入没报错而是输出了合法汇编。 33 | 34 | ## 用于 debug 的中间文件 35 | 测例文件在 `testcases/step*/*.c` 和 `failcases/step*/*.c`,运行 `./check.sh` 后同一目录下会有如下中间文件方便 debug 36 | * `*.err`:`ERR` 的错误信息 37 | * `*.expected`:用 gcc 编译运行得到的标准答案 38 | * `*.actual`:运行你的编译结果得到的答案,需要和上面一样 39 | * `*.{gcc,my}`:可执行文件 40 | 41 | ## 常见问题 42 | 遇到问题,请先尝试重设仓库,删除临时文件,撤销所有你的改动: 43 | ``` 44 | $ git clean -fdx 45 | $ git checkout -- . 46 | ``` 47 | 48 | 如果出错,除了下面内容也请阅读实验指导书的[常见问题](https://decaf-lang.github.io/minidecaf-tutorial/docs/step0/faq.html)一节。 49 | 50 | * permission denied: ./check.sh 51 | - 需要给 `check.sh` 加执行权限 `chmod +x check.sh` 52 | * gcc not found 或 qemu not found 53 | - 请按照[实验指导书](https://decaf-lang.github.io/minidecaf-tutorial/docs/lab0/env.html)配好环境。 54 | * macOS 下找不到 realpath 命令 55 | ``` 56 | ./check.sh: line 25: realpath: command not found 57 | ./check.sh: line 26: realpath: command not found 58 | ./check.sh: line 30: $asmfile: ambiguous redirect 59 | ``` 60 | - 使用 Homebrew 安装 coreutils:`brew install coreutils` 61 | 62 | ## 参考 63 | * [Nora Sandler's compiler testsuits](https://github.com/nlsandler/write_a_c_compiler) 64 | -------------------------------------------------------------------------------- /check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export CC="riscv64-unknown-elf-gcc -std=c17 -march=rv32im -mabi=ilp32" 3 | export QEMU=qemu-riscv32 4 | export SPIKE="spike --isa=RV32G /usr/local/bin/pk" 5 | if [ $(uname) = "Linux" ]; then 6 | export EMU=$QEMU 7 | else 8 | export EMU=$SPIKE 9 | fi 10 | 11 | : ${USE_PARALLEL:=true} 12 | : ${PROJ_PATH:=..} 13 | export PROJ_PATH 14 | 15 | if [[ $CI_COMMIT_REF_NAME == "stage-1" ]]; then 16 | : ${STEP_FROM:=1} 17 | : ${STEP_UNTIL:=4} 18 | elif [[ $CI_COMMIT_REF_NAME == "stage-2" ]]; then 19 | : ${STEP_FROM:=5} 20 | : ${STEP_UNTIL:=5} 21 | elif [[ $CI_COMMIT_REF_NAME == "stage-3" ]]; then 22 | : ${STEP_FROM:=6} 23 | : ${STEP_UNTIL:=6} 24 | elif [[ $CI_COMMIT_REF_NAME == "stage-4" ]]; then 25 | : ${STEP_FROM:=7} 26 | : ${STEP_UNTIL:=8} 27 | elif [[ $CI_COMMIT_REF_NAME == "stage-5" ]]; then 28 | : ${STEP_FROM:=9} 29 | : ${STEP_UNTIL:=9} 30 | elif [[ $CI_COMMIT_REF_NAME == "stage-6" ]]; then 31 | : ${STEP_FROM:=10} 32 | : ${STEP_UNTIL:=12} 33 | elif [ -v $CI_COMMIT_REF_NAME ]; then 34 | echo "The test is not in CI." 35 | echo "All testcases are taken into account, unless you manually set STEP_FROM and STEP_UNTIL." 36 | : ${STEP_FROM:=1} 37 | : ${STEP_UNTIL:=12} 38 | else 39 | echo "Warning: unknown branch" 40 | echo "All testcases are taken into account, unless you manually set STEP_FROM and STEP_UNTIL." 41 | : ${STEP_FROM:=1} 42 | : ${STEP_UNTIL:=12} 43 | fi 44 | 45 | if [[ $STEP_UNTIL -lt $STEP_FROM ]]; then 46 | echo "STEP_UNTIL < STEP_FROM: no tests run" 47 | exit 0 48 | else 49 | JOBS=() 50 | for step in `seq ${STEP_FROM} ${STEP_UNTIL}`; do 51 | if [ -d "testcases/step${step}" ]; then 52 | JOBS+=($(eval echo testcases/step${step}/*.c)) 53 | fi 54 | done 55 | 56 | FAILJOBS=() 57 | for step in `seq ${STEP_FROM} ${STEP_UNTIL}`; do 58 | if [ -d "failcases/step${step}" ]; then 59 | FAILJOBS+=($(eval echo failcases/step${step}/*.c)) 60 | fi 61 | done 62 | 63 | job_cnt=$((${#JOBS[@]} + ${#FAILJOBS[@]})) 64 | fi 65 | 66 | gen_asm() { 67 | cfile=$(realpath "$1") 68 | asmfile=$(realpath "$2") 69 | 70 | # 根据特征文件判断所使用的语言 71 | rm -f _unrecog_impl 72 | if [[ -f $PROJ_PATH/requirements.txt ]]; then # Python: minidecaf/requirements.txt 73 | python $PROJ_PATH/main.py --input "$cfile" --riscv >"$asmfile" 74 | else 75 | touch _unrecog_impl 76 | fi 77 | } 78 | export -f gen_asm 79 | 80 | RED='\033[0;31m' 81 | GREEN='\033[0;32m' 82 | YELLOW='\033[1;33m' 83 | NC='\033[0m' 84 | 85 | run_job() { 86 | infile=$1 87 | outbase=${infile%.c} 88 | 89 | rm $outbase.{gcc,expected,err,my,actual,s} 1>/dev/null 2>&1 90 | 91 | $CC $infile -o $outbase.gcc 92 | $EMU $outbase.gcc >/dev/null 93 | echo $? > $outbase.expected 94 | 95 | if ! ( 96 | gen_asm $infile $outbase.s && 97 | $CC $outbase.s -o $outbase.my ) >$outbase.err 2>&1 98 | then 99 | echo -e "\n${YELLOW}ERR${NC} ${infile}" 100 | echo "==== Error information =======================================================" 101 | cat $outbase.err 102 | echo -e "==============================================================================\n" 103 | return 2 104 | fi 105 | $EMU $outbase.my >/dev/null 106 | echo $? > $outbase.actual 107 | 108 | if ! diff -q $outbase.expected $outbase.actual >/dev/null ; then 109 | echo -e "\n${RED}FAIL${NC} ${infile}" 110 | echo "==== Fail information (above: expected, below: actual) =======================" 111 | diff $outbase.expected $outbase.actual 112 | echo -e "==============================================================================\n" 113 | return 1 114 | else 115 | echo -e "${GREEN}OK${NC} ${infile}" 116 | return 0 117 | fi 118 | } 119 | export -f run_job 120 | 121 | 122 | run_failjob() { 123 | infile=$1 124 | outbase=${infile%.c} 125 | 126 | rm $outbase.{my,s} 1>/dev/null 2>&1 127 | 128 | if (gen_asm $infile $outbase.s && 129 | $CC $outbase.s -o $outbase.my) >/dev/null 2>&1 130 | then 131 | echo -e "\n${RED}FAIL${NC} ${infile}" 132 | echo "==== Fail information (failed to detect input error) =========================" 133 | cat $outbase.s 134 | echo -e "==============================================================================\n" 135 | return 1 136 | else 137 | echo -e "${GREEN}OK${NC} ${infile}" 138 | return 0 139 | fi 140 | } 141 | export -f run_failjob 142 | 143 | 144 | check_env_and_parallel() { 145 | if ! $CC --version >/dev/null 2>&1; then 146 | echo "gcc not found" 147 | exit 1 148 | fi 149 | echo "gcc found" 150 | 151 | if ! $EMU -h >/dev/null 2>&1; then 152 | echo "${EMU%% *} not found" 153 | exit 1 154 | fi 155 | echo "${EMU%% *} found" 156 | 157 | if $USE_PARALLEL && parallel --version >/dev/null 2>&1; then 158 | echo "running tests in parallel" 159 | return 0 160 | else 161 | echo "running tests serially" 162 | return 1 163 | fi 164 | } 165 | 166 | 167 | main() { 168 | echo "$job_cnt cases in total" 169 | 170 | err_cnt=0 171 | if check_env_and_parallel; then 172 | # save logs in file and read logs later 173 | parallel --joblog /tmp/test.log run_job ::: ${JOBS[@]} 174 | parallel --joblog /tmp/fail.log run_failjob ::: ${FAILJOBS[@]} 175 | 176 | err_cnt=$(($err_cnt + $(cat /tmp/test.log | awk '{if ($7 != 0) {x += 1}} END { print x - 1 }'))) 177 | err_cnt=$(($err_cnt + $(cat /tmp/fail.log | awk '{if ($7 != 0) {x += 1}} END { print x - 1 }'))) 178 | else 179 | for job in ${JOBS[@]}; do 180 | run_job $job 181 | if [[ $? -ne 0 ]]; then 182 | err_cnt=$(($err_cnt + 1)) 183 | fi 184 | done 185 | for job in ${FAILJOBS[@]}; do 186 | run_failjob $job 187 | if [[ $? -ne 0 ]]; then 188 | err_cnt=$(($err_cnt + 1)) 189 | fi 190 | done 191 | fi 192 | echo -e "Pass $(($job_cnt - $err_cnt)) / $job_cnt cases in total.\n" 193 | return $err_cnt 194 | } 195 | 196 | # check if the report exists 197 | check_report() { 198 | if [ ! -v $CI_COMMIT_REF_NAME ] && 199 | [[ $CI_COMMIT_REF_NAME =~ ^(stage-1|stage-2|stage-3|stage-4|stage-5|parser-stage)$ ]]; then 200 | if [[ ! -f $PROJ_PATH/reports/$CI_COMMIT_REF_NAME.pdf ]]; then 201 | echo -e "${RED}ERROR: There's no reports/$CI_COMMIT_REF_NAME.pdf${NC}" 202 | return 1 203 | else 204 | echo -e "${GREEN}SUCCESS: The report is submitted.${NC}" 205 | return 0 206 | fi 207 | else 208 | return 0 209 | fi 210 | } 211 | 212 | cd "$(dirname "$0")" 213 | 214 | if ! [[ -d $PROJ_PATH ]]; then 215 | echo "Put your code in $PROJ_PATH" 216 | exit 1 217 | fi 218 | 219 | main; ec1=$? 220 | check_report; ec2=$? 221 | if [[ $(($ec1 + $ec2)) -ne 0 ]]; then 222 | if [[ -f _unrecog_impl ]]; then 223 | echo "Unrecognized implementation. Are you using one of the supported language & frameworks? Or did you put check.sh in the wrong place?" 224 | rm _unrecog_impl; 225 | fi 226 | 227 | echo FAILED 228 | exit 1; 229 | fi 230 | --------------------------------------------------------------------------------