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
# -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
TARGET = picoc
@ -21,6 +21,7 @@ $(TARGET): $(OBJS)
test: all
(cd tests; make test)
(cd tests; make csmith)
clean:
rm -f $(TARGET) $(OBJS) *~

View file

@ -27,7 +27,7 @@ void LibraryInit(Picoc *pc)
}
/* 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;
int Count;
@ -39,7 +39,7 @@ void LibraryAdd(Picoc *pc, struct Table *GlobalTable, const char *LibraryName, s
/* read all the library definitions */
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);
TypeParse(&Parser, &ReturnType, &Identifier, NULL);
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 */
struct LibraryFunction StdCtypeFunctions[] =
{
{ StdIsalnum, "int isalnum(int);" },
{ StdIsalpha, "int isalpha(int);" },
{ StdIsblank, "int isblank(int);" },
{ StdIscntrl, "int iscntrl(int);" },
{ StdIsdigit, "int isdigit(int);" },
{ StdIsgraph, "int isgraph(int);" },
{ StdIslower, "int islower(int);" },
{ StdIsprint, "int isprint(int);" },
{ StdIspunct, "int ispunct(int);" },
{ StdIsspace, "int isspace(int);" },
{ StdIsupper, "int isupper(int);" },
{ StdIsxdigit, "int isxdigit(int);" },
{ StdTolower, "int tolower(int);" },
{ StdToupper, "int toupper(int);" },
{ StdIsascii, "int isascii(int);" },
{ StdToascii, "int toascii(int);" },
{ NULL, NULL }
{StdIsalnum, "int isalnum(int);"},
{StdIsalpha, "int isalpha(int);"},
{StdIsblank, "int isblank(int);"},
{StdIscntrl, "int iscntrl(int);"},
{StdIsdigit, "int isdigit(int);"},
{StdIsgraph, "int isgraph(int);"},
{StdIslower, "int islower(int);"},
{StdIsprint, "int isprint(int);"},
{StdIspunct, "int ispunct(int);"},
{StdIsspace, "int isspace(int);"},
{StdIsupper, "int isupper(int);"},
{StdIsxdigit, "int isxdigit(int);"},
{StdTolower, "int tolower(int);"},
{StdToupper, "int toupper(int);"},
{StdIsascii, "int isascii(int);"},
{StdToascii, "int toascii(int);"},
{NULL, NULL }
};

View file

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

View file

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

View file

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

View file

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

View file

@ -88,23 +88,23 @@ typedef int clock_t;\
/* all string.h functions */
struct LibraryFunction StdTimeFunctions[] =
{
{StdAsctime, "char *asctime(struct tm *);"},
{StdClock, "time_t clock();"},
{StdCtime, "char *ctime(int *);"},
{StdAsctime, "char *asctime(struct tm *);"},
{StdClock, "time_t clock();"},
{StdCtime, "char *ctime(int *);"},
#ifndef NO_FP
{StdDifftime, "double difftime(int, int);"},
{StdDifftime, "double difftime(int, int);"},
#endif
{StdGmtime, "struct tm *gmtime(int *);"},
{StdLocaltime, "struct tm *localtime(int *);"},
{StdMktime, "int mktime(struct tm *ptm);"},
{StdTime, "int time(int *);"},
{StdStrftime, "int strftime(char *, int, char *, struct tm *);"},
{StdGmtime, "struct tm *gmtime(int *);"},
{StdLocaltime, "struct tm *localtime(int *);"},
{StdMktime, "int mktime(struct tm *ptm);"},
{StdTime, "int time(int *);"},
{StdStrftime, "int strftime(char *, int, char *, struct tm *);"},
#ifndef WIN32
{StdStrptime, "char *strptime(char *, char *, struct tm *);"},
{StdGmtime_r, "struct tm *gmtime_r(int *, struct tm *);"},
{StdTimegm, "int timegm(struct tm *);"},
{StdStrptime, "char *strptime(char *, char *, struct tm *);"},
{StdGmtime_r, "struct tm *gmtime_r(int *, struct tm *);"},
{StdTimegm, "int timegm(struct tm *);"},
#endif
{NULL, NULL}
{NULL, NULL}
};

View file

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

View file

@ -14,14 +14,6 @@
#define DEEP_PRECEDENCE (BRACKET_PRECEDENCE*1000)
#ifdef DEBUG_EXPRESSIONS
#define debugf printf
#else
void debugf(char *Format, ...)
{
}
#endif
/* local prototypes */
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->ArraySize == 0) { /* char x[] = "abcd", x is unsized */
int Size = strlen(SourceValue->Val->Pointer) + 1;
#ifdef DEBUG_ARRAY_INITIALIZER
#ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS;
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);
VariableRealloc(Parser, DestValue, TypeSizeValue(DestValue, FALSE));
}
/* else, it's char x[10] = "abcd" */
#ifdef DEBUG_ARRAY_INITIALIZER
#ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS;
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));
break;
}
@ -509,7 +501,10 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack
struct Value *Result;
union AnyValue *ValPtr;
debugf("ExpressionPrefixOperator()\n");
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionPrefixOperator()\n");
#endif
switch (Op) {
case TokenAmpersand:
if (!TopValue->IsLValue)
@ -597,7 +592,9 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack
/* evaluate a postfix operator */
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
if (TopValue->Typ == &Parser->pc->FPType) {
/* floating point prefix arithmetic */
@ -657,7 +654,10 @@ void ExpressionInfixOperator(struct ParseState *Parser, struct ExpressionStack *
struct Value *StackValue;
void *Pointer;
debugf("ExpressionInfixOperator()\n");
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionInfixOperator()\n");
#endif
if (BottomValue == NULL || TopValue == NULL)
ProgramFail(Parser, "invalid expression");
@ -846,8 +846,8 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
struct ExpressionStack *TopStackNode = *StackTop;
struct ExpressionStack *TopOperatorNode;
debugf("ExpressionStackCollapse(%d):\n", Precedence);
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionStackCollapse(%d):\n", Precedence);
ExpressionStackShow(Parser->pc, *StackTop);
#endif
while (TopStackNode != NULL && TopStackNode->Next != NULL && FoundPrecedence >= Precedence) {
@ -865,7 +865,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
switch (TopOperatorNode->Order) {
case OrderPrefix:
/* prefix evaluation */
debugf("prefix evaluation\n");
#ifdef DEBUG_EXPRESSIONS
printf("prefix evaluation\n");
#endif
TopValue = TopStackNode->Val;
/* 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:
/* postfix evaluation */
debugf("postfix evaluation\n");
#ifdef DEBUG_EXPRESSIONS
printf("postfix evaluation\n");
#endif
TopValue = TopStackNode->Next->Val;
/* 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:
/* infix evaluation */
debugf("infix evaluation\n");
#ifdef DEBUG_EXPRESSIONS
printf("infix evaluation\n");
#endif
TopValue = TopStackNode->Val;
if (TopValue != NULL) {
BottomValue = TopOperatorNode->Next->Val;
@ -944,8 +950,8 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
#endif
TopStackNode = *StackTop;
}
debugf("ExpressionStackCollapse() finished\n");
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionStackCollapse() finished\n");
ExpressionStackShow(Parser->pc, *StackTop);
#endif
}
@ -959,7 +965,9 @@ void ExpressionStackPushOperator(struct ParseState *Parser, struct ExpressionSta
StackNode->Op = Token;
StackNode->Precedence = Precedence;
*StackTop = StackNode;
debugf("ExpressionStackPushOperator()\n");
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionStackPushOperator()\n");
#endif
#ifdef FANCY_ERROR_MESSAGES
StackNode->Line = Parser->Line;
StackNode->CharacterPos = Parser->CharacterPos;
@ -1020,7 +1028,10 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
struct ExpressionStack *StackTop = NULL;
int TernaryDepth = 0;
debugf("ExpressionParse():\n");
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionParse():\n");
#endif
do {
struct ParseState PreState;
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));
}
debugf("ExpressionParse() done\n\n");
#ifdef DEBUG_EXPRESSIONS
printf("ExpressionParse() done\n\n");
ExpressionStackShow(Parser->pc, StackTop);
#endif
return StackTop != NULL;
@ -1264,7 +1275,7 @@ void ExpressionParseMacroCall(struct ParseState *Parser, struct ExpressionStack
HeapPushStackFrame(Parser->pc);
ParamArray = HeapAllocStack(Parser->pc, sizeof(struct Value *) * MDef->NumParams);
if (ParamArray == NULL)
ProgramFail(Parser, "out of memory");
ProgramFail(Parser, "(ExpressionParseMacroCall) out of memory");
} else
ExpressionPushInt(Parser, StackTop, 0);
@ -1348,7 +1359,7 @@ void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionSta
HeapPushStackFrame(Parser->pc);
ParamArray = HeapAllocStack(Parser->pc, sizeof(struct Value *) * FuncValue->Val->FuncDef.NumParams);
if (ParamArray == NULL)
ProgramFail(Parser, "out of memory");
ProgramFail(Parser, "(ExpressionParseFunctionCall) out of memory");
} else {
ExpressionPushInt(Parser, StackTop, 0);
Parser->Mode = RunModeSkip;
@ -1391,9 +1402,9 @@ void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionSta
if (FuncValue->Val->FuncDef.Intrinsic == NULL) {
/* run a user-defined function */
struct ParseState FuncParser;
int Count;
int OldScopeID = Parser->ScopeID;
struct ParseState FuncParser;
if (FuncValue->Val->FuncDef.Body.Pos == NULL)
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)
{
char *NewMem = pc->HeapStackTop;
char *NewTop = (char *)pc->HeapStackTop + MEM_ALIGN(Size);
char *NewTop = (char*)pc->HeapStackTop + MEM_ALIGN(Size);
#ifdef DEBUG_HEAP
printf("HeapAllocStack(%ld) at 0x%lx\n", (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop);
#endif
if (NewTop > (char *)pc->HeapBottom)
if (NewTop > (char*)pc->HeapBottom)
return NULL;
pc->HeapStackTop = (void *)NewTop;
memset((void *)NewMem, '\0', Size);
pc->HeapStackTop = (void*)NewTop;
memset((void*)NewMem, '\0', Size);
return NewMem;
}
@ -68,21 +68,21 @@ void HeapUnpopStack(Picoc *pc, int Size)
#ifdef DEBUG_HEAP
printf("HeapUnpopStack(%ld) at 0x%lx\n", (unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop);
#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 */
int HeapPopStack(Picoc *pc, void *Addr, int 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;
#ifdef DEBUG_HEAP
printf("HeapPopStack(0x%lx, %ld) back to 0x%lx\n", (unsigned long)Addr,
(unsigned long)MEM_ALIGN(Size), (unsigned long)pc->HeapStackTop - ToLose);
#endif
pc->HeapStackTop = (void *)((char *)pc->HeapStackTop - ToLose);
pc->HeapStackTop = (void*)((char*)pc->HeapStackTop - ToLose);
assert(Addr == NULL || pc->HeapStackTop == Addr);
return TRUE;
@ -96,7 +96,7 @@ void HeapPushStackFrame(Picoc *pc)
#endif
*(void **)pc->HeapStackTop = pc->StackFrame;
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 */
@ -104,7 +104,7 @@ int HeapPopStackFrame(Picoc *pc)
{
if (*(void **)pc->StackFrame != NULL) {
pc->HeapStackTop = pc->StackFrame;
pc->StackFrame = *(void **)pc->StackFrame;
pc->StackFrame = *(void**)pc->StackFrame;
#ifdef DEBUG_HEAP
printf("Popping stack frame back to 0x%lx\n", (unsigned long)pc->HeapStackTop);
#endif

View file

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

View file

@ -122,7 +122,7 @@ struct ParseState
short int HashIfLevel; /* how many "if"s we're nested down */
short int HashIfEvaluateToLevel; /* if we're not evaluating an if branch, what the last evaluated level was */
char DebugMode; /* debugging mode */
int ScopeID; /* for keeping track of local variables (free them after they go out of scope) */
int ScopeID; /* for keeping track of local variables (free them after they go out of scope) */
};
/* values */
@ -218,7 +218,7 @@ struct Value
char ValOnStack; /* the AnyValue is on the stack along with this Value */
char AnyValOnHeap; /* the AnyValue is separately allocated from the Value on the heap */
char IsLValue; /* is modifiable and is allocated somewhere we can usefully modify it */
int ScopeID; /* to know when it goes out of scope */
int ScopeID; /* to know when it goes out of scope */
char OutOfScope;
};
@ -404,9 +404,9 @@ struct Picoc_Struct
struct ValueType UnsignedShortType;
struct ValueType UnsignedLongType;
struct ValueType UnsignedCharType;
#ifndef NO_FP
#ifndef NO_FP
struct ValueType FPType;
#endif
#endif
struct ValueType VoidType;
struct ValueType TypeType;
struct ValueType FunctionType;
@ -539,13 +539,13 @@ extern void VariableStackFramePop(struct ParseState *Parser);
extern struct Value *VariableStringLiteralGet(Picoc *pc, char *Ident);
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 int VariableScopeBegin(struct ParseState * Parser, int* PrevScopeID);
extern void VariableScopeEnd(struct ParseState * Parser, int ScopeID, int PrevScopeID);
extern int VariableScopeBegin(struct ParseState *Parser, int *PrevScopeID);
extern void VariableScopeEnd(struct ParseState *Parser, int ScopeID, int PrevScopeID);
/* clibrary.c */
extern void BasicIOInit(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 PrintCh(char OutCh, IOFILE *Stream);
extern void PrintSimpleInt(long Num, IOFILE *Stream);

21
lex.c
View file

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

17
parse.c
View file

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

View file

@ -11,7 +11,8 @@
#include <stdio.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)
{

View file

@ -33,7 +33,7 @@ void TableInitTable(struct Table *Tbl, struct TableEntry **HashTable, int Size,
Tbl->Size = Size;
Tbl->OnHeap = OnHeap;
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 */
@ -41,7 +41,6 @@ static struct TableEntry *TableSearch(struct Table *Tbl, const char *Key, int *A
{
struct TableEntry *Entry;
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) {
if (Entry->p.v.Key == Key)
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 */
struct TableEntry *NewEntry = HeapAllocMem(pc, sizeof(struct TableEntry) - sizeof(union TableEntryPayload) + IdentLen + 1);
if (NewEntry == NULL)
ProgramFailNoParser(pc, "out of memory");
ProgramFailNoParser(pc, "(TableSetIdentifier) out of memory");
strncpy((char *)&NewEntry->p.Key[0], (char *)Ident, IdentLen);
NewEntry->p.Key[IdentLen] = '\0';

View file

@ -82,7 +82,7 @@ include csmith/Makefile
exit 1; \
fi; \
rm -f $*.output
all: test
test: $(TESTS)

View file

@ -72,7 +72,7 @@ void *VariableAlloc(Picoc *pc, struct ParseState *Parser, int Size, int OnHeap)
NewValue = HeapAllocStack(pc, Size);
if (NewValue == NULL)
ProgramFail(Parser, "out of memory");
ProgramFail(Parser, "(VariableAlloc) out of memory");
#ifdef DEBUG_HEAP
if (!OnHeap)
@ -95,7 +95,7 @@ struct Value *VariableAllocValueAndData(Picoc *pc, struct ParseState *Parser, in
if (Parser)
NewValue->ScopeID = Parser->ScopeID;
NewValue->OutOfScope = 0;
NewValue->OutOfScope = FALSE;
return NewValue;
}
@ -159,19 +159,20 @@ void VariableRealloc(struct ParseState *Parser, struct Value *FromValue, int New
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 *NextEntry;
Picoc *pc = Parser->pc;
int Count;
#ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0;
#endif
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... */
*OldScopeID = Parser->ScopeID;
@ -182,7 +183,7 @@ int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
for (Count = 0; Count < HashTable->Size; Count++) {
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry) {
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.Key = (char*)((intptr_t)Entry->p.v.Key & ~1);
#ifdef VAR_SCOPE_DEBUG
@ -197,24 +198,26 @@ int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
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;
Picoc *pc = Parser->pc;
struct TableEntry *Entry;
struct TableEntry *NextEntry = NULL;
#ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0;
#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 (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry) {
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
if (!FirstPrint) {
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;
for (Count = 0; Count < HashTable->Size; Count++) {
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;
}
}
@ -397,7 +400,7 @@ void VariableStackFrameAdd(struct ParseState *Parser, const char *FuncName, int
HeapPushStackFrame(Parser->pc);
NewFrame = HeapAllocStack(Parser->pc, sizeof(struct StackFrame) + sizeof(struct Value *) * NumParams);
if (NewFrame == NULL)
ProgramFail(Parser, "out of memory");
ProgramFail(Parser, "(VariableStackFrameAdd) out of memory");
ParserCopy(&NewFrame->ReturnParser, Parser);
NewFrame->FuncName = FuncName;