diff --git a/Makefile b/Makefile index 1d8342b..40d2495 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ CC=gcc -#CFLAGS=-Wall -pedantic -g -DUNIX_HOST -CFLAGS=-Wall -pedantic -g -DSRV1_UNIX_HOST +CFLAGS=-Wall -pedantic -g -DUNIX_HOST LIBS=-lm TARGET = picoc diff --git a/include.c b/include.c index 42f83cd..68e763d 100644 --- a/include.c +++ b/include.c @@ -9,28 +9,46 @@ struct IncludeLibrary void (*SetupFunction)(void); struct LibraryFunction (*FuncList)[]; const char *SetupCSource; + struct IncludeLibrary *NextLib; }; -struct IncludeLibrary IncludeLibInfo[] = +struct IncludeLibrary *IncludeLibList = NULL; + + +/* initialise the built-in include libraries */ +void IncludeInit() { - { "stdio.h", - &StdioSetupFunc, - &StdioFunctions, - StdioDefs }, - { "math.h", - &MathSetupFunc, - &MathFunctions, - NULL }, - { "string.h", - &StringSetupFunc, - &StringFunctions, - NULL }, - { "stdlib.h", - &StdlibSetupFunc, - &StdlibFunctions, - NULL }, - { NULL, NULL, NULL, NULL } -}; + IncludeRegister("stdio.h", &StdioSetupFunc, &StdioFunctions, StdioDefs); + IncludeRegister("math.h", &MathSetupFunc, &MathFunctions, NULL); + IncludeRegister("string.h", &StringSetupFunc, &StringFunctions, NULL); + IncludeRegister("stdlib.h", &StdlibSetupFunc, &StdlibFunctions, NULL); +} + +/* clean up space used by the include system */ +void IncludeCleanup() +{ + struct IncludeLibrary *ThisInclude = IncludeLibList; + struct IncludeLibrary *NextInclude; + + while (ThisInclude != NULL) + { + NextInclude = ThisInclude->NextLib; + HeapFreeMem(ThisInclude); + ThisInclude = NextInclude; + } +} + +/* register a new build-in include file */ +void IncludeRegister(const char *IncludeName, void (*SetupFunction)(void), struct LibraryFunction (*FuncList)[], const char *SetupCSource) +{ + struct IncludeLibrary *NewLib = HeapAllocMem(sizeof(struct IncludeLibrary)); + NewLib->IncludeName = TableStrRegister(IncludeName); + NewLib->SetupFunction = SetupFunction; + NewLib->FuncList = FuncList; + NewLib->SetupCSource = SetupCSource; + NewLib->NextLib = IncludeLibList; + IncludeLibList = NewLib; +} /* include one of a number of predefined libraries, or perhaps an actual file */ @@ -39,7 +57,7 @@ void IncludeFile(char *FileName) struct IncludeLibrary *LInclude; /* scan for the include file name to see if it's in our list of predefined includes */ - for (LInclude = &IncludeLibInfo[0]; LInclude->IncludeName != NULL; LInclude++) + for (LInclude = IncludeLibList; LInclude != NULL; LInclude = LInclude->NextLib) { if (strcmp(LInclude->IncludeName, FileName) == 0) { diff --git a/picoc.c b/picoc.c index 07d6027..b71198d 100644 --- a/picoc.c +++ b/picoc.c @@ -9,18 +9,19 @@ void Initialise() VariableInit(); LexInit(); TypeInit(); + IncludeInit(); #ifdef BUILTIN_MINI_STDLIB LibraryInit(&GlobalTable, "c library", &CLibrary); CLibraryInit(); #endif PlatformLibraryInit(); - LibraryInit(&GlobalTable, "platform library", &PlatformLibrary); } /* free memory */ void Cleanup() { PlatformCleanup(); + IncludeCleanup(); ParseCleanup(); LexCleanup(); VariableCleanup(); diff --git a/picoc.h b/picoc.h index cabeba8..0d6b90e 100644 --- a/picoc.h +++ b/picoc.h @@ -291,7 +291,6 @@ extern struct ValueType *VoidPtrType; extern char *StrEmpty; extern struct PointerValue NULLPointer; extern struct LibraryFunction CLibrary[]; -extern struct LibraryFunction PlatformLibrary[]; extern IOFILE *CStdOut; /* table.c */ @@ -410,6 +409,9 @@ void Initialise(); void Cleanup(); /* include.c */ +void IncludeInit(); +void IncludeCleanup(); +void IncludeRegister(const char *IncludeName, void (*SetupFunction)(void), struct LibraryFunction (*FuncList)[], const char *SetupCSource); void IncludeFile(char *Filename); /* stdio.c */ diff --git a/platform/library_ffox.c b/platform/library_ffox.c index 0b39f55..15465a9 100644 --- a/platform/library_ffox.c +++ b/platform/library_ffox.c @@ -1,12 +1,13 @@ #include "../picoc.h" -void PlatformLibraryInit() -{ -} - /* list of all library functions and their prototypes */ struct LibraryFunction PlatformLibrary[] = { { NULL, NULL } }; +void PlatformLibraryInit() +{ + LibraryInit(&GlobalTable, "platform library", &PlatformLibrary); +} + diff --git a/platform/library_srv1.c b/platform/library_srv1.c index 3cfd42b..6b6f7ed 100644 --- a/platform/library_srv1.c +++ b/platform/library_srv1.c @@ -4,10 +4,11 @@ static int Blobcnt, Blobx1, Blobx2, Bloby1, Bloby2, Iy1, Iy2, Iu1, Iu2, Iv1, Iv2 static int GPSlat, GPSlon, GPSalt, GPSfix, GPSsat, GPSutc, Elcount, Ercount; static int ScanVect[16], NNVect[NUM_OUTPUT]; -void PlatformLibraryInit() -{ - struct ValueType *IntArrayType; - +struct ValueType *IntArrayType; + + +void SRV1SetupFunc() +{ IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty); VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE); VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE); @@ -733,8 +734,14 @@ void Cerrormsg (struct ParseState *Parser, struct Value *ReturnValue, struct Val LibPrintf(Parser, ReturnValue, Param, NumArgs); } -/* list of all library functions and their prototypes */ +/* nothing here because we don't add any functions until srv1.h is #included */ struct LibraryFunction PlatformLibrary[] = +{ + { NULL, NULL } +}; + +/* list of all library functions included with srv1.h */ +struct LibraryFunction SRV1Functions[] = { { Csignal, "int signal();" }, { Cinput, "int input();" }, @@ -796,3 +803,7 @@ struct LibraryFunction PlatformLibrary[] = { NULL, NULL } }; +void PlatformLibraryInit() +{ + IncludeRegister("srv1.h", &SRV1SetupFunc, &SRV1Functions, NULL); +} diff --git a/platform/library_surveyor.c b/platform/library_surveyor.c index 3cfd42b..3eb273a 100644 --- a/platform/library_surveyor.c +++ b/platform/library_surveyor.c @@ -4,34 +4,6 @@ static int Blobcnt, Blobx1, Blobx2, Bloby1, Bloby2, Iy1, Iy2, Iu1, Iu2, Iv1, Iv2 static int GPSlat, GPSlon, GPSalt, GPSfix, GPSsat, GPSutc, Elcount, Ercount; static int ScanVect[16], NNVect[NUM_OUTPUT]; -void PlatformLibraryInit() -{ - struct ValueType *IntArrayType; - - IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty); - VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE); - VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE); - VariableDefinePlatformVar(NULL, "blobcnt", &IntType, (union AnyValue *)&Blobcnt, FALSE); - VariableDefinePlatformVar(NULL, "blobx1", &IntType, (union AnyValue *)&Blobx1, FALSE); - VariableDefinePlatformVar(NULL, "blobx2", &IntType, (union AnyValue *)&Blobx2, FALSE); - VariableDefinePlatformVar(NULL, "bloby1", &IntType, (union AnyValue *)&Bloby1, FALSE); - VariableDefinePlatformVar(NULL, "bloby2", &IntType, (union AnyValue *)&Bloby2, FALSE); - VariableDefinePlatformVar(NULL, "lcount", &IntType, (union AnyValue *)&Elcount, FALSE); - VariableDefinePlatformVar(NULL, "rcount", &IntType, (union AnyValue *)&Ercount, FALSE); - VariableDefinePlatformVar(NULL, "y1", &IntType, (union AnyValue *)&Iy1, FALSE); - VariableDefinePlatformVar(NULL, "y2", &IntType, (union AnyValue *)&Iy2, FALSE); - VariableDefinePlatformVar(NULL, "u1", &IntType, (union AnyValue *)&Iu1, FALSE); - VariableDefinePlatformVar(NULL, "u2", &IntType, (union AnyValue *)&Iu2, FALSE); - VariableDefinePlatformVar(NULL, "v1", &IntType, (union AnyValue *)&Iv1, FALSE); - VariableDefinePlatformVar(NULL, "v2", &IntType, (union AnyValue *)&Iv2, FALSE); - VariableDefinePlatformVar(NULL, "gpslat", &IntType, (union AnyValue *)&GPSlat, FALSE); - VariableDefinePlatformVar(NULL, "gpslon", &IntType, (union AnyValue *)&GPSlon, FALSE); - VariableDefinePlatformVar(NULL, "gpsalt", &IntType, (union AnyValue *)&GPSalt, FALSE); - VariableDefinePlatformVar(NULL, "gpsfix", &IntType, (union AnyValue *)&GPSfix, FALSE); - VariableDefinePlatformVar(NULL, "gpssat", &IntType, (union AnyValue *)&GPSsat, FALSE); - VariableDefinePlatformVar(NULL, "gpsutc", &IntType, (union AnyValue *)&GPSutc, FALSE); -} - void Csignal(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // check for kbhit, return t or nil { if (getsignal()) @@ -796,3 +768,33 @@ struct LibraryFunction PlatformLibrary[] = { NULL, NULL } }; +void PlatformLibraryInit() +{ + struct ValueType *IntArrayType; + + IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty); + VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE); + VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE); + VariableDefinePlatformVar(NULL, "blobcnt", &IntType, (union AnyValue *)&Blobcnt, FALSE); + VariableDefinePlatformVar(NULL, "blobx1", &IntType, (union AnyValue *)&Blobx1, FALSE); + VariableDefinePlatformVar(NULL, "blobx2", &IntType, (union AnyValue *)&Blobx2, FALSE); + VariableDefinePlatformVar(NULL, "bloby1", &IntType, (union AnyValue *)&Bloby1, FALSE); + VariableDefinePlatformVar(NULL, "bloby2", &IntType, (union AnyValue *)&Bloby2, FALSE); + VariableDefinePlatformVar(NULL, "lcount", &IntType, (union AnyValue *)&Elcount, FALSE); + VariableDefinePlatformVar(NULL, "rcount", &IntType, (union AnyValue *)&Ercount, FALSE); + VariableDefinePlatformVar(NULL, "y1", &IntType, (union AnyValue *)&Iy1, FALSE); + VariableDefinePlatformVar(NULL, "y2", &IntType, (union AnyValue *)&Iy2, FALSE); + VariableDefinePlatformVar(NULL, "u1", &IntType, (union AnyValue *)&Iu1, FALSE); + VariableDefinePlatformVar(NULL, "u2", &IntType, (union AnyValue *)&Iu2, FALSE); + VariableDefinePlatformVar(NULL, "v1", &IntType, (union AnyValue *)&Iv1, FALSE); + VariableDefinePlatformVar(NULL, "v2", &IntType, (union AnyValue *)&Iv2, FALSE); + VariableDefinePlatformVar(NULL, "gpslat", &IntType, (union AnyValue *)&GPSlat, FALSE); + VariableDefinePlatformVar(NULL, "gpslon", &IntType, (union AnyValue *)&GPSlon, FALSE); + VariableDefinePlatformVar(NULL, "gpsalt", &IntType, (union AnyValue *)&GPSalt, FALSE); + VariableDefinePlatformVar(NULL, "gpsfix", &IntType, (union AnyValue *)&GPSfix, FALSE); + VariableDefinePlatformVar(NULL, "gpssat", &IntType, (union AnyValue *)&GPSsat, FALSE); + VariableDefinePlatformVar(NULL, "gpsutc", &IntType, (union AnyValue *)&GPSutc, FALSE); + + LibraryInit(&GlobalTable, "platform library", &PlatformLibrary); +} + diff --git a/platform/library_unix.c b/platform/library_unix.c index fce00d3..a2df558 100644 --- a/platform/library_unix.c +++ b/platform/library_unix.c @@ -1,7 +1,7 @@ #include "../picoc.h" -void PlatformLibraryInit() -{ +void UnixSetupFunc() +{ } void Ctest (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) @@ -16,10 +16,14 @@ void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value } /* list of all library functions and their prototypes */ -struct LibraryFunction PlatformLibrary[] = +struct LibraryFunction UnixFunctions[] = { { Ctest, "void test(int);" }, { Clineno, "int lineno();" }, { NULL, NULL } }; +void PlatformLibraryInit() +{ + IncludeRegister("picoc_unix.h", &UnixSetupFunc, &UnixFunctions, NULL); +}