-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for #include of predefined libraries.
Created a stdio.h predefined library.
- Loading branch information
zik.saleeba
committed
Jun 5, 2010
1 parent
e572ba3
commit b782c1b
Showing
8 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "picoc.h" | ||
|
||
#ifndef NO_HASH_INCLUDE | ||
|
||
/* a list of libraries we can include */ | ||
struct IncludeLibrary | ||
{ | ||
const char *IncludeName; | ||
void (*SetupFunction)(void); | ||
struct LibraryFunction (*FuncList)[]; | ||
const char *SetupCSource; | ||
}; | ||
|
||
struct IncludeLibrary IncludeLibInfo[] = | ||
{ | ||
{ "stdio.h", | ||
&StdioSetupFunc, | ||
&StdioFunctions, | ||
StdioDefs }, | ||
{ NULL, NULL, NULL, NULL } | ||
}; | ||
|
||
|
||
/* include one of a number of predefined libraries, or perhaps an actual file */ | ||
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++) | ||
{ | ||
if (strcmp(LInclude->IncludeName, FileName) == 0) | ||
{ | ||
/* found it - protect against multiple inclusion */ | ||
if (!VariableDefined(FileName)) | ||
{ | ||
VariableDefine(NULL, FileName, NULL, &VoidType, FALSE); | ||
|
||
/* parse the setup C source code - may define types etc. */ | ||
if (LInclude->SetupFunction != NULL) | ||
(*LInclude->SetupFunction)(); | ||
|
||
/* parse the setup C source code - may define types etc. */ | ||
if (LInclude->SetupCSource != NULL) | ||
Parse(FileName, LInclude->SetupCSource, strlen(LInclude->SetupCSource), TRUE); | ||
|
||
/* set up the library functions */ | ||
if (LInclude->FuncList != NULL) | ||
LibraryInit(&GlobalTable, FileName, LInclude->FuncList); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
/* not a predefined file, read a real file */ | ||
PlatformScanFile(FileName); | ||
} | ||
|
||
#endif /* NO_HASH_INCLUDE */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* stdio.h library */ | ||
#include "picoc.h" | ||
|
||
#ifndef NO_HASH_INCLUDE | ||
|
||
void StdioFopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) | ||
{ | ||
ReturnValue->Val->NativePointer = fopen(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer); | ||
} | ||
|
||
void StdioFclose(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) | ||
{ | ||
ReturnValue->Val->Integer = fclose(Param[0]->Val->NativePointer); | ||
} | ||
|
||
void StdioFread(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) | ||
{ | ||
ReturnValue->Val->Integer = fread(Param[0]->Val->NativePointer, Param[1]->Val->Integer, Param[2]->Val->Integer, Param[3]->Val->NativePointer); | ||
} | ||
|
||
void StdioFwrite(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) | ||
{ | ||
ReturnValue->Val->Integer = fwrite(Param[0]->Val->NativePointer, Param[1]->Val->Integer, Param[2]->Val->Integer, Param[3]->Val->NativePointer); | ||
} | ||
|
||
void StdioFgetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) | ||
{ | ||
ReturnValue->Val->Integer = fgetc(Param[0]->Val->NativePointer); | ||
} | ||
|
||
void StdioFgets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) | ||
{ | ||
ReturnValue->Val->NativePointer = fgets(Param[0]->Val->NativePointer, Param[1]->Val->Integer, Param[2]->Val->NativePointer); | ||
} | ||
|
||
const char StdioDefs[] = "\ | ||
typedef struct FILEStruct FILE; \ | ||
"; | ||
|
||
struct LibraryFunction StdioFunctions[] = | ||
{ | ||
{ StdioFopen, "FILE *fopen(char *, char *);" }, | ||
{ StdioFclose, "int fclose(FILE *);" }, | ||
{ StdioFread, "int fread(void *, int, int, FILE *);" }, | ||
{ StdioFwrite, "int fwrite(void *, int, int, FILE *);" }, | ||
{ StdioFgetc, "int fgetc(FILE *);" }, | ||
{ StdioFgetc, "int getc(FILE *);" }, | ||
{ StdioFgets, "char *fgets(char *, int, FILE *);" }, | ||
{ NULL, NULL } | ||
}; | ||
|
||
void StdioSetupFunc(void) | ||
{ | ||
/* make a "struct FILEStruct" which is the same size as a native FILE structure */ | ||
TypeCreateOpaqueStruct(NULL, TableStrRegister("FILEStruct"), sizeof(FILE)); | ||
} | ||
|
||
#endif /* NO_HASH_INCLUDE */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters