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
This commit is contained in:
zik.saleeba 2010-07-27 16:20:09 +00:00
parent df9129b355
commit 43174676ca
41 changed files with 104 additions and 9 deletions

33
picoc.c
View file

@ -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

View file

@ -10,3 +10,4 @@ printf("%d\n", b);
int c = 12, d = 34;
printf("%d, %d\n", c, d);
void main() {}

View file

@ -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() {}

View file

@ -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() {}

View file

@ -13,3 +13,5 @@ bloggs.natasha = 34;
printf("%d\n", bloggs.boris);
printf("%d\n", bloggs.natasha);
void main() {}

View file

@ -6,3 +6,5 @@ for (Count = 1; Count <= 10; Count++)
{
printf("%d\n", Count);
}
void main() {}

View file

@ -12,3 +12,5 @@ for (Count = 0; Count < 10; Count++)
{
printf("%d\n", Array[Count]);
}
void main() {}

View file

@ -20,3 +20,5 @@ for (Count = 0; Count < 4; Count++)
break;
}
}
void main() {}

View file

@ -21,3 +21,5 @@ void qfunc()
}
qfunc();
void main() {}

View file

@ -15,3 +15,5 @@ while (a < 100)
a = t + p;
p = t;
}
void main() {}

View file

@ -16,3 +16,5 @@ do
p = t;
} while (a < 100);
void main() {}

View file

@ -33,3 +33,5 @@ printf("tsar->c = %d\n", tsar->c);
b = &(bolshevic.b);
printf("bolshevic.b = %d\n", *b);
*/
void main() {}

View file

@ -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() {}

View file

@ -6,3 +6,5 @@
printf("%d\n", FRED);
printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3));
void main() {}

View file

@ -12,3 +12,5 @@ printf("%d\n", c);
printf("%d\n", d);
printf("%d\n", e);
void main() {}

View file

@ -13,3 +13,5 @@ if (b)
else
printf("b is false\n");
void main() {}

View file

@ -12,3 +12,5 @@ int Count;
for (Count = 1; Count <= 10; Count++)
printf("%d\n", factorial(Count));
void main() {}

View file

@ -12,3 +12,5 @@ for (x = 0; x < 2; x++)
}
}
}
void main() {}

View file

@ -20,3 +20,5 @@ frod = 12;
printf("%d\n", frod);
frod = e;
printf("%d\n", frod);
void main() {}

View file

@ -3,3 +3,5 @@
printf("including\n");
#include "18_include.h"
printf("done\n");
void main() {}

View file

@ -20,3 +20,5 @@ if (c == NULL)
else
printf("c is not NULL\n");
void main() {}

View file

@ -16,3 +16,5 @@ d = e;
printf("%d\n", d == e);
printf("%d\n", d != e);
void main() {}

View file

@ -25,3 +25,5 @@ while (*src != 0)
printf("copied string is %s\n", destarray);
void main() {}

View file

@ -41,3 +41,5 @@ printf("%f\n", -12.34);
a = 2;
printf("%f\n", a);
printf("%f\n", sin(2));
void main() {}

View file

@ -45,3 +45,5 @@ float f = 'a';
float g = 97;
printf("%f %f\n", f, g);
void main() {}

View file

@ -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() {}

View file

@ -9,3 +9,5 @@ printf("%d\n", '\x10');
printf("%d\n", '\x40');
printf("test \x407\n");
void main() {}

View file

@ -8,3 +8,5 @@ printf("%d\n", sizeof(a));
printf("%d\n", sizeof(b));
printf("%d\n", sizeof(c));
void main() {}

View file

@ -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() {}

View file

@ -4,3 +4,5 @@
char a[10];
strcpy(a, "abcdef");
printf("%s\n", &a[1]);
void main() {}

View file

@ -6,3 +6,5 @@ for (Count = 0; Count < 10; Count++)
{
printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3));
}
void main() {}

View file

@ -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() {}

View file

@ -5,3 +5,5 @@ short b;
printf("%d %d\n", sizeof(char), sizeof(a));
printf("%d %d\n", sizeof(short), sizeof(b));
void main() {}

View file

@ -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() {}

View file

@ -8,3 +8,5 @@ for (Count = 1; Count <= 20; Count++)
sprintf(Buf, "->%02d<-\n", Count);
printf("%s", Buf);
}
void main() {}

View file

@ -25,3 +25,5 @@ for (x = 0; x < 4; x++)
}
printf("\n");
}
void main() {}

View file

@ -22,3 +22,5 @@ typedef MyFunStruct *MoreFunThanEver;
MoreFunThanEver c = &b;
printf("%d,%d\n", c->i, c->j);
void main() {}

View file

@ -43,3 +43,5 @@ while (fgets(freddy, sizeof(freddy), f) != NULL)
printf("x: %s", freddy);
fclose(f);
void main() {}

View file

@ -76,3 +76,5 @@ printf("#include test\n");
printf("t\n");
#endif
#endif
void main() {}

View file

@ -6,3 +6,5 @@ void fred(void)
}
fred();
void main() {}

View file

@ -8,3 +8,5 @@ for (a = 0; a < 2; a++)
}
printf("it's all good\n");
void main() {}