git-svn-id: http://picoc.googlecode.com/svn/trunk@467 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2010-07-19 20:16:32 +00:00
parent 3deb0908bb
commit e744c816c5

319
picoc.c
View file

@ -1,160 +1,159 @@
#include "picoc.h" #include "picoc.h"
#define CALL_MAIN_NO_ARGS "main();" #define CALL_MAIN_NO_ARGS "main();"
#define CALL_MAIN_WITH_ARGS "main(__argc,__argv);" #define CALL_MAIN_WITH_ARGS "main(__argc,__argv);"
/* initialise everything */ /* initialise everything */
void Initialise() void Initialise()
{ {
BasicIOInit(); BasicIOInit();
HeapInit(); HeapInit();
TableInit(); TableInit();
VariableInit(); VariableInit();
LexInit(); LexInit();
TypeInit(); TypeInit();
IncludeInit(); IncludeInit();
#ifdef BUILTIN_MINI_STDLIB #ifdef BUILTIN_MINI_STDLIB
LibraryInit(&GlobalTable, "c library", &CLibrary[0]); LibraryInit(&GlobalTable, "c library", &CLibrary[0]);
CLibraryInit(); CLibraryInit();
#endif #endif
PlatformLibraryInit(); PlatformLibraryInit();
} }
/* free memory */ /* free memory */
void Cleanup() void Cleanup()
{ {
PlatformCleanup(); PlatformCleanup();
IncludeCleanup(); IncludeCleanup();
ParseCleanup(); ParseCleanup();
LexCleanup(); LexCleanup();
VariableCleanup(); VariableCleanup();
TypeCleanup(); TypeCleanup();
TableStrFree(); TableStrFree();
} }
/* platform-dependent code for running programs is in this file */ /* platform-dependent code for running programs is in this file */
#ifdef UNIX_HOST #ifdef UNIX_HOST
void CallMain(int argc, char **argv) void CallMain(int argc, char **argv)
{ {
/* check if the program wants arguments */ /* check if the program wants arguments */
struct Value *FuncValue = NULL; struct Value *FuncValue = NULL;
if (!VariableDefined(TableStrRegister("main"))) if (!VariableDefined(TableStrRegister("main")))
return; return;
VariableGet(NULL, TableStrRegister("main"), &FuncValue); VariableGet(NULL, TableStrRegister("main"), &FuncValue);
if (FuncValue->Typ->Base != TypeFunction) if (FuncValue->Typ->Base != TypeFunction)
ProgramFail(NULL, "main is not a function - can't call it"); ProgramFail(NULL, "main is not a function - can't call it");
if (FuncValue->Val->FuncDef.NumParams == 0) if (FuncValue->Val->FuncDef.NumParams == 0)
Parse("", CALL_MAIN_NO_ARGS, strlen(CALL_MAIN_NO_ARGS), TRUE); Parse("", CALL_MAIN_NO_ARGS, strlen(CALL_MAIN_NO_ARGS), TRUE);
else else
{ {
/* define the arguments */ /* define the arguments */
VariableDefinePlatformVar(NULL, "__argc", &IntType, (union AnyValue *)&argc, FALSE); VariableDefinePlatformVar(NULL, "__argc", &IntType, (union AnyValue *)&argc, FALSE);
VariableDefinePlatformVar(NULL, "__argv", CharPtrPtrType, (union AnyValue *)&argv, FALSE); VariableDefinePlatformVar(NULL, "__argv", CharPtrPtrType, (union AnyValue *)&argv, FALSE);
Parse("", CALL_MAIN_WITH_ARGS, strlen(CALL_MAIN_WITH_ARGS), TRUE); Parse("", CALL_MAIN_WITH_ARGS, strlen(CALL_MAIN_WITH_ARGS), TRUE);
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ParamCount = 1; int ParamCount = 1;
int DontRunMain = FALSE; int DontRunMain = FALSE;
if (argc < 2) if (argc < 2)
{ {
printf("Format: picoc <csource1.c>... [- <arg1>...] : run a program (calls main() to start it)\n" printf("Format: picoc <csource1.c>... [- <arg1>...] : run a program (calls main() to start it)\n"
" picoc -m <csource1.c>... [- <arg1>...] : run a program without calling main()\n" " picoc -m <csource1.c>... [- <arg1>...] : run a program without calling main()\n"
" picoc -i : interactive mode\n"); " picoc -i : interactive mode\n");
exit(1); exit(1);
} }
Initialise(); Initialise();
if (strcmp(argv[ParamCount], "-m") == 0) if (strcmp(argv[ParamCount], "-m") == 0)
{ {
DontRunMain = TRUE; DontRunMain = TRUE;
ParamCount++; ParamCount++;
} }
if (strcmp(argv[ParamCount], "-i") == 0) if (strcmp(argv[ParamCount], "-i") == 0)
ParseInteractive(); ParseInteractive();
else else
{ {
if (PlatformSetExitPoint()) if (PlatformSetExitPoint())
{ {
Cleanup(); Cleanup();
return 1; return 1;
} }
for (; ParamCount < argc && strcmp(argv[ParamCount], "-") != 0; ParamCount++) for (; ParamCount < argc && strcmp(argv[ParamCount], "-") != 0; ParamCount++)
PlatformScanFile(argv[ParamCount]); PlatformScanFile(argv[ParamCount]);
if (!DontRunMain) if (!DontRunMain)
CallMain(argc - ParamCount, &argv[ParamCount]); CallMain(argc - ParamCount, &argv[ParamCount]);
} }
Cleanup(); Cleanup();
return 0; return 0;
} }
#else #else
# ifdef SURVEYOR_HOST # ifdef SURVEYOR_HOST
int picoc(char *SourceStr) int picoc(char *SourceStr)
{ {
int ix; int ix;
Initialise(); Initialise();
if (SourceStr) { if (SourceStr) {
for (ix=0; ix<strlen(SourceStr); ix++) /* clear out ctrl-z from XMODEM transfer */ for (ix=0; ix<strlen(SourceStr); ix++) /* clear out ctrl-z from XMODEM transfer */
if (SourceStr[ix] == 0x1A) if (SourceStr[ix] == 0x1A)
SourceStr[ix] = 0x20; SourceStr[ix] = 0x20;
/*printf("%s\n\r", SourceStr);*/ /* display program source */ /*printf("%s\n\r", SourceStr);*/ /* display program source */
/*printf("=====================\n");*/ /*printf("=====================\n");*/
} }
ExitBuf[40] = 0; ExitBuf[40] = 0;
PlatformSetExitPoint(); PlatformSetExitPoint();
if (ExitBuf[40]) { if (ExitBuf[40]) {
printf("leaving picoC\n\r"); printf("leaving picoC\n\r");
Cleanup(); Cleanup();
return 1; return 1;
} }
if (SourceStr) if (SourceStr)
Parse("test.c", SourceStr, strlen(SourceStr), TRUE); Parse("test.c", SourceStr, strlen(SourceStr), TRUE);
ParseInteractive(); ParseInteractive();
Cleanup(); Cleanup();
return 0; return 0;
} }
# else # else
# ifdef SRV1_UNIX_HOST # ifdef SRV1_UNIX_HOST
/* /*
* Howard - this was my guess at what might suit you. * Howard - this was my guess at what might suit you.
* Please feel free to change this to whatever type of main function suits you best * Please feel free to change this to whatever type of main function suits you best
*/ */
int picoc(char *SourceFile, int Interactive) int picoc(char *SourceFile, int Interactive)
{ {
Initialise(); Initialise();
if (Interactive) if (Interactive)
ParseInteractive(); ParseInteractive();
else else
{ {
if (PlatformSetExitPoint()) if (PlatformSetExitPoint())
{ {
Cleanup(); Cleanup();
return 1; return 1;
} }
PlatformScanFile(SourceFile); PlatformScanFile(SourceFile);
} }
Cleanup(); Cleanup();
return 0; return 0;
} }
# endif # endif
# endif # endif
#endif #endif