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

@ -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
{
@ -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);

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

@ -545,7 +545,7 @@ extern void VariableScopeEnd(struct ParseState * Parser, int ScopeID, int PrevSc
/* 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

@ -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

@ -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;
}
@ -161,17 +161,18 @@ void VariableRealloc(struct ParseState *Parser, struct Value *FromValue, int New
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
@ -199,22 +200,24 @@ int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
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;
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;