2010-07-19 16:16:32 -04:00
|
|
|
#include "picoc.h"
|
|
|
|
|
2010-07-27 12:20:09 -04:00
|
|
|
#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);"
|
2010-07-19 16:16:32 -04:00
|
|
|
|
|
|
|
/* initialise everything */
|
2010-07-27 10:03:06 -04:00
|
|
|
void Initialise(int StackSize)
|
2010-07-19 16:16:32 -04:00
|
|
|
{
|
|
|
|
BasicIOInit();
|
2010-07-27 10:03:06 -04:00
|
|
|
HeapInit(StackSize);
|
2010-07-19 16:16:32 -04:00
|
|
|
TableInit();
|
|
|
|
VariableInit();
|
|
|
|
LexInit();
|
|
|
|
TypeInit();
|
|
|
|
IncludeInit();
|
|
|
|
#ifdef BUILTIN_MINI_STDLIB
|
|
|
|
LibraryInit(&GlobalTable, "c library", &CLibrary[0]);
|
|
|
|
CLibraryInit();
|
|
|
|
#endif
|
|
|
|
PlatformLibraryInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free memory */
|
|
|
|
void Cleanup()
|
|
|
|
{
|
|
|
|
PlatformCleanup();
|
|
|
|
IncludeCleanup();
|
|
|
|
ParseCleanup();
|
|
|
|
LexCleanup();
|
|
|
|
VariableCleanup();
|
|
|
|
TypeCleanup();
|
|
|
|
TableStrFree();
|
2010-07-27 10:03:06 -04:00
|
|
|
HeapCleanup();
|
2010-07-19 16:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* platform-dependent code for running programs is in this file */
|
|
|
|
#ifdef UNIX_HOST
|
|
|
|
void CallMain(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* check if the program wants arguments */
|
|
|
|
struct Value *FuncValue = NULL;
|
|
|
|
|
|
|
|
if (!VariableDefined(TableStrRegister("main")))
|
2010-07-27 12:20:09 -04:00
|
|
|
ProgramFail(NULL, "main() is not defined");
|
2010-07-19 16:16:32 -04:00
|
|
|
|
|
|
|
VariableGet(NULL, TableStrRegister("main"), &FuncValue);
|
|
|
|
if (FuncValue->Typ->Base != TypeFunction)
|
|
|
|
ProgramFail(NULL, "main is not a function - can't call it");
|
|
|
|
|
2010-07-27 12:20:09 -04:00
|
|
|
if (FuncValue->Val->FuncDef.NumParams != 0)
|
2010-07-19 16:16:32 -04:00
|
|
|
{
|
|
|
|
/* define the arguments */
|
|
|
|
VariableDefinePlatformVar(NULL, "__argc", &IntType, (union AnyValue *)&argc, FALSE);
|
|
|
|
VariableDefinePlatformVar(NULL, "__argv", CharPtrPtrType, (union AnyValue *)&argv, FALSE);
|
2010-07-27 12:20:09 -04:00
|
|
|
}
|
2010-07-19 16:16:32 -04:00
|
|
|
|
2010-07-27 12:20:09 -04:00
|
|
|
if (FuncValue->Val->FuncDef.ReturnType == &VoidType)
|
|
|
|
{
|
|
|
|
if (FuncValue->Val->FuncDef.NumParams == 0)
|
2010-07-27 14:16:05 -04:00
|
|
|
Parse("startup", CALL_MAIN_NO_ARGS_RETURN_VOID, strlen(CALL_MAIN_NO_ARGS_RETURN_VOID), TRUE, TRUE, FALSE);
|
2010-07-27 12:20:09 -04:00
|
|
|
else
|
2010-07-27 14:16:05 -04:00
|
|
|
Parse("startup", CALL_MAIN_WITH_ARGS_RETURN_VOID, strlen(CALL_MAIN_WITH_ARGS_RETURN_VOID), TRUE, TRUE, FALSE);
|
2010-07-27 12:20:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VariableDefinePlatformVar(NULL, "__exit_value", &IntType, (union AnyValue *)&ExitValue, TRUE);
|
|
|
|
|
|
|
|
if (FuncValue->Val->FuncDef.NumParams == 0)
|
2010-07-27 14:16:05 -04:00
|
|
|
Parse("startup", CALL_MAIN_NO_ARGS_RETURN_INT, strlen(CALL_MAIN_NO_ARGS_RETURN_INT), TRUE, TRUE, FALSE);
|
2010-07-27 12:20:09 -04:00
|
|
|
else
|
2010-07-27 14:16:05 -04:00
|
|
|
Parse("startup", CALL_MAIN_WITH_ARGS_RETURN_INT, strlen(CALL_MAIN_WITH_ARGS_RETURN_INT), TRUE, TRUE, FALSE);
|
2010-07-19 16:16:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ParamCount = 1;
|
|
|
|
int DontRunMain = FALSE;
|
2010-07-27 10:03:06 -04:00
|
|
|
int StackSize = getenv("STACKSIZE") ? atoi(getenv("STACKSIZE")) : STACK_SIZE;
|
2010-07-19 16:16:32 -04:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
printf("Format: picoc <csource1.c>... [- <arg1>...] : run a program (calls main() to start it)\n"
|
2010-07-27 07:48:39 -04:00
|
|
|
" picoc -s <csource1.c>... [- <arg1>...] : script mode - runs the program without calling main()\n"
|
2010-07-19 16:16:32 -04:00
|
|
|
" picoc -i : interactive mode\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-07-27 10:03:06 -04:00
|
|
|
Initialise(StackSize);
|
2010-07-19 16:16:32 -04:00
|
|
|
|
2010-07-27 07:48:39 -04:00
|
|
|
if (strcmp(argv[ParamCount], "-s") == 0 || strcmp(argv[ParamCount], "-m") == 0)
|
2010-07-19 16:16:32 -04:00
|
|
|
{
|
|
|
|
DontRunMain = TRUE;
|
2010-07-27 07:48:39 -04:00
|
|
|
IncludeAllSystemHeaders();
|
2010-07-19 16:16:32 -04:00
|
|
|
ParamCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[ParamCount], "-i") == 0)
|
2010-07-27 07:48:39 -04:00
|
|
|
{
|
|
|
|
IncludeAllSystemHeaders();
|
2010-07-19 16:16:32 -04:00
|
|
|
ParseInteractive();
|
2010-07-27 07:48:39 -04:00
|
|
|
}
|
2010-07-19 16:16:32 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PlatformSetExitPoint())
|
|
|
|
{
|
|
|
|
Cleanup();
|
2010-07-27 11:35:25 -04:00
|
|
|
return ExitValue;
|
2010-07-19 16:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (; ParamCount < argc && strcmp(argv[ParamCount], "-") != 0; ParamCount++)
|
|
|
|
PlatformScanFile(argv[ParamCount]);
|
|
|
|
|
|
|
|
if (!DontRunMain)
|
|
|
|
CallMain(argc - ParamCount, &argv[ParamCount]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cleanup();
|
2010-07-27 12:20:09 -04:00
|
|
|
return ExitValue;
|
2010-07-19 16:16:32 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# ifdef SURVEYOR_HOST
|
|
|
|
int picoc(char *SourceStr)
|
|
|
|
{
|
|
|
|
int ix;
|
|
|
|
|
2010-07-27 10:03:06 -04:00
|
|
|
Initialise(HEAP_SIZE);
|
2010-07-19 16:16:32 -04:00
|
|
|
if (SourceStr) {
|
|
|
|
for (ix=0; ix<strlen(SourceStr); ix++) /* clear out ctrl-z from XMODEM transfer */
|
|
|
|
if (SourceStr[ix] == 0x1A)
|
|
|
|
SourceStr[ix] = 0x20;
|
|
|
|
/*printf("%s\n\r", SourceStr);*/ /* display program source */
|
|
|
|
/*printf("=====================\n");*/
|
|
|
|
}
|
|
|
|
ExitBuf[40] = 0;
|
|
|
|
PlatformSetExitPoint();
|
|
|
|
if (ExitBuf[40]) {
|
|
|
|
printf("leaving picoC\n\r");
|
|
|
|
Cleanup();
|
2010-07-27 11:35:25 -04:00
|
|
|
return ExitValue;
|
2010-07-19 16:16:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SourceStr)
|
|
|
|
Parse("test.c", SourceStr, strlen(SourceStr), TRUE);
|
|
|
|
ParseInteractive();
|
|
|
|
Cleanup();
|
2010-07-27 12:20:09 -04:00
|
|
|
return ExitValue;
|
2010-07-19 16:16:32 -04:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|