misc formatting, more descriptive out of mem erros, increase default stack size

This commit is contained in:
Joseph Poirier 2015-06-09 02:45:00 -05:00
parent 6c9bd24783
commit c504c52cd1
19 changed files with 259 additions and 234 deletions

View file

@ -2,7 +2,7 @@ CC=gcc
# -O3 # -O3
# -std=gnu11 # -std=gnu11
CFLAGS=-Wall -O3 -std=gnu11 -pedantic -DUNIX_HOST -DVER=\"`git show-ref --abbrev=8 --head --hash head`\" -DTAG=\"`git describe --abbrev=0 --tags`\" CFLAGS=-Wall -g -std=gnu11 -pedantic -DUNIX_HOST -DVER=\"`git show-ref --abbrev=8 --head --hash head`\" -DTAG=\"`git describe --abbrev=0 --tags`\"
LIBS=-lm -lreadline LIBS=-lm -lreadline
TARGET = picoc TARGET = picoc
@ -21,6 +21,7 @@ $(TARGET): $(OBJS)
test: all test: all
(cd tests; make test) (cd tests; make test)
(cd tests; make csmith)
clean: clean:
rm -f $(TARGET) $(OBJS) *~ rm -f $(TARGET) $(OBJS) *~

View file

@ -27,7 +27,7 @@ void LibraryInit(Picoc *pc)
} }
/* add a library */ /* add a library */
void LibraryAdd(Picoc *pc, struct Table *GlobalTable, const char *LibraryName, struct LibraryFunction *FuncList) void LibraryAdd(Picoc *pc, struct Table *GlobalTable, struct LibraryFunction *FuncList)
{ {
struct ParseState Parser; struct ParseState Parser;
int Count; int Count;
@ -39,7 +39,7 @@ void LibraryAdd(Picoc *pc, struct Table *GlobalTable, const char *LibraryName, s
/* read all the library definitions */ /* read all the library definitions */
for (Count = 0; FuncList[Count].Prototype != NULL; Count++) { for (Count = 0; FuncList[Count].Prototype != NULL; Count++) {
Tokens = LexAnalyse(pc, IntrinsicName, FuncList[Count].Prototype, strlen((char *)FuncList[Count].Prototype), NULL); Tokens = LexAnalyse(pc, (const char*)IntrinsicName, FuncList[Count].Prototype, strlen((char*)FuncList[Count].Prototype), NULL);
LexInitParser(&Parser, pc, FuncList[Count].Prototype, Tokens, IntrinsicName, TRUE, FALSE); LexInitParser(&Parser, pc, FuncList[Count].Prototype, Tokens, IntrinsicName, TRUE, FALSE);
TypeParse(&Parser, &ReturnType, &Identifier, NULL); TypeParse(&Parser, &ReturnType, &Identifier, NULL);
NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier); NewValue = ParseFunctionDefinition(&Parser, ReturnType, Identifier);

View file

@ -87,22 +87,22 @@ void StdToascii(struct ParseState *Parser, struct Value *ReturnValue, struct Val
/* all string.h functions */ /* all string.h functions */
struct LibraryFunction StdCtypeFunctions[] = struct LibraryFunction StdCtypeFunctions[] =
{ {
{ StdIsalnum, "int isalnum(int);" }, {StdIsalnum, "int isalnum(int);"},
{ StdIsalpha, "int isalpha(int);" }, {StdIsalpha, "int isalpha(int);"},
{ StdIsblank, "int isblank(int);" }, {StdIsblank, "int isblank(int);"},
{ StdIscntrl, "int iscntrl(int);" }, {StdIscntrl, "int iscntrl(int);"},
{ StdIsdigit, "int isdigit(int);" }, {StdIsdigit, "int isdigit(int);"},
{ StdIsgraph, "int isgraph(int);" }, {StdIsgraph, "int isgraph(int);"},
{ StdIslower, "int islower(int);" }, {StdIslower, "int islower(int);"},
{ StdIsprint, "int isprint(int);" }, {StdIsprint, "int isprint(int);"},
{ StdIspunct, "int ispunct(int);" }, {StdIspunct, "int ispunct(int);"},
{ StdIsspace, "int isspace(int);" }, {StdIsspace, "int isspace(int);"},
{ StdIsupper, "int isupper(int);" }, {StdIsupper, "int isupper(int);"},
{ StdIsxdigit, "int isxdigit(int);" }, {StdIsxdigit, "int isxdigit(int);"},
{ StdTolower, "int tolower(int);" }, {StdTolower, "int tolower(int);"},
{ StdToupper, "int toupper(int);" }, {StdToupper, "int toupper(int);"},
{ StdIsascii, "int isascii(int);" }, {StdIsascii, "int isascii(int);"},
{ StdToascii, "int toascii(int);" }, {StdToascii, "int toascii(int);"},
{ NULL, NULL } {NULL, NULL }
}; };

View file

@ -138,30 +138,30 @@ void MathFloor(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
/* all math.h functions */ /* all math.h functions */
struct LibraryFunction MathFunctions[] = struct LibraryFunction MathFunctions[] =
{ {
{ MathAcos, "float acos(float);" }, {MathAcos, "float acos(float);"},
{ MathAsin, "float asin(float);" }, {MathAsin, "float asin(float);"},
{ MathAtan, "float atan(float);" }, {MathAtan, "float atan(float);"},
{ MathAtan2, "float atan2(float, float);" }, {MathAtan2, "float atan2(float, float);"},
{ MathCeil, "float ceil(float);" }, {MathCeil, "float ceil(float);"},
{ MathCos, "float cos(float);" }, {MathCos, "float cos(float);"},
{ MathCosh, "float cosh(float);" }, {MathCosh, "float cosh(float);"},
{ MathExp, "float exp(float);" }, {MathExp, "float exp(float);"},
{ MathFabs, "float fabs(float);" }, {MathFabs, "float fabs(float);"},
{ MathFloor, "float floor(float);" }, {MathFloor, "float floor(float);"},
{ MathFmod, "float fmod(float, float);" }, {MathFmod, "float fmod(float, float);"},
{ MathFrexp, "float frexp(float, int *);" }, {MathFrexp, "float frexp(float, int *);"},
{ MathLdexp, "float ldexp(float, int);" }, {MathLdexp, "float ldexp(float, int);"},
{ MathLog, "float log(float);" }, {MathLog, "float log(float);"},
{ MathLog10, "float log10(float);" }, {MathLog10, "float log10(float);"},
{ MathModf, "float modf(float, float *);" }, {MathModf, "float modf(float, float *);"},
{ MathPow, "float pow(float,float);" }, {MathPow, "float pow(float,float);"},
{ MathRound, "float round(float);" }, {MathRound, "float round(float);"},
{ MathSin, "float sin(float);" }, {MathSin, "float sin(float);"},
{ MathSinh, "float sinh(float);" }, {MathSinh, "float sinh(float);"},
{ MathSqrt, "float sqrt(float);" }, {MathSqrt, "float sqrt(float);"},
{ MathTan, "float tan(float);" }, {MathTan, "float tan(float);"},
{ MathTanh, "float tanh(float);" }, {MathTanh, "float tanh(float);"},
{ NULL, NULL } {NULL, NULL }
}; };
/* creates various system-dependent definitions */ /* creates various system-dependent definitions */

View file

@ -583,54 +583,54 @@ typedef struct __FILEStruct FILE;\
/* all stdio functions */ /* all stdio functions */
struct LibraryFunction StdioFunctions[] = struct LibraryFunction StdioFunctions[] =
{ {
{ StdioFopen, "FILE *fopen(char *, char *);" }, {StdioFopen, "FILE *fopen(char *, char *);"},
{ StdioFreopen, "FILE *freopen(char *, char *, FILE *);" }, {StdioFreopen, "FILE *freopen(char *, char *, FILE *);"},
{ StdioFclose, "int fclose(FILE *);" }, {StdioFclose, "int fclose(FILE *);"},
{ StdioFread, "int fread(void *, int, int, FILE *);" }, {StdioFread, "int fread(void *, int, int, FILE *);"},
{ StdioFwrite, "int fwrite(void *, int, int, FILE *);" }, {StdioFwrite, "int fwrite(void *, int, int, FILE *);"},
{ StdioFgetc, "int fgetc(FILE *);" }, {StdioFgetc, "int fgetc(FILE *);"},
{ StdioFgetc, "int getc(FILE *);" }, {StdioFgetc, "int getc(FILE *);"},
{ StdioFgets, "char *fgets(char *, int, FILE *);" }, {StdioFgets, "char *fgets(char *, int, FILE *);"},
{ StdioFputc, "int fputc(int, FILE *);" }, {StdioFputc, "int fputc(int, FILE *);"},
{ StdioFputs, "int fputs(char *, FILE *);" }, {StdioFputs, "int fputs(char *, FILE *);"},
{ StdioRemove, "int remove(char *);" }, {StdioRemove, "int remove(char *);"},
{ StdioRename, "int rename(char *, char *);" }, {StdioRename, "int rename(char *, char *);"},
{ StdioRewind, "void rewind(FILE *);" }, {StdioRewind, "void rewind(FILE *);"},
{ StdioTmpfile, "FILE *tmpfile();" }, {StdioTmpfile, "FILE *tmpfile();"},
{ StdioClearerr,"void clearerr(FILE *);" }, {StdioClearerr,"void clearerr(FILE *);"},
{ StdioFeof, "int feof(FILE *);" }, {StdioFeof, "int feof(FILE *);"},
{ StdioFerror, "int ferror(FILE *);" }, {StdioFerror, "int ferror(FILE *);"},
{ StdioFileno, "int fileno(FILE *);" }, {StdioFileno, "int fileno(FILE *);"},
{ StdioFflush, "int fflush(FILE *);" }, {StdioFflush, "int fflush(FILE *);"},
{ StdioFgetpos, "int fgetpos(FILE *, int *);" }, {StdioFgetpos, "int fgetpos(FILE *, int *);"},
{ StdioFsetpos, "int fsetpos(FILE *, int *);" }, {StdioFsetpos, "int fsetpos(FILE *, int *);"},
{ StdioFtell, "int ftell(FILE *);" }, {StdioFtell, "int ftell(FILE *);"},
{ StdioFseek, "int fseek(FILE *, int, int);" }, {StdioFseek, "int fseek(FILE *, int, int);"},
{ StdioPerror, "void perror(char *);" }, {StdioPerror, "void perror(char *);"},
{ StdioPutc, "int putc(char *, FILE *);" }, {StdioPutc, "int putc(char *, FILE *);"},
{ StdioPutchar, "int putchar(int);" }, {StdioPutchar, "int putchar(int);"},
{ StdioPutchar, "int fputchar(int);" }, {StdioPutchar, "int fputchar(int);"},
{ StdioSetbuf, "void setbuf(FILE *, char *);" }, {StdioSetbuf, "void setbuf(FILE *, char *);"},
{ StdioSetvbuf, "void setvbuf(FILE *, char *, int, int);" }, {StdioSetvbuf, "void setvbuf(FILE *, char *, int, int);"},
{ StdioUngetc, "int ungetc(int, FILE *);" }, {StdioUngetc, "int ungetc(int, FILE *);"},
{ StdioPuts, "int puts(char *);" }, {StdioPuts, "int puts(char *);"},
{ StdioGets, "char *gets(char *);" }, {StdioGets, "char *gets(char *);"},
{ StdioGetchar, "int getchar();" }, {StdioGetchar, "int getchar();"},
{ StdioPrintf, "int printf(char *, ...);" }, {StdioPrintf, "int printf(char *, ...);"},
{ StdioFprintf, "int fprintf(FILE *, char *, ...);" }, {StdioFprintf, "int fprintf(FILE *, char *, ...);"},
{ StdioSprintf, "int sprintf(char *, char *, ...);" }, {StdioSprintf, "int sprintf(char *, char *, ...);"},
{ StdioSnprintf,"int snprintf(char *, int, char *, ...);" }, {StdioSnprintf,"int snprintf(char *, int, char *, ...);"},
{ StdioScanf, "int scanf(char *, ...);" }, {StdioScanf, "int scanf(char *, ...);"},
{ StdioFscanf, "int fscanf(FILE *, char *, ...);" }, {StdioFscanf, "int fscanf(FILE *, char *, ...);"},
{ StdioSscanf, "int sscanf(char *, char *, ...);" }, {StdioSscanf, "int sscanf(char *, char *, ...);"},
{ StdioVprintf, "int vprintf(char *, va_list);" }, {StdioVprintf, "int vprintf(char *, va_list);"},
{ StdioVfprintf,"int vfprintf(FILE *, char *, va_list);" }, {StdioVfprintf,"int vfprintf(FILE *, char *, va_list);"},
{ StdioVsprintf,"int vsprintf(char *, char *, va_list);" }, {StdioVsprintf,"int vsprintf(char *, char *, va_list);"},
{ StdioVsnprintf,"int vsnprintf(char *, int, char *, va_list);" }, {StdioVsnprintf,"int vsnprintf(char *, int, char *, va_list);"},
{ StdioVscanf, "int vscanf(char *, va_list);" }, {StdioVscanf, "int vscanf(char *, va_list);"},
{ StdioVfscanf, "int vfscanf(FILE *, char *, va_list);" }, {StdioVfscanf, "int vfscanf(FILE *, char *, va_list);"},
{ StdioVsscanf, "int vsscanf(char *, char *, va_list);" }, {StdioVsscanf, "int vsscanf(char *, char *, va_list);"},
{ NULL, NULL } {NULL, NULL}
}; };
/* creates various system-dependent definitions */ /* creates various system-dependent definitions */

View file

@ -134,32 +134,32 @@ typedef struct { \
struct LibraryFunction StdlibFunctions[] = struct LibraryFunction StdlibFunctions[] =
{ {
#ifndef NO_FP #ifndef NO_FP
{ StdlibAtof, "float atof(char *);" }, {StdlibAtof, "float atof(char *);"},
{ StdlibStrtod, "float strtod(char *,char **);" }, {StdlibStrtod, "float strtod(char *,char **);"},
#endif #endif
{ StdlibAtoi, "int atoi(char *);" }, {StdlibAtoi, "int atoi(char *);"},
{ StdlibAtol, "int atol(char *);" }, {StdlibAtol, "int atol(char *);"},
{ StdlibStrtol, "int strtol(char *,char **,int);" }, {StdlibStrtol, "int strtol(char *,char **,int);"},
{ StdlibStrtoul, "int strtoul(char *,char **,int);" }, {StdlibStrtoul, "int strtoul(char *,char **,int);"},
{ StdlibMalloc, "void *malloc(int);" }, {StdlibMalloc, "void *malloc(int);"},
{ StdlibCalloc, "void *calloc(int,int);" }, {StdlibCalloc, "void *calloc(int,int);"},
{ StdlibRealloc, "void *realloc(void *,int);" }, {StdlibRealloc, "void *realloc(void *,int);"},
{ StdlibFree, "void free(void *);" }, {StdlibFree, "void free(void *);"},
{ StdlibRand, "int rand();" }, {StdlibRand, "int rand();"},
{ StdlibSrand, "void srand(int);" }, {StdlibSrand, "void srand(int);"},
{ StdlibAbort, "void abort();" }, {StdlibAbort, "void abort();"},
{ StdlibExit, "void exit(int);" }, {StdlibExit, "void exit(int);"},
{ StdlibGetenv, "char *getenv(char *);" }, {StdlibGetenv, "char *getenv(char *);"},
{ StdlibSystem, "int system(char *);" }, {StdlibSystem, "int system(char *);"},
/* { StdlibBsearch, "void *bsearch(void *,void *,int,int,int (*)());" }, */ /* {StdlibBsearch, "void *bsearch(void *,void *,int,int,int (*)());"}, */
/* { StdlibQsort, "void *qsort(void *,int,int,int (*)());" }, */ /* {StdlibQsort, "void *qsort(void *,int,int,int (*)());"}, */
{ StdlibAbs, "int abs(int);" }, {StdlibAbs, "int abs(int);"},
{ StdlibLabs, "int labs(int);" }, {StdlibLabs, "int labs(int);"},
#if 0 #if 0
{ StdlibDiv, "div_t div(int);" }, {StdlibDiv, "div_t div(int);"},
{ StdlibLdiv, "ldiv_t ldiv(int);" }, {StdlibLdiv, "ldiv_t ldiv(int);"},
#endif #endif
{ NULL, NULL } {NULL, NULL}
}; };
/* creates various system-dependent definitions */ /* creates various system-dependent definitions */

View file

@ -142,36 +142,36 @@ void StringStrtok_r(struct ParseState *Parser, struct Value *ReturnValue, struct
struct LibraryFunction StringFunctions[] = struct LibraryFunction StringFunctions[] =
{ {
#ifndef WIN32 #ifndef WIN32
{ StringIndex, "char *index(char *,int);" }, {StringIndex, "char *index(char *,int);"},
{ StringRindex, "char *rindex(char *,int);" }, {StringRindex, "char *rindex(char *,int);"},
#endif #endif
{ StringMemcpy, "void *memcpy(void *,void *,int);" }, {StringMemcpy, "void *memcpy(void *,void *,int);"},
{ StringMemmove, "void *memmove(void *,void *,int);" }, {StringMemmove, "void *memmove(void *,void *,int);"},
{ StringMemchr, "void *memchr(char *,int,int);" }, {StringMemchr, "void *memchr(char *,int,int);"},
{ StringMemcmp, "int memcmp(void *,void *,int);" }, {StringMemcmp, "int memcmp(void *,void *,int);"},
{ StringMemset, "void *memset(void *,int,int);" }, {StringMemset, "void *memset(void *,int,int);"},
{ StringStrcat, "char *strcat(char *,char *);" }, {StringStrcat, "char *strcat(char *,char *);"},
{ StringStrncat, "char *strncat(char *,char *,int);" }, {StringStrncat, "char *strncat(char *,char *,int);"},
{ StringStrchr, "char *strchr(char *,int);" }, {StringStrchr, "char *strchr(char *,int);"},
{ StringStrrchr, "char *strrchr(char *,int);" }, {StringStrrchr, "char *strrchr(char *,int);"},
{ StringStrcmp, "int strcmp(char *,char *);" }, {StringStrcmp, "int strcmp(char *,char *);"},
{ StringStrncmp, "int strncmp(char *,char *,int);" }, {StringStrncmp, "int strncmp(char *,char *,int);"},
{ StringStrcoll, "int strcoll(char *,char *);" }, {StringStrcoll, "int strcoll(char *,char *);"},
{ StringStrcpy, "char *strcpy(char *,char *);" }, {StringStrcpy, "char *strcpy(char *,char *);"},
{ StringStrncpy, "char *strncpy(char *,char *,int);" }, {StringStrncpy, "char *strncpy(char *,char *,int);"},
{ StringStrerror, "char *strerror(int);" }, {StringStrerror,"char *strerror(int);"},
{ StringStrlen, "int strlen(char *);" }, {StringStrlen, "int strlen(char *);"},
{ StringStrspn, "int strspn(char *,char *);" }, {StringStrspn, "int strspn(char *,char *);"},
{ StringStrcspn, "int strcspn(char *,char *);" }, {StringStrcspn, "int strcspn(char *,char *);"},
{ StringStrpbrk, "char *strpbrk(char *,char *);" }, {StringStrpbrk, "char *strpbrk(char *,char *);"},
{ StringStrstr, "char *strstr(char *,char *);" }, {StringStrstr, "char *strstr(char *,char *);"},
{ StringStrtok, "char *strtok(char *,char *);" }, {StringStrtok, "char *strtok(char *,char *);"},
{ StringStrxfrm, "int strxfrm(char *,char *,int);" }, {StringStrxfrm, "int strxfrm(char *,char *,int);"},
#ifndef WIN32 #ifndef WIN32
{ StringStrdup, "char *strdup(char *);" }, {StringStrdup, "char *strdup(char *);"},
{ StringStrtok_r, "char *strtok_r(char *,char *,char **);" }, {StringStrtok_r,"char *strtok_r(char *,char *,char **);"},
#endif #endif
{ NULL, NULL } {NULL, NULL }
}; };
/* creates various system-dependent definitions */ /* creates various system-dependent definitions */

View file

@ -53,7 +53,7 @@ void DebugSetBreakpoint(struct ParseState *Parser)
/* add it to the table */ /* add it to the table */
struct TableEntry *NewEntry = HeapAllocMem(pc, sizeof(struct TableEntry)); struct TableEntry *NewEntry = HeapAllocMem(pc, sizeof(struct TableEntry));
if (NewEntry == NULL) if (NewEntry == NULL)
ProgramFailNoParser(pc, "out of memory"); ProgramFailNoParser(pc, "(DebugSetBreakpoint) out of memory");
NewEntry->p.b.FileName = Parser->FileName; NewEntry->p.b.FileName = Parser->FileName;
NewEntry->p.b.Line = Parser->Line; NewEntry->p.b.Line = Parser->Line;

View file

@ -14,14 +14,6 @@
#define DEEP_PRECEDENCE (BRACKET_PRECEDENCE*1000) #define DEEP_PRECEDENCE (BRACKET_PRECEDENCE*1000)
#ifdef DEBUG_EXPRESSIONS
#define debugf printf
#else
void debugf(char *Format, ...)
{
}
#endif
/* local prototypes */ /* local prototypes */
enum OperatorOrder enum OperatorOrder
{ {
@ -436,19 +428,19 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct
if (DestValue->Typ->FromType->Base == TypeChar && SourceValue->Typ->Base == TypePointer && SourceValue->Typ->FromType->Base == TypeChar) { if (DestValue->Typ->FromType->Base == TypeChar && SourceValue->Typ->Base == TypePointer && SourceValue->Typ->FromType->Base == TypeChar) {
if (DestValue->Typ->ArraySize == 0) { /* char x[] = "abcd", x is unsized */ if (DestValue->Typ->ArraySize == 0) { /* char x[] = "abcd", x is unsized */
int Size = strlen(SourceValue->Val->Pointer) + 1; int Size = strlen(SourceValue->Val->Pointer) + 1;
#ifdef DEBUG_ARRAY_INITIALIZER #ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS; PRINT_SOURCE_POS;
fprintf(stderr, "str size: %d\n", Size); fprintf(stderr, "str size: %d\n", Size);
#endif #endif
DestValue->Typ = TypeGetMatching(Parser->pc, Parser, DestValue->Typ->FromType, DestValue->Typ->Base, Size, DestValue->Typ->Identifier, TRUE); DestValue->Typ = TypeGetMatching(Parser->pc, Parser, DestValue->Typ->FromType, DestValue->Typ->Base, Size, DestValue->Typ->Identifier, TRUE);
VariableRealloc(Parser, DestValue, TypeSizeValue(DestValue, FALSE)); VariableRealloc(Parser, DestValue, TypeSizeValue(DestValue, FALSE));
} }
/* else, it's char x[10] = "abcd" */ /* else, it's char x[10] = "abcd" */
#ifdef DEBUG_ARRAY_INITIALIZER #ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS; PRINT_SOURCE_POS;
fprintf(stderr, "char[%d] from char* (len=%d)\n", DestValue->Typ->ArraySize, strlen(SourceValue->Val->Pointer)); fprintf(stderr, "char[%d] from char* (len=%d)\n", DestValue->Typ->ArraySize, strlen(SourceValue->Val->Pointer));
#endif #endif
memcpy((void *)DestValue->Val, SourceValue->Val->Pointer, TypeSizeValue(DestValue, FALSE)); memcpy((void *)DestValue->Val, SourceValue->Val->Pointer, TypeSizeValue(DestValue, FALSE));
break; break;
} }
@ -509,7 +501,10 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack
struct Value *Result; struct Value *Result;
union AnyValue *ValPtr; union AnyValue *ValPtr;
debugf("ExpressionPrefixOperator()\n"); #ifdef DEBUG_EXPRESSIONS
printf("ExpressionPrefixOperator()\n");
#endif
switch (Op) { switch (Op) {
case TokenAmpersand: case TokenAmpersand:
if (!TopValue->IsLValue) if (!TopValue->IsLValue)
@ -597,7 +592,9 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack
/* evaluate a postfix operator */ /* evaluate a postfix operator */
void ExpressionPostfixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue) void ExpressionPostfixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue)
{ {
debugf("ExpressionPostfixOperator()\n"); #ifdef DEBUG_EXPRESSIONS
printf("ExpressionPostfixOperator()\n");
#endif
#ifndef NO_FP #ifndef NO_FP
if (TopValue->Typ == &Parser->pc->FPType) { if (TopValue->Typ == &Parser->pc->FPType) {
/* floating point prefix arithmetic */ /* floating point prefix arithmetic */
@ -657,7 +654,10 @@ void ExpressionInfixOperator(struct ParseState *Parser, struct ExpressionStack *
struct Value *StackValue; struct Value *StackValue;
void *Pointer; void *Pointer;
debugf("ExpressionInfixOperator()\n"); #ifdef DEBUG_EXPRESSIONS
printf("ExpressionInfixOperator()\n");
#endif
if (BottomValue == NULL || TopValue == NULL) if (BottomValue == NULL || TopValue == NULL)
ProgramFail(Parser, "invalid expression"); ProgramFail(Parser, "invalid expression");
@ -846,8 +846,8 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
struct ExpressionStack *TopStackNode = *StackTop; struct ExpressionStack *TopStackNode = *StackTop;
struct ExpressionStack *TopOperatorNode; struct ExpressionStack *TopOperatorNode;
debugf("ExpressionStackCollapse(%d):\n", Precedence);
#ifdef DEBUG_EXPRESSIONS #ifdef DEBUG_EXPRESSIONS
printf("ExpressionStackCollapse(%d):\n", Precedence);
ExpressionStackShow(Parser->pc, *StackTop); ExpressionStackShow(Parser->pc, *StackTop);
#endif #endif
while (TopStackNode != NULL && TopStackNode->Next != NULL && FoundPrecedence >= Precedence) { while (TopStackNode != NULL && TopStackNode->Next != NULL && FoundPrecedence >= Precedence) {
@ -865,7 +865,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
switch (TopOperatorNode->Order) { switch (TopOperatorNode->Order) {
case OrderPrefix: case OrderPrefix:
/* prefix evaluation */ /* prefix evaluation */
debugf("prefix evaluation\n"); #ifdef DEBUG_EXPRESSIONS
printf("prefix evaluation\n");
#endif
TopValue = TopStackNode->Val; TopValue = TopStackNode->Val;
/* pop the value and then the prefix operator - assume they'll still be there until we're done */ /* pop the value and then the prefix operator - assume they'll still be there until we're done */
@ -885,7 +887,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
case OrderPostfix: case OrderPostfix:
/* postfix evaluation */ /* postfix evaluation */
debugf("postfix evaluation\n"); #ifdef DEBUG_EXPRESSIONS
printf("postfix evaluation\n");
#endif
TopValue = TopStackNode->Next->Val; TopValue = TopStackNode->Next->Val;
/* pop the postfix operator and then the value - assume they'll still be there until we're done */ /* pop the postfix operator and then the value - assume they'll still be there until we're done */
@ -905,7 +909,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
case OrderInfix: case OrderInfix:
/* infix evaluation */ /* infix evaluation */
debugf("infix evaluation\n"); #ifdef DEBUG_EXPRESSIONS
printf("infix evaluation\n");
#endif
TopValue = TopStackNode->Val; TopValue = TopStackNode->Val;
if (TopValue != NULL) { if (TopValue != NULL) {
BottomValue = TopOperatorNode->Next->Val; BottomValue = TopOperatorNode->Next->Val;
@ -944,8 +950,8 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
#endif #endif
TopStackNode = *StackTop; TopStackNode = *StackTop;
} }
debugf("ExpressionStackCollapse() finished\n");
#ifdef DEBUG_EXPRESSIONS #ifdef DEBUG_EXPRESSIONS
printf("ExpressionStackCollapse() finished\n");
ExpressionStackShow(Parser->pc, *StackTop); ExpressionStackShow(Parser->pc, *StackTop);
#endif #endif
} }
@ -959,7 +965,9 @@ void ExpressionStackPushOperator(struct ParseState *Parser, struct ExpressionSta
StackNode->Op = Token; StackNode->Op = Token;
StackNode->Precedence = Precedence; StackNode->Precedence = Precedence;
*StackTop = StackNode; *StackTop = StackNode;
debugf("ExpressionStackPushOperator()\n"); #ifdef DEBUG_EXPRESSIONS
printf("ExpressionStackPushOperator()\n");
#endif
#ifdef FANCY_ERROR_MESSAGES #ifdef FANCY_ERROR_MESSAGES
StackNode->Line = Parser->Line; StackNode->Line = Parser->Line;
StackNode->CharacterPos = Parser->CharacterPos; StackNode->CharacterPos = Parser->CharacterPos;
@ -1020,7 +1028,10 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
struct ExpressionStack *StackTop = NULL; struct ExpressionStack *StackTop = NULL;
int TernaryDepth = 0; int TernaryDepth = 0;
debugf("ExpressionParse():\n"); #ifdef DEBUG_EXPRESSIONS
printf("ExpressionParse():\n");
#endif
do { do {
struct ParseState PreState; struct ParseState PreState;
enum LexToken Token; enum LexToken Token;
@ -1236,8 +1247,8 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
HeapPopStack(Parser->pc, StackTop->Val, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(StackTop->Val)); HeapPopStack(Parser->pc, StackTop->Val, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(StackTop->Val));
} }
debugf("ExpressionParse() done\n\n");
#ifdef DEBUG_EXPRESSIONS #ifdef DEBUG_EXPRESSIONS
printf("ExpressionParse() done\n\n");
ExpressionStackShow(Parser->pc, StackTop); ExpressionStackShow(Parser->pc, StackTop);
#endif #endif
return StackTop != NULL; return StackTop != NULL;
@ -1264,7 +1275,7 @@ void ExpressionParseMacroCall(struct ParseState *Parser, struct ExpressionStack
HeapPushStackFrame(Parser->pc); HeapPushStackFrame(Parser->pc);
ParamArray = HeapAllocStack(Parser->pc, sizeof(struct Value *) * MDef->NumParams); ParamArray = HeapAllocStack(Parser->pc, sizeof(struct Value *) * MDef->NumParams);
if (ParamArray == NULL) if (ParamArray == NULL)
ProgramFail(Parser, "out of memory"); ProgramFail(Parser, "(ExpressionParseMacroCall) out of memory");
} else } else
ExpressionPushInt(Parser, StackTop, 0); ExpressionPushInt(Parser, StackTop, 0);
@ -1348,7 +1359,7 @@ void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionSta
HeapPushStackFrame(Parser->pc); HeapPushStackFrame(Parser->pc);
ParamArray = HeapAllocStack(Parser->pc, sizeof(struct Value *) * FuncValue->Val->FuncDef.NumParams); ParamArray = HeapAllocStack(Parser->pc, sizeof(struct Value *) * FuncValue->Val->FuncDef.NumParams);
if (ParamArray == NULL) if (ParamArray == NULL)
ProgramFail(Parser, "out of memory"); ProgramFail(Parser, "(ExpressionParseFunctionCall) out of memory");
} else { } else {
ExpressionPushInt(Parser, StackTop, 0); ExpressionPushInt(Parser, StackTop, 0);
Parser->Mode = RunModeSkip; Parser->Mode = RunModeSkip;
@ -1391,9 +1402,9 @@ void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionSta
if (FuncValue->Val->FuncDef.Intrinsic == NULL) { if (FuncValue->Val->FuncDef.Intrinsic == NULL) {
/* run a user-defined function */ /* run a user-defined function */
struct ParseState FuncParser;
int Count; int Count;
int OldScopeID = Parser->ScopeID; int OldScopeID = Parser->ScopeID;
struct ParseState FuncParser;
if (FuncValue->Val->FuncDef.Body.Pos == NULL) if (FuncValue->Val->FuncDef.Body.Pos == NULL)
ProgramFail(Parser, "ExpressionParseFunctionCall FuncName: '%s' is undefined", FuncName); ProgramFail(Parser, "ExpressionParseFunctionCall FuncName: '%s' is undefined", FuncName);

18
heap.c
View file

@ -50,15 +50,15 @@ void HeapCleanup(Picoc *pc)
void *HeapAllocStack(Picoc *pc, int Size) void *HeapAllocStack(Picoc *pc, int Size)
{ {
char *NewMem = pc->HeapStackTop; char *NewMem = pc->HeapStackTop;
char *NewTop = (char *)pc->HeapStackTop + MEM_ALIGN(Size); char *NewTop = (char*)pc->HeapStackTop + MEM_ALIGN(Size);
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("HeapAllocStack(%ld) at 0x%lx\n", (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop); printf("HeapAllocStack(%ld) at 0x%lx\n", (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop);
#endif #endif
if (NewTop > (char *)pc->HeapBottom) if (NewTop > (char*)pc->HeapBottom)
return NULL; return NULL;
pc->HeapStackTop = (void *)NewTop; pc->HeapStackTop = (void*)NewTop;
memset((void *)NewMem, '\0', Size); memset((void*)NewMem, '\0', Size);
return NewMem; return NewMem;
} }
@ -68,21 +68,21 @@ void HeapUnpopStack(Picoc *pc, int Size)
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("HeapUnpopStack(%ld) at 0x%lx\n", (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop); printf("HeapUnpopStack(%ld) at 0x%lx\n", (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop);
#endif #endif
pc->HeapStackTop = (void *)((char *)pc->HeapStackTop + MEM_ALIGN(Size)); pc->HeapStackTop = (void*)((char*)pc->HeapStackTop + MEM_ALIGN(Size));
} }
/* free some space at the top of the stack */ /* free some space at the top of the stack */
int HeapPopStack(Picoc *pc, void *Addr, int Size) int HeapPopStack(Picoc *pc, void *Addr, int Size)
{ {
int ToLose = MEM_ALIGN(Size); int ToLose = MEM_ALIGN(Size);
if (ToLose > ((char *)pc->HeapStackTop - (char *)&(pc->HeapMemory)[0])) if (ToLose > ((char*)pc->HeapStackTop - (char*)&(pc->HeapMemory)[0]))
return FALSE; return FALSE;
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("HeapPopStack(0x%lx, %ld) back to 0x%lx\n", (unsigned long)Addr, printf("HeapPopStack(0x%lx, %ld) back to 0x%lx\n", (unsigned long)Addr,
(unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop - ToLose); (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop - ToLose);
#endif #endif
pc->HeapStackTop = (void *)((char *)pc->HeapStackTop - ToLose); pc->HeapStackTop = (void*)((char*)pc->HeapStackTop - ToLose);
assert(Addr == NULL || pc->HeapStackTop == Addr); assert(Addr == NULL || pc->HeapStackTop == Addr);
return TRUE; return TRUE;
@ -96,7 +96,7 @@ void HeapPushStackFrame(Picoc *pc)
#endif #endif
*(void **)pc->HeapStackTop = pc->StackFrame; *(void **)pc->HeapStackTop = pc->StackFrame;
pc->StackFrame = pc->HeapStackTop; pc->StackFrame = pc->HeapStackTop;
pc->HeapStackTop = (void *)((char *)pc->HeapStackTop + MEM_ALIGN(sizeof(ALIGN_TYPE))); pc->HeapStackTop = (void*)((char*)pc->HeapStackTop + MEM_ALIGN(sizeof(ALIGN_TYPE)));
} }
/* pop the current stack frame, freeing all memory in the frame. can return NULL */ /* pop the current stack frame, freeing all memory in the frame. can return NULL */
@ -104,7 +104,7 @@ int HeapPopStackFrame(Picoc *pc)
{ {
if (*(void **)pc->StackFrame != NULL) { if (*(void **)pc->StackFrame != NULL) {
pc->HeapStackTop = pc->StackFrame; pc->HeapStackTop = pc->StackFrame;
pc->StackFrame = *(void **)pc->StackFrame; pc->StackFrame = *(void**)pc->StackFrame;
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf("Popping stack frame back to 0x%lx\n", (unsigned long)pc->HeapStackTop); printf("Popping stack frame back to 0x%lx\n", (unsigned long)pc->HeapStackTop);
#endif #endif

View file

@ -81,7 +81,7 @@ void IncludeFile(Picoc *pc, char *FileName)
/* set up the library functions */ /* set up the library functions */
if (LInclude->FuncList != NULL) if (LInclude->FuncList != NULL)
LibraryAdd(pc, &pc->GlobalTable, FileName, LInclude->FuncList); LibraryAdd(pc, &pc->GlobalTable, LInclude->FuncList);
} }
return; return;

View file

@ -404,9 +404,9 @@ struct Picoc_Struct
struct ValueType UnsignedShortType; struct ValueType UnsignedShortType;
struct ValueType UnsignedLongType; struct ValueType UnsignedLongType;
struct ValueType UnsignedCharType; struct ValueType UnsignedCharType;
#ifndef NO_FP #ifndef NO_FP
struct ValueType FPType; struct ValueType FPType;
#endif #endif
struct ValueType VoidType; struct ValueType VoidType;
struct ValueType TypeType; struct ValueType TypeType;
struct ValueType FunctionType; struct ValueType FunctionType;
@ -539,13 +539,13 @@ extern void VariableStackFramePop(struct ParseState *Parser);
extern struct Value *VariableStringLiteralGet(Picoc *pc, char *Ident); extern struct Value *VariableStringLiteralGet(Picoc *pc, char *Ident);
extern void VariableStringLiteralDefine(Picoc *pc, char *Ident, struct Value *Val); extern void VariableStringLiteralDefine(Picoc *pc, char *Ident, struct Value *Val);
extern void *VariableDereferencePointer(struct ParseState *Parser, struct Value *PointerValue, struct Value **DerefVal, int *DerefOffset, struct ValueType **DerefType, int *DerefIsLValue); extern void *VariableDereferencePointer(struct ParseState *Parser, struct Value *PointerValue, struct Value **DerefVal, int *DerefOffset, struct ValueType **DerefType, int *DerefIsLValue);
extern int VariableScopeBegin(struct ParseState * Parser, int* PrevScopeID); extern int VariableScopeBegin(struct ParseState *Parser, int *PrevScopeID);
extern void VariableScopeEnd(struct ParseState * Parser, int ScopeID, int PrevScopeID); extern void VariableScopeEnd(struct ParseState *Parser, int ScopeID, int PrevScopeID);
/* clibrary.c */ /* clibrary.c */
extern void BasicIOInit(Picoc *pc); extern void BasicIOInit(Picoc *pc);
extern void LibraryInit(Picoc *pc); extern void LibraryInit(Picoc *pc);
extern void LibraryAdd(Picoc *pc, struct Table *GlobalTable, const char *LibraryName, struct LibraryFunction *FuncList); extern void LibraryAdd(Picoc *pc, struct Table *GlobalTable, struct LibraryFunction *FuncList);
extern void CLibraryInit(Picoc *pc); extern void CLibraryInit(Picoc *pc);
extern void PrintCh(char OutCh, IOFILE *Stream); extern void PrintCh(char OutCh, IOFILE *Stream);
extern void PrintSimpleInt(long Num, IOFILE *Stream); extern void PrintSimpleInt(long Num, IOFILE *Stream);

21
lex.c
View file

@ -31,13 +31,14 @@ struct ReservedWord
static struct ReservedWord ReservedWords[] = static struct ReservedWord ReservedWords[] =
{ {
{"#define", TokenHashDefine}, /* wrf, when optimizations are set escaping certain chars is required or they disappear */
{"#else", TokenHashElse}, {"\#define", TokenHashDefine},
{"#endif", TokenHashEndif}, {"\#else", TokenHashElse},
{"#if", TokenHashIf}, {"\#endif", TokenHashEndif},
{"#ifdef", TokenHashIfdef}, {"\#if", TokenHashIf},
{"#ifndef", TokenHashIfndef}, {"\#ifdef", TokenHashIfdef},
{"#include", TokenHashInclude}, {"\#ifndef", TokenHashIfndef},
{"\#include", TokenHashInclude},
{"auto", TokenAutoType}, {"auto", TokenAutoType},
{"break", TokenBreak}, {"break", TokenBreak},
{"case", TokenCase}, {"case", TokenCase},
@ -335,7 +336,7 @@ enum LexToken LexGetStringConstant(Picoc *pc, struct LexState *Lexer, struct Val
EscBuf = HeapAllocStack(pc, EndPos - StartPos); EscBuf = HeapAllocStack(pc, EndPos - StartPos);
if (EscBuf == NULL) if (EscBuf == NULL)
LexFail(pc, Lexer, "out of memory"); LexFail(pc, Lexer, "(LexGetStringConstant) out of memory");
for (EscBufPos = EscBuf, Lexer->Pos = StartPos; Lexer->Pos != EndPos;) for (EscBufPos = EscBuf, Lexer->Pos = StartPos; Lexer->Pos != EndPos;)
*EscBufPos++ = LexUnEscapeCharacter(&Lexer->Pos, EndPos); *EscBufPos++ = LexUnEscapeCharacter(&Lexer->Pos, EndPos);
@ -499,7 +500,7 @@ void *LexTokenise(Picoc *pc, struct LexState *Lexer, int *TokenLen)
int LastCharacterPos = 0; int LastCharacterPos = 0;
if (TokenSpace == NULL) if (TokenSpace == NULL)
LexFail(pc, Lexer, "out of memory"); LexFail(pc, Lexer, "(LexTokenise TokenSpace == NULL) out of memory");
do { do {
/* store the token at the end of the stack area */ /* store the token at the end of the stack area */
@ -530,7 +531,7 @@ void *LexTokenise(Picoc *pc, struct LexState *Lexer, int *TokenLen)
HeapMem = HeapAllocMem(pc, MemUsed); HeapMem = HeapAllocMem(pc, MemUsed);
if (HeapMem == NULL) if (HeapMem == NULL)
LexFail(pc, Lexer, "out of memory"); LexFail(pc, Lexer, "(LexTokenise HeapMem == NULL) out of memory");
assert(ReserveSpace >= MemUsed); assert(ReserveSpace >= MemUsed);
memcpy(HeapMem, TokenSpace, MemUsed); memcpy(HeapMem, TokenSpace, MemUsed);

17
parse.c
View file

@ -3,6 +3,13 @@
#include "picoc.h" #include "picoc.h"
#include "interpreter.h" #include "interpreter.h"
#ifdef DEBUGGER
static int gEnableDebugger = TRUE;
#else
static int gEnableDebugger = FALSE;
#endif
/* deallocate any memory */ /* deallocate any memory */
void ParseCleanup(Picoc *pc) void ParseCleanup(Picoc *pc)
{ {
@ -410,7 +417,8 @@ void ParseFor(struct ParseState *Parser)
enum RunMode OldMode = Parser->Mode; enum RunMode OldMode = Parser->Mode;
int PrevScopeID = 0, ScopeID = VariableScopeBegin(Parser, &PrevScopeID); int PrevScopeID = 0;
int ScopeID = VariableScopeBegin(Parser, &PrevScopeID);
if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket) if (LexGetToken(Parser, NULL, TRUE) != TokenOpenBracket)
ProgramFail(Parser, "'(' expected"); ProgramFail(Parser, "'(' expected");
@ -472,7 +480,8 @@ void ParseFor(struct ParseState *Parser)
/* parse a block of code and return what mode it returned in */ /* parse a block of code and return what mode it returned in */
enum RunMode ParseBlock(struct ParseState *Parser, int AbsorbOpenBrace, int Condition) enum RunMode ParseBlock(struct ParseState *Parser, int AbsorbOpenBrace, int Condition)
{ {
int PrevScopeID = 0, ScopeID = VariableScopeBegin(Parser, &PrevScopeID); int PrevScopeID = 0;
int ScopeID = VariableScopeBegin(Parser, &PrevScopeID);
if (AbsorbOpenBrace && LexGetToken(Parser, NULL, TRUE) != TokenLeftBrace) if (AbsorbOpenBrace && LexGetToken(Parser, NULL, TRUE) != TokenLeftBrace)
ProgramFail(Parser, "'{' expected"); ProgramFail(Parser, "'{' expected");
@ -818,7 +827,7 @@ void PicocParse(Picoc *pc, const char *FileName, const char *Source, int SourceL
if (!CleanupNow) { if (!CleanupNow) {
NewCleanupNode = HeapAllocMem(pc, sizeof(struct CleanupTokenNode)); NewCleanupNode = HeapAllocMem(pc, sizeof(struct CleanupTokenNode));
if (NewCleanupNode == NULL) if (NewCleanupNode == NULL)
ProgramFailNoParser(pc, "out of memory"); ProgramFailNoParser(pc, "(PicocParse) out of memory");
NewCleanupNode->Tokens = Tokens; NewCleanupNode->Tokens = Tokens;
if (CleanupSource) if (CleanupSource)
@ -872,5 +881,5 @@ void PicocParseInteractiveNoStartPrompt(Picoc *pc, int EnableDebugger)
void PicocParseInteractive(Picoc *pc) void PicocParseInteractive(Picoc *pc)
{ {
PlatformPrintf(pc->CStdOut, INTERACTIVE_PROMPT_START); PlatformPrintf(pc->CStdOut, INTERACTIVE_PROMPT_START);
PicocParseInteractiveNoStartPrompt(pc, TRUE); PicocParseInteractiveNoStartPrompt(pc, gEnableDebugger);
} }

View file

@ -11,7 +11,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define PICOC_STACK_SIZE (128*1024) /* space for the the stack */ /* Override via STACKSIZE environment variable */
#define PICOC_STACK_SIZE (1024*1024) /* space for the the stack */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {

View file

@ -33,7 +33,7 @@ void TableInitTable(struct Table *Tbl, struct TableEntry **HashTable, int Size,
Tbl->Size = Size; Tbl->Size = Size;
Tbl->OnHeap = OnHeap; Tbl->OnHeap = OnHeap;
Tbl->HashTable = HashTable; Tbl->HashTable = HashTable;
memset((void *)HashTable, '\0', sizeof(struct TableEntry *) * Size); memset((void*)HashTable, '\0', sizeof(struct TableEntry*) * Size);
} }
/* check a hash table entry for a key */ /* check a hash table entry for a key */
@ -41,7 +41,6 @@ static struct TableEntry *TableSearch(struct Table *Tbl, const char *Key, int *A
{ {
struct TableEntry *Entry; struct TableEntry *Entry;
int HashValue = ((unsigned long)Key) % Tbl->Size; /* shared strings have unique addresses so we don't need to hash them */ int HashValue = ((unsigned long)Key) % Tbl->Size; /* shared strings have unique addresses so we don't need to hash them */
for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next) { for (Entry = Tbl->HashTable[HashValue]; Entry != NULL; Entry = Entry->Next) {
if (Entry->p.v.Key == Key) if (Entry->p.v.Key == Key)
return Entry; /* found */ return Entry; /* found */
@ -139,7 +138,7 @@ char *TableSetIdentifier(Picoc *pc, struct Table *Tbl, const char *Ident, int Id
else { /* add it to the table - we economise by not allocating the whole structure here */ else { /* add it to the table - we economise by not allocating the whole structure here */
struct TableEntry *NewEntry = HeapAllocMem(pc, sizeof(struct TableEntry) - sizeof(union TableEntryPayload) + IdentLen + 1); struct TableEntry *NewEntry = HeapAllocMem(pc, sizeof(struct TableEntry) - sizeof(union TableEntryPayload) + IdentLen + 1);
if (NewEntry == NULL) if (NewEntry == NULL)
ProgramFailNoParser(pc, "out of memory"); ProgramFailNoParser(pc, "(TableSetIdentifier) out of memory");
strncpy((char *)&NewEntry->p.Key[0], (char *)Ident, IdentLen); strncpy((char *)&NewEntry->p.Key[0], (char *)Ident, IdentLen);
NewEntry->p.Key[IdentLen] = '\0'; NewEntry->p.Key[IdentLen] = '\0';

View file

@ -72,7 +72,7 @@ void *VariableAlloc(Picoc *pc, struct ParseState *Parser, int Size, int OnHeap)
NewValue = HeapAllocStack(pc, Size); NewValue = HeapAllocStack(pc, Size);
if (NewValue == NULL) if (NewValue == NULL)
ProgramFail(Parser, "out of memory"); ProgramFail(Parser, "(VariableAlloc) out of memory");
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
if (!OnHeap) if (!OnHeap)
@ -95,7 +95,7 @@ struct Value *VariableAllocValueAndData(Picoc *pc, struct ParseState *Parser, in
if (Parser) if (Parser)
NewValue->ScopeID = Parser->ScopeID; NewValue->ScopeID = Parser->ScopeID;
NewValue->OutOfScope = 0; NewValue->OutOfScope = FALSE;
return NewValue; return NewValue;
} }
@ -159,19 +159,20 @@ void VariableRealloc(struct ParseState *Parser, struct Value *FromValue, int New
FromValue->AnyValOnHeap = TRUE; FromValue->AnyValOnHeap = TRUE;
} }
int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID) int VariableScopeBegin(struct ParseState *Parser, int* OldScopeID)
{ {
int Count;
Picoc *pc = Parser->pc;
struct TableEntry *Entry; struct TableEntry *Entry;
struct TableEntry *NextEntry; struct TableEntry *NextEntry;
Picoc *pc = Parser->pc;
int Count;
#ifdef VAR_SCOPE_DEBUG #ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0; int FirstPrint = 0;
#endif #endif
struct Table *HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable; struct Table *HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
if (Parser->ScopeID == -1) return -1; if (Parser->ScopeID == -1)
return -1;
/* XXX dumb hash, let's hope for no collisions... */ /* XXX dumb hash, let's hope for no collisions... */
*OldScopeID = Parser->ScopeID; *OldScopeID = Parser->ScopeID;
@ -182,7 +183,7 @@ int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
for (Count = 0; Count < HashTable->Size; Count++) { for (Count = 0; Count < HashTable->Size; Count++) {
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry) { for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry) {
NextEntry = Entry->Next; NextEntry = Entry->Next;
if (Entry->p.v.Val->ScopeID == Parser->ScopeID && Entry->p.v.Val->OutOfScope) { if (Entry->p.v.Val->ScopeID == Parser->ScopeID && Entry->p.v.Val->OutOfScope == TRUE) {
Entry->p.v.Val->OutOfScope = FALSE; Entry->p.v.Val->OutOfScope = FALSE;
Entry->p.v.Key = (char*)((intptr_t)Entry->p.v.Key & ~1); Entry->p.v.Key = (char*)((intptr_t)Entry->p.v.Key & ~1);
#ifdef VAR_SCOPE_DEBUG #ifdef VAR_SCOPE_DEBUG
@ -197,24 +198,26 @@ int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
return Parser->ScopeID; return Parser->ScopeID;
} }
void VariableScopeEnd(struct ParseState * Parser, int ScopeID, int PrevScopeID) void VariableScopeEnd(struct ParseState *Parser, int ScopeID, int PrevScopeID)
{ {
struct TableEntry *Entry;
struct TableEntry *NextEntry;
Picoc * pc = Parser->pc;
int Count; int Count;
Picoc *pc = Parser->pc;
struct TableEntry *Entry;
struct TableEntry *NextEntry = NULL;
#ifdef VAR_SCOPE_DEBUG #ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0; int FirstPrint = 0;
#endif #endif
struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable; struct Table *HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
if (ScopeID == -1) return; if (ScopeID == -1)
return;
for (Count = 0; Count < HashTable->Size; Count++) { for (Count = 0; Count < HashTable->Size; Count++) {
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry) { for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry) {
NextEntry = Entry->Next; NextEntry = Entry->Next;
if (Entry->p.v.Val->ScopeID == ScopeID && !Entry->p.v.Val->OutOfScope) { if ((Entry->p.v.Val->ScopeID == ScopeID) && (Entry->p.v.Val->OutOfScope == FALSE)) {
#ifdef VAR_SCOPE_DEBUG #ifdef VAR_SCOPE_DEBUG
if (!FirstPrint) { if (!FirstPrint) {
PRINT_SOURCE_POS; PRINT_SOURCE_POS;
@ -239,7 +242,7 @@ int VariableDefinedAndOutOfScope(Picoc * pc, const char* Ident)
struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable; struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
for (Count = 0; Count < HashTable->Size; Count++) { for (Count = 0; Count < HashTable->Size; Count++) {
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = Entry->Next) { for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = Entry->Next) {
if (Entry->p.v.Val->OutOfScope && (char*)((intptr_t)Entry->p.v.Key & ~1) == Ident) if (Entry->p.v.Val->OutOfScope == TRUE && (char*)((intptr_t)Entry->p.v.Key & ~1) == Ident)
return TRUE; return TRUE;
} }
} }
@ -397,7 +400,7 @@ void VariableStackFrameAdd(struct ParseState *Parser, const char *FuncName, int
HeapPushStackFrame(Parser->pc); HeapPushStackFrame(Parser->pc);
NewFrame = HeapAllocStack(Parser->pc, sizeof(struct StackFrame) + sizeof(struct Value *) * NumParams); NewFrame = HeapAllocStack(Parser->pc, sizeof(struct StackFrame) + sizeof(struct Value *) * NumParams);
if (NewFrame == NULL) if (NewFrame == NULL)
ProgramFail(Parser, "out of memory"); ProgramFail(Parser, "(VariableStackFrameAdd) out of memory");
ParserCopy(&NewFrame->ReturnParser, Parser); ParserCopy(&NewFrame->ReturnParser, Parser);
NewFrame->FuncName = FuncName; NewFrame->FuncName = FuncName;