2011-05-01 02:50:00 -04:00
|
|
|
/* picoc include system - can emulate system includes from built-in libraries
|
|
|
|
* or it can include and parse files if the system has files */
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-05 15:00:46 -04:00
|
|
|
#include "picoc.h"
|
2011-02-17 02:11:20 -05:00
|
|
|
#include "interpreter.h"
|
2010-06-05 15:00:46 -04:00
|
|
|
|
2010-06-13 16:47:17 -04:00
|
|
|
|
|
|
|
/* initialise the built-in include libraries */
|
2012-09-22 01:11:44 -04:00
|
|
|
void IncludeInit(Picoc *pc)
|
2010-06-05 15:00:46 -04:00
|
|
|
{
|
2012-09-22 01:11:44 -04:00
|
|
|
IncludeRegister(pc, "ctype.h", NULL, &StdCtypeFunctions[0], NULL);
|
|
|
|
IncludeRegister(pc, "errno.h", &StdErrnoSetupFunc, NULL, NULL);
|
2011-10-05 06:57:24 -04:00
|
|
|
# ifndef NO_FP
|
2012-09-22 01:11:44 -04:00
|
|
|
IncludeRegister(pc, "math.h", &MathSetupFunc, &MathFunctions[0], NULL);
|
2011-10-05 06:57:24 -04:00
|
|
|
# endif
|
2012-09-22 01:11:44 -04:00
|
|
|
IncludeRegister(pc, "stdbool.h", &StdboolSetupFunc, NULL, StdboolDefs);
|
|
|
|
IncludeRegister(pc, "stdio.h", &StdioSetupFunc, &StdioFunctions[0], StdioDefs);
|
|
|
|
IncludeRegister(pc, "stdlib.h", &StdlibSetupFunc, &StdlibFunctions[0], NULL);
|
|
|
|
IncludeRegister(pc, "string.h", &StringSetupFunc, &StringFunctions[0], NULL);
|
|
|
|
IncludeRegister(pc, "time.h", &StdTimeSetupFunc, &StdTimeFunctions[0], StdTimeDefs);
|
2011-10-05 06:57:24 -04:00
|
|
|
# ifndef WIN32
|
2012-09-22 01:11:44 -04:00
|
|
|
IncludeRegister(pc, "unistd.h", &UnistdSetupFunc, &UnistdFunctions[0], UnistdDefs);
|
2011-10-05 06:57:24 -04:00
|
|
|
# endif
|
2010-06-13 16:47:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up space used by the include system */
|
2012-09-22 01:11:44 -04:00
|
|
|
void IncludeCleanup(Picoc *pc)
|
2010-06-13 16:47:17 -04:00
|
|
|
{
|
2012-09-22 01:11:44 -04:00
|
|
|
struct IncludeLibrary *ThisInclude = pc->IncludeLibList;
|
2010-06-13 16:47:17 -04:00
|
|
|
struct IncludeLibrary *NextInclude;
|
2015-06-07 00:40:08 -04:00
|
|
|
|
|
|
|
while (ThisInclude != NULL) {
|
2010-06-13 16:47:17 -04:00
|
|
|
NextInclude = ThisInclude->NextLib;
|
2012-09-22 01:11:44 -04:00
|
|
|
HeapFreeMem(pc, ThisInclude);
|
2010-06-13 16:47:17 -04:00
|
|
|
ThisInclude = NextInclude;
|
|
|
|
}
|
2010-08-21 02:07:02 -04:00
|
|
|
|
2012-09-22 01:11:44 -04:00
|
|
|
pc->IncludeLibList = NULL;
|
2010-06-13 16:47:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* register a new build-in include file */
|
2012-09-22 01:11:44 -04:00
|
|
|
void IncludeRegister(Picoc *pc, const char *IncludeName, void (*SetupFunction)(Picoc *pc), struct LibraryFunction *FuncList, const char *SetupCSource)
|
2010-06-13 16:47:17 -04:00
|
|
|
{
|
2012-09-22 01:11:44 -04:00
|
|
|
struct IncludeLibrary *NewLib = HeapAllocMem(pc, sizeof(struct IncludeLibrary));
|
|
|
|
NewLib->IncludeName = TableStrRegister(pc, IncludeName);
|
2010-06-13 16:47:17 -04:00
|
|
|
NewLib->SetupFunction = SetupFunction;
|
|
|
|
NewLib->FuncList = FuncList;
|
|
|
|
NewLib->SetupCSource = SetupCSource;
|
2012-09-22 01:11:44 -04:00
|
|
|
NewLib->NextLib = pc->IncludeLibList;
|
|
|
|
pc->IncludeLibList = NewLib;
|
2010-06-13 16:47:17 -04:00
|
|
|
}
|
2010-06-05 15:00:46 -04:00
|
|
|
|
2010-07-27 07:48:39 -04:00
|
|
|
/* include all of the system headers */
|
2012-09-22 01:11:44 -04:00
|
|
|
void PicocIncludeAllSystemHeaders(Picoc *pc)
|
2010-07-27 07:48:39 -04:00
|
|
|
{
|
2012-09-22 01:11:44 -04:00
|
|
|
struct IncludeLibrary *ThisInclude = pc->IncludeLibList;
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-07-27 07:48:39 -04:00
|
|
|
for (; ThisInclude != NULL; ThisInclude = ThisInclude->NextLib)
|
2012-09-22 01:11:44 -04:00
|
|
|
IncludeFile(pc, ThisInclude->IncludeName);
|
2010-07-27 07:48:39 -04:00
|
|
|
}
|
2010-06-05 15:00:46 -04:00
|
|
|
|
|
|
|
/* include one of a number of predefined libraries, or perhaps an actual file */
|
2012-09-22 01:11:44 -04:00
|
|
|
void IncludeFile(Picoc *pc, char *FileName)
|
2010-06-05 15:00:46 -04:00
|
|
|
{
|
|
|
|
struct IncludeLibrary *LInclude;
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-05 15:00:46 -04:00
|
|
|
/* scan for the include file name to see if it's in our list of predefined includes */
|
2015-06-07 00:40:08 -04:00
|
|
|
for (LInclude = pc->IncludeLibList; LInclude != NULL; LInclude = LInclude->NextLib) {
|
|
|
|
if (strcmp(LInclude->IncludeName, FileName) == 0) {
|
2010-06-05 15:00:46 -04:00
|
|
|
/* found it - protect against multiple inclusion */
|
2015-06-07 00:40:08 -04:00
|
|
|
if (!VariableDefined(pc, FileName)) {
|
2015-06-10 15:24:53 -04:00
|
|
|
VariableDefine(pc, NULL, FileName, NULL, &pc->VoidType, false);
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-12 08:45:18 -04:00
|
|
|
/* run an extra startup function if there is one */
|
2010-06-05 15:00:46 -04:00
|
|
|
if (LInclude->SetupFunction != NULL)
|
2012-09-22 01:11:44 -04:00
|
|
|
(*LInclude->SetupFunction)(pc);
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-12 08:45:18 -04:00
|
|
|
/* parse the setup C source code - may define types etc. */
|
2010-06-05 15:00:46 -04:00
|
|
|
if (LInclude->SetupCSource != NULL)
|
2015-06-10 15:24:53 -04:00
|
|
|
PicocParse(pc, FileName, LInclude->SetupCSource, strlen(LInclude->SetupCSource), true, true, false, false);
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-05 15:00:46 -04:00
|
|
|
/* set up the library functions */
|
|
|
|
if (LInclude->FuncList != NULL)
|
2015-06-09 03:45:00 -04:00
|
|
|
LibraryAdd(pc, &pc->GlobalTable, LInclude->FuncList);
|
2010-06-05 15:00:46 -04:00
|
|
|
}
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-05 15:00:46 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-06-07 00:40:08 -04:00
|
|
|
|
2010-06-05 15:00:46 -04:00
|
|
|
/* not a predefined file, read a real file */
|
2012-09-22 01:11:44 -04:00
|
|
|
PicocPlatformScanFile(pc, FileName);
|
2010-06-05 15:00:46 -04:00
|
|
|
}
|
|
|
|
|