From 43174676cabea104c04a1874cec2e574748134b6 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Tue, 27 Jul 2010 16:20:09 +0000 Subject: [PATCH] Now handling return values from main. Changed to more strict handling of main() - if it's not defined in main-calling mode then the program will crash rather than ignoring the problem. Tests changed to correspond to this approach. git-svn-id: http://picoc.googlecode.com/svn/trunk@479 21eae674-98b7-11dd-bd71-f92a316d2d60 --- picoc.c | 33 ++++++++++++++++++++++++--------- tests/00_assignment.c | 1 + tests/01_comment.c | 3 +++ tests/02_printf.c | 2 ++ tests/03_struct.c | 2 ++ tests/04_for.c | 2 ++ tests/05_array.c | 2 ++ tests/06_case.c | 2 ++ tests/07_function.c | 2 ++ tests/08_while.c | 2 ++ tests/09_do_while.c | 2 ++ tests/10_pointer.c | 2 ++ tests/11_precedence.c | 2 ++ tests/12_hashdefine.c | 2 ++ tests/13_integer_literals.c | 2 ++ tests/14_if.c | 2 ++ tests/15_recursion.c | 2 ++ tests/16_nesting.c | 2 ++ tests/17_enum.c | 2 ++ tests/18_include.c | 2 ++ tests/19_pointer_arithmetic.c | 2 ++ tests/20_pointer_comparison.c | 2 ++ tests/21_char_array.c | 2 ++ tests/22_floating_point.c | 2 ++ tests/23_type_coercion.c | 2 ++ tests/24_math_library.c | 2 ++ tests/26_character_constants.c | 2 ++ tests/27_sizeof.c | 2 ++ tests/28_strings.c | 2 ++ tests/29_array_address.c | 2 ++ tests/33_ternary_op.c | 2 ++ tests/34_array_assignment.c | 2 ++ tests/35_sizeof.c | 2 ++ tests/36_array_initialisers.c | 2 ++ tests/37_sprintf.c | 2 ++ tests/38_multiple_array_index.c | 2 ++ tests/39_typedef.c | 2 ++ tests/40_stdio.c | 2 ++ tests/41_hashif.c | 2 ++ tests/43_void_param.c | 2 ++ tests/44_scoped_declarations.c | 2 ++ 41 files changed, 104 insertions(+), 9 deletions(-) diff --git a/picoc.c b/picoc.c index d32674f..b67f112 100644 --- a/picoc.c +++ b/picoc.c @@ -1,7 +1,9 @@ #include "picoc.h" -#define CALL_MAIN_NO_ARGS "main();" -#define CALL_MAIN_WITH_ARGS "main(__argc,__argv);" +#define CALL_MAIN_NO_ARGS_RETURN_VOID "main();" +#define CALL_MAIN_WITH_ARGS_RETURN_VOID "main(__argc,__argv);" +#define CALL_MAIN_NO_ARGS_RETURN_INT "__exit_value = main();" +#define CALL_MAIN_WITH_ARGS_RETURN_INT "__exit_value = main(__argc,__argv);" /* initialise everything */ void Initialise(int StackSize) @@ -41,21 +43,34 @@ void CallMain(int argc, char **argv) struct Value *FuncValue = NULL; if (!VariableDefined(TableStrRegister("main"))) - return; + ProgramFail(NULL, "main() is not defined"); VariableGet(NULL, TableStrRegister("main"), &FuncValue); if (FuncValue->Typ->Base != TypeFunction) ProgramFail(NULL, "main is not a function - can't call it"); - if (FuncValue->Val->FuncDef.NumParams == 0) - Parse("", CALL_MAIN_NO_ARGS, strlen(CALL_MAIN_NO_ARGS), TRUE); - else + if (FuncValue->Val->FuncDef.NumParams != 0) { /* define the arguments */ VariableDefinePlatformVar(NULL, "__argc", &IntType, (union AnyValue *)&argc, FALSE); VariableDefinePlatformVar(NULL, "__argv", CharPtrPtrType, (union AnyValue *)&argv, FALSE); + } - Parse("", CALL_MAIN_WITH_ARGS, strlen(CALL_MAIN_WITH_ARGS), TRUE); + if (FuncValue->Val->FuncDef.ReturnType == &VoidType) + { + if (FuncValue->Val->FuncDef.NumParams == 0) + Parse("", CALL_MAIN_NO_ARGS_RETURN_VOID, strlen(CALL_MAIN_NO_ARGS_RETURN_VOID), TRUE); + else + Parse("", CALL_MAIN_WITH_ARGS_RETURN_VOID, strlen(CALL_MAIN_WITH_ARGS_RETURN_VOID), TRUE); + } + else + { + VariableDefinePlatformVar(NULL, "__exit_value", &IntType, (union AnyValue *)&ExitValue, TRUE); + + if (FuncValue->Val->FuncDef.NumParams == 0) + Parse("", CALL_MAIN_NO_ARGS_RETURN_INT, strlen(CALL_MAIN_NO_ARGS_RETURN_INT), TRUE); + else + Parse("", CALL_MAIN_WITH_ARGS_RETURN_INT, strlen(CALL_MAIN_WITH_ARGS_RETURN_INT), TRUE); } } @@ -103,7 +118,7 @@ int main(int argc, char **argv) } Cleanup(); - return 0; + return ExitValue; } #else # ifdef SURVEYOR_HOST @@ -131,7 +146,7 @@ int picoc(char *SourceStr) Parse("test.c", SourceStr, strlen(SourceStr), TRUE); ParseInteractive(); Cleanup(); - return 0; + return ExitValue; } # endif #endif diff --git a/tests/00_assignment.c b/tests/00_assignment.c index 62adcec..56738ba 100644 --- a/tests/00_assignment.c +++ b/tests/00_assignment.c @@ -10,3 +10,4 @@ printf("%d\n", b); int c = 12, d = 34; printf("%d, %d\n", c, d); +void main() {} diff --git a/tests/01_comment.c b/tests/01_comment.c index d2b3ad9..ce8d687 100644 --- a/tests/01_comment.c +++ b/tests/01_comment.c @@ -5,3 +5,6 @@ printf("Hello\n"); /* this is a comment */ printf("Hello\n"); printf("Hello\n"); // this is also a comment sayhello(); printf("Hello\n"); + + +void main() {} diff --git a/tests/02_printf.c b/tests/02_printf.c index 66eed98..f779c1e 100644 --- a/tests/02_printf.c +++ b/tests/02_printf.c @@ -9,3 +9,5 @@ for (Count = -5; Count <= 5; Count++) printf("String 'hello', 'there' is '%s', '%s'\n", "hello", "there"); printf("Character 'A' is '%c'\n", 65); printf("Character 'a' is '%c'\n", 'a'); + +void main() {} diff --git a/tests/03_struct.c b/tests/03_struct.c index db62cc2..76fe7a8 100644 --- a/tests/03_struct.c +++ b/tests/03_struct.c @@ -13,3 +13,5 @@ bloggs.natasha = 34; printf("%d\n", bloggs.boris); printf("%d\n", bloggs.natasha); + +void main() {} diff --git a/tests/04_for.c b/tests/04_for.c index 046e473..de563b5 100644 --- a/tests/04_for.c +++ b/tests/04_for.c @@ -6,3 +6,5 @@ for (Count = 1; Count <= 10; Count++) { printf("%d\n", Count); } + +void main() {} diff --git a/tests/05_array.c b/tests/05_array.c index a70ac0a..03dac05 100644 --- a/tests/05_array.c +++ b/tests/05_array.c @@ -12,3 +12,5 @@ for (Count = 0; Count < 10; Count++) { printf("%d\n", Array[Count]); } + +void main() {} diff --git a/tests/06_case.c b/tests/06_case.c index d7f883d..1858205 100644 --- a/tests/06_case.c +++ b/tests/06_case.c @@ -20,3 +20,5 @@ for (Count = 0; Count < 4; Count++) break; } } + +void main() {} diff --git a/tests/07_function.c b/tests/07_function.c index e7c4fa7..0501380 100644 --- a/tests/07_function.c +++ b/tests/07_function.c @@ -21,3 +21,5 @@ void qfunc() } qfunc(); + +void main() {} diff --git a/tests/08_while.c b/tests/08_while.c index bb990b3..aee79ba 100644 --- a/tests/08_while.c +++ b/tests/08_while.c @@ -15,3 +15,5 @@ while (a < 100) a = t + p; p = t; } + +void main() {} diff --git a/tests/09_do_while.c b/tests/09_do_while.c index adc4cae..e55b83a 100644 --- a/tests/09_do_while.c +++ b/tests/09_do_while.c @@ -16,3 +16,5 @@ do p = t; } while (a < 100); + +void main() {} diff --git a/tests/10_pointer.c b/tests/10_pointer.c index 0e99787..1bd40e3 100644 --- a/tests/10_pointer.c +++ b/tests/10_pointer.c @@ -33,3 +33,5 @@ printf("tsar->c = %d\n", tsar->c); b = &(bolshevic.b); printf("bolshevic.b = %d\n", *b); */ + +void main() {} diff --git a/tests/11_precedence.c b/tests/11_precedence.c index 7360312..d8f027d 100644 --- a/tests/11_precedence.c +++ b/tests/11_precedence.c @@ -33,3 +33,5 @@ printf("%d\n", a != b && c != d); printf("%d\n", a + b * c / f); printf("%d\n", a + b * c / f); + +void main() {} diff --git a/tests/12_hashdefine.c b/tests/12_hashdefine.c index 50e032a..6ffaf3f 100644 --- a/tests/12_hashdefine.c +++ b/tests/12_hashdefine.c @@ -6,3 +6,5 @@ printf("%d\n", FRED); printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3)); + +void main() {} diff --git a/tests/13_integer_literals.c b/tests/13_integer_literals.c index c0e99a1..f021de2 100644 --- a/tests/13_integer_literals.c +++ b/tests/13_integer_literals.c @@ -12,3 +12,5 @@ printf("%d\n", c); printf("%d\n", d); printf("%d\n", e); + +void main() {} diff --git a/tests/14_if.c b/tests/14_if.c index 58dbed1..bb56ef4 100644 --- a/tests/14_if.c +++ b/tests/14_if.c @@ -13,3 +13,5 @@ if (b) else printf("b is false\n"); + +void main() {} diff --git a/tests/15_recursion.c b/tests/15_recursion.c index 766aa87..4733c8f 100644 --- a/tests/15_recursion.c +++ b/tests/15_recursion.c @@ -12,3 +12,5 @@ int Count; for (Count = 1; Count <= 10; Count++) printf("%d\n", factorial(Count)); + +void main() {} diff --git a/tests/16_nesting.c b/tests/16_nesting.c index 520777e..90a62bf 100644 --- a/tests/16_nesting.c +++ b/tests/16_nesting.c @@ -12,3 +12,5 @@ for (x = 0; x < 2; x++) } } } + +void main() {} diff --git a/tests/17_enum.c b/tests/17_enum.c index 7d8cb61..f53c88c 100644 --- a/tests/17_enum.c +++ b/tests/17_enum.c @@ -20,3 +20,5 @@ frod = 12; printf("%d\n", frod); frod = e; printf("%d\n", frod); + +void main() {} diff --git a/tests/18_include.c b/tests/18_include.c index 0f14569..f2bfcc6 100644 --- a/tests/18_include.c +++ b/tests/18_include.c @@ -3,3 +3,5 @@ printf("including\n"); #include "18_include.h" printf("done\n"); + +void main() {} diff --git a/tests/19_pointer_arithmetic.c b/tests/19_pointer_arithmetic.c index 149edc4..96c7e6a 100644 --- a/tests/19_pointer_arithmetic.c +++ b/tests/19_pointer_arithmetic.c @@ -20,3 +20,5 @@ if (c == NULL) else printf("c is not NULL\n"); + +void main() {} diff --git a/tests/20_pointer_comparison.c b/tests/20_pointer_comparison.c index 6977118..bab1d5e 100644 --- a/tests/20_pointer_comparison.c +++ b/tests/20_pointer_comparison.c @@ -16,3 +16,5 @@ d = e; printf("%d\n", d == e); printf("%d\n", d != e); + +void main() {} diff --git a/tests/21_char_array.c b/tests/21_char_array.c index c21e5ca..0f158cd 100644 --- a/tests/21_char_array.c +++ b/tests/21_char_array.c @@ -25,3 +25,5 @@ while (*src != 0) printf("copied string is %s\n", destarray); + +void main() {} diff --git a/tests/22_floating_point.c b/tests/22_floating_point.c index 8c6b394..37754a4 100644 --- a/tests/22_floating_point.c +++ b/tests/22_floating_point.c @@ -41,3 +41,5 @@ printf("%f\n", -12.34); a = 2; printf("%f\n", a); printf("%f\n", sin(2)); + +void main() {} diff --git a/tests/23_type_coercion.c b/tests/23_type_coercion.c index 7070cc2..9076952 100644 --- a/tests/23_type_coercion.c +++ b/tests/23_type_coercion.c @@ -45,3 +45,5 @@ float f = 'a'; float g = 97; printf("%f %f\n", f, g); + +void main() {} diff --git a/tests/24_math_library.c b/tests/24_math_library.c index 88fd817..892c4b8 100644 --- a/tests/24_math_library.c +++ b/tests/24_math_library.c @@ -19,3 +19,5 @@ printf("%f\n", sqrt(0.12)); printf("%f\n", round(12.34)); printf("%f\n", ceil(12.34)); printf("%f\n", floor(12.34)); + +void main() {} diff --git a/tests/26_character_constants.c b/tests/26_character_constants.c index 5674b88..6553f76 100644 --- a/tests/26_character_constants.c +++ b/tests/26_character_constants.c @@ -9,3 +9,5 @@ printf("%d\n", '\x10'); printf("%d\n", '\x40'); printf("test \x407\n"); + +void main() {} diff --git a/tests/27_sizeof.c b/tests/27_sizeof.c index e6a20fd..52703aa 100644 --- a/tests/27_sizeof.c +++ b/tests/27_sizeof.c @@ -8,3 +8,5 @@ printf("%d\n", sizeof(a)); printf("%d\n", sizeof(b)); printf("%d\n", sizeof(c)); + +void main() {} diff --git a/tests/28_strings.c b/tests/28_strings.c index 862800e..d173060 100644 --- a/tests/28_strings.c +++ b/tests/28_strings.c @@ -37,3 +37,5 @@ printf("%d\n", memcmp(a, "apple", 4) > 0); printf("%d\n", memcmp(a, "grgr", 4) == 0); printf("%d\n", memcmp(a, "zebra", 4) < 0); + +void main() {} diff --git a/tests/29_array_address.c b/tests/29_array_address.c index d70e116..a8562af 100644 --- a/tests/29_array_address.c +++ b/tests/29_array_address.c @@ -4,3 +4,5 @@ char a[10]; strcpy(a, "abcdef"); printf("%s\n", &a[1]); + +void main() {} diff --git a/tests/33_ternary_op.c b/tests/33_ternary_op.c index d36ac03..5c1cead 100644 --- a/tests/33_ternary_op.c +++ b/tests/33_ternary_op.c @@ -6,3 +6,5 @@ for (Count = 0; Count < 10; Count++) { printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3)); } + +void main() {} diff --git a/tests/34_array_assignment.c b/tests/34_array_assignment.c index 4e096cd..afe544e 100644 --- a/tests/34_array_assignment.c +++ b/tests/34_array_assignment.c @@ -14,3 +14,5 @@ int b[4]; b = a; printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]); + +void main() {} diff --git a/tests/35_sizeof.c b/tests/35_sizeof.c index 7b27af0..87c783a 100644 --- a/tests/35_sizeof.c +++ b/tests/35_sizeof.c @@ -5,3 +5,5 @@ short b; printf("%d %d\n", sizeof(char), sizeof(a)); printf("%d %d\n", sizeof(short), sizeof(b)); + +void main() {} diff --git a/tests/36_array_initialisers.c b/tests/36_array_initialisers.c index 7ff386a..ce8695b 100644 --- a/tests/36_array_initialisers.c +++ b/tests/36_array_initialisers.c @@ -12,3 +12,5 @@ int Array2[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753, }; for (Count = 0; Count < 10; Count++) printf("%d: %d\n", Count, Array2[Count]); + +void main() {} diff --git a/tests/37_sprintf.c b/tests/37_sprintf.c index 4d8aa67..839a4d8 100644 --- a/tests/37_sprintf.c +++ b/tests/37_sprintf.c @@ -8,3 +8,5 @@ for (Count = 1; Count <= 20; Count++) sprintf(Buf, "->%02d<-\n", Count); printf("%s", Buf); } + +void main() {} diff --git a/tests/38_multiple_array_index.c b/tests/38_multiple_array_index.c index dbb32e9..383d3ee 100644 --- a/tests/38_multiple_array_index.c +++ b/tests/38_multiple_array_index.c @@ -25,3 +25,5 @@ for (x = 0; x < 4; x++) } printf("\n"); } + +void main() {} diff --git a/tests/39_typedef.c b/tests/39_typedef.c index 60d37ec..e36791c 100644 --- a/tests/39_typedef.c +++ b/tests/39_typedef.c @@ -22,3 +22,5 @@ typedef MyFunStruct *MoreFunThanEver; MoreFunThanEver c = &b; printf("%d,%d\n", c->i, c->j); + +void main() {} diff --git a/tests/40_stdio.c b/tests/40_stdio.c index f1d76b9..546ca52 100644 --- a/tests/40_stdio.c +++ b/tests/40_stdio.c @@ -43,3 +43,5 @@ while (fgets(freddy, sizeof(freddy), f) != NULL) printf("x: %s", freddy); fclose(f); + +void main() {} diff --git a/tests/41_hashif.c b/tests/41_hashif.c index 673e47e..b1883c0 100644 --- a/tests/41_hashif.c +++ b/tests/41_hashif.c @@ -76,3 +76,5 @@ printf("#include test\n"); printf("t\n"); #endif #endif + +void main() {} diff --git a/tests/43_void_param.c b/tests/43_void_param.c index d505f02..4803d94 100644 --- a/tests/43_void_param.c +++ b/tests/43_void_param.c @@ -6,3 +6,5 @@ void fred(void) } fred(); + +void main() {} diff --git a/tests/44_scoped_declarations.c b/tests/44_scoped_declarations.c index 5cbed65..50337ef 100644 --- a/tests/44_scoped_declarations.c +++ b/tests/44_scoped_declarations.c @@ -8,3 +8,5 @@ for (a = 0; a < 2; a++) } printf("it's all good\n"); + +void main() {}