Final problems with the new expression system fixed
git-svn-id: http://picoc.googlecode.com/svn/trunk@248 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
fdbdb116b6
commit
2e22614595
1
TODO
1
TODO
|
@ -13,6 +13,7 @@ Improvements:
|
||||||
* periodic heap cleanup
|
* periodic heap cleanup
|
||||||
* octal/hex character constants
|
* octal/hex character constants
|
||||||
* see if we can use ParserCopyPos() in other places rather than copying everything
|
* see if we can use ParserCopyPos() in other places rather than copying everything
|
||||||
|
* check for no value returned in functions with a return value
|
||||||
|
|
||||||
Need test/debug:
|
Need test/debug:
|
||||||
* new/delete heap allocation
|
* new/delete heap allocation
|
||||||
|
|
29
expression.c
29
expression.c
|
@ -581,7 +581,7 @@ void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionSta
|
||||||
/* show the contents of the expression stack */
|
/* show the contents of the expression stack */
|
||||||
void ExpressionStackShow(struct ExpressionStack *StackTop)
|
void ExpressionStackShow(struct ExpressionStack *StackTop)
|
||||||
{
|
{
|
||||||
printf("Expression stack: ");
|
printf("Expression stack [0x%lx,0x%lx]: ", (long)HeapStackTop, (long)StackTop);
|
||||||
|
|
||||||
while (StackTop != NULL)
|
while (StackTop != NULL)
|
||||||
{
|
{
|
||||||
|
@ -612,12 +612,14 @@ void ExpressionStackShow(struct ExpressionStack *StackTop)
|
||||||
case TypeEnum: printf("%s:enum", StackTop->p.Val->Val->Identifier); break;
|
case TypeEnum: printf("%s:enum", StackTop->p.Val->Val->Identifier); break;
|
||||||
default: printf("unknown"); break;
|
default: printf("unknown"); break;
|
||||||
}
|
}
|
||||||
|
printf("[0x%lx,0x%lx]", (long)StackTop, (long)StackTop->p.Val);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ /* it's an operator */
|
{ /* it's an operator */
|
||||||
printf("op='%s' %s %d", OperatorPrecedence[(int)StackTop->p.Op].Name,
|
printf("op='%s' %s %d", OperatorPrecedence[(int)StackTop->p.Op].Name,
|
||||||
(StackTop->Order == OrderPrefix) ? "prefix" : ((StackTop->Order == OrderPostfix) ? "postfix" : "infix"),
|
(StackTop->Order == OrderPrefix) ? "prefix" : ((StackTop->Order == OrderPostfix) ? "postfix" : "infix"),
|
||||||
StackTop->Precedence);
|
StackTop->Precedence);
|
||||||
|
printf("[0x%lx]", (long)StackTop);
|
||||||
}
|
}
|
||||||
|
|
||||||
StackTop = StackTop->Next;
|
StackTop = StackTop->Next;
|
||||||
|
@ -644,39 +646,39 @@ void ExpressionStackPushValueNode(struct ParseState *Parser, struct ExpressionSt
|
||||||
/* push a blank value on to the expression stack by type */
|
/* push a blank value on to the expression stack by type */
|
||||||
void ExpressionStackPushValueByType(struct ParseState *Parser, struct ExpressionStack **StackTop, struct ValueType *PushType)
|
void ExpressionStackPushValueByType(struct ParseState *Parser, struct ExpressionStack **StackTop, struct ValueType *PushType)
|
||||||
{
|
{
|
||||||
struct Value *ValueLoc = VariableAllocValueFromType(Parser, PushType, FALSE, NULL);
|
|
||||||
debugf("ExpressionStackPushValueByType()\n");
|
debugf("ExpressionStackPushValueByType()\n");
|
||||||
|
struct Value *ValueLoc = VariableAllocValueFromType(Parser, PushType, FALSE, NULL);
|
||||||
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* push a value on to the expression stack */
|
/* push a value on to the expression stack */
|
||||||
void ExpressionStackPushValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue)
|
void ExpressionStackPushValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue)
|
||||||
{
|
{
|
||||||
struct Value *ValueLoc = VariableAllocValueAndCopy(Parser, PushValue, FALSE);
|
|
||||||
debugf("ExpressionStackPushValue()\n");
|
debugf("ExpressionStackPushValue()\n");
|
||||||
|
struct Value *ValueLoc = VariableAllocValueAndCopy(Parser, PushValue, FALSE);
|
||||||
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionStackPushLValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue)
|
void ExpressionStackPushLValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue)
|
||||||
{
|
{
|
||||||
struct Value *ValueLoc = VariableAllocValueShared(Parser, PushValue);
|
|
||||||
debugf("ExpressionStackPushLValue()\n");
|
debugf("ExpressionStackPushLValue()\n");
|
||||||
|
struct Value *ValueLoc = VariableAllocValueShared(Parser, PushValue);
|
||||||
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionPushInt(struct ParseState *Parser, struct ExpressionStack **StackTop, int IntValue)
|
void ExpressionPushInt(struct ParseState *Parser, struct ExpressionStack **StackTop, int IntValue)
|
||||||
{
|
{
|
||||||
|
debugf("ExpressionPushInt()\n");
|
||||||
struct Value *ValueLoc = VariableAllocValueFromType(Parser, &IntType, FALSE, NULL);
|
struct Value *ValueLoc = VariableAllocValueFromType(Parser, &IntType, FALSE, NULL);
|
||||||
ValueLoc->Val->Integer = IntValue;
|
ValueLoc->Val->Integer = IntValue;
|
||||||
debugf("ExpressionPushInt()\n");
|
|
||||||
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpressionPushFP(struct ParseState *Parser, struct ExpressionStack **StackTop, double FPValue)
|
void ExpressionPushFP(struct ParseState *Parser, struct ExpressionStack **StackTop, double FPValue)
|
||||||
{
|
{
|
||||||
|
debugf("ExpressionPushFP()\n");
|
||||||
struct Value *ValueLoc = VariableAllocValueFromType(Parser, &FPType, FALSE, NULL);
|
struct Value *ValueLoc = VariableAllocValueFromType(Parser, &FPType, FALSE, NULL);
|
||||||
ValueLoc->Val->FP = FPValue;
|
ValueLoc->Val->FP = FPValue;
|
||||||
debugf("ExpressionPushFP()\n");
|
|
||||||
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1034,6 +1036,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
|
||||||
TopStackNode = *StackTop;
|
TopStackNode = *StackTop;
|
||||||
}
|
}
|
||||||
debugf("ExpressionStackCollapse() finished\n");
|
debugf("ExpressionStackCollapse() finished\n");
|
||||||
|
#ifdef DEBUG_EXPRESSIONS
|
||||||
|
ExpressionStackShow(*StackTop);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* push an operator on to the expression stack */
|
/* push an operator on to the expression stack */
|
||||||
|
@ -1230,10 +1235,10 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
}
|
}
|
||||||
else /* push a dummy value */
|
else /* push a dummy value */
|
||||||
ExpressionPushInt(Parser, &StackTop, 0);
|
ExpressionPushInt(Parser, &StackTop, 0);
|
||||||
|
}
|
||||||
|
|
||||||
PrefixState = FALSE;
|
PrefixState = FALSE;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if ((int)Token > TokenCloseBracket && (int)Token <= TokenCharacterConstant)
|
else if ((int)Token > TokenCloseBracket && (int)Token <= TokenCharacterConstant)
|
||||||
{
|
{
|
||||||
/* it's a value of some sort, push it */
|
/* it's a value of some sort, push it */
|
||||||
|
@ -1264,12 +1269,18 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
{
|
{
|
||||||
/* all that should be left is a single value on the stack */
|
/* all that should be left is a single value on the stack */
|
||||||
if (Parser->Mode == RunModeRun)
|
if (Parser->Mode == RunModeRun)
|
||||||
|
{
|
||||||
*Result = StackTop->p.Val;
|
*Result = StackTop->p.Val;
|
||||||
|
|
||||||
HeapPopStack(StackTop, sizeof(struct ExpressionStack));
|
HeapPopStack(StackTop, sizeof(struct ExpressionStack));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
HeapPopStack(StackTop->p.Val, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(StackTop->p.Val));
|
||||||
|
}
|
||||||
|
|
||||||
debugf("ExpressionParse() done\n");
|
debugf("ExpressionParse() done\n");
|
||||||
|
#ifdef DEBUG_EXPRESSIONS
|
||||||
|
ExpressionStackShow(StackTop);
|
||||||
|
#endif
|
||||||
return StackTop != NULL;
|
return StackTop != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1298,6 +1309,8 @@ void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionSta
|
||||||
if (ParamArray == NULL)
|
if (ParamArray == NULL)
|
||||||
ProgramFail(Parser, "out of memory");
|
ProgramFail(Parser, "out of memory");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
ExpressionPushInt(Parser, StackTop, 0);
|
||||||
|
|
||||||
/* parse arguments */
|
/* parse arguments */
|
||||||
ArgCount = 0;
|
ArgCount = 0;
|
||||||
|
|
34
heap.c
34
heap.c
|
@ -9,12 +9,12 @@
|
||||||
static unsigned char *HeapMemory = (unsigned char *)C_HEAPSTART; /* all memory - stack and heap */
|
static unsigned char *HeapMemory = (unsigned char *)C_HEAPSTART; /* all memory - stack and heap */
|
||||||
static void *HeapBottom = (void *)C_HEAPSTART + HEAP_SIZE; /* the bottom of the (downward-growing) heap */
|
static void *HeapBottom = (void *)C_HEAPSTART + HEAP_SIZE; /* the bottom of the (downward-growing) heap */
|
||||||
static void *StackFrame = (void *)C_HEAPSTART; /* the current stack frame */
|
static void *StackFrame = (void *)C_HEAPSTART; /* the current stack frame */
|
||||||
static void *StackTop = (void *)C_HEAPSTART; /* the top of the stack */
|
static void *HeapStackTop = (void *)C_HEAPSTART; /* the top of the stack */
|
||||||
#else
|
#else
|
||||||
static unsigned char HeapMemory[HEAP_SIZE]; /* all memory - stack and heap */
|
static unsigned char HeapMemory[HEAP_SIZE]; /* all memory - stack and heap */
|
||||||
static void *HeapBottom = &HeapMemory[HEAP_SIZE]; /* the bottom of the (downward-growing) heap */
|
static void *HeapBottom = &HeapMemory[HEAP_SIZE]; /* the bottom of the (downward-growing) heap */
|
||||||
static void *StackFrame = &HeapMemory[0]; /* the current stack frame */
|
static void *StackFrame = &HeapMemory[0]; /* the current stack frame */
|
||||||
static void *StackTop = &HeapMemory[0]; /* the top of the stack */
|
void *HeapStackTop = &HeapMemory[0]; /* the top of the stack */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static struct AllocNode *FreeListBucket[FREELIST_BUCKETS]; /* we keep a pool of freelist buckets to reduce fragmentation */
|
static struct AllocNode *FreeListBucket[FREELIST_BUCKETS]; /* we keep a pool of freelist buckets to reduce fragmentation */
|
||||||
|
@ -39,7 +39,7 @@ void HeapInit()
|
||||||
int Count;
|
int Count;
|
||||||
|
|
||||||
StackFrame = &HeapMemory[0];
|
StackFrame = &HeapMemory[0];
|
||||||
StackTop = &HeapMemory[0];
|
HeapStackTop = &HeapMemory[0];
|
||||||
*(void **)StackFrame = NULL;
|
*(void **)StackFrame = NULL;
|
||||||
HeapBottom = &HeapMemory[HEAP_SIZE];
|
HeapBottom = &HeapMemory[HEAP_SIZE];
|
||||||
FreeListBig = NULL;
|
FreeListBig = NULL;
|
||||||
|
@ -51,12 +51,12 @@ void HeapInit()
|
||||||
* clears memory. can return NULL if out of stack space */
|
* clears memory. can return NULL if out of stack space */
|
||||||
void *HeapAllocStack(int Size)
|
void *HeapAllocStack(int Size)
|
||||||
{
|
{
|
||||||
void *NewMem = StackTop;
|
void *NewMem = HeapStackTop;
|
||||||
void *NewTop = StackTop + MEM_ALIGN(Size);
|
void *NewTop = HeapStackTop + MEM_ALIGN(Size);
|
||||||
if (NewTop > HeapBottom)
|
if (NewTop > HeapBottom)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
StackTop = NewTop;
|
HeapStackTop = NewTop;
|
||||||
memset(NewMem, '\0', Size);
|
memset(NewMem, '\0', Size);
|
||||||
return NewMem;
|
return NewMem;
|
||||||
}
|
}
|
||||||
|
@ -65,11 +65,13 @@ void *HeapAllocStack(int Size)
|
||||||
int HeapPopStack(void *Addr, int Size)
|
int HeapPopStack(void *Addr, int Size)
|
||||||
{
|
{
|
||||||
int ToLose = MEM_ALIGN(Size);
|
int ToLose = MEM_ALIGN(Size);
|
||||||
if (ToLose > (StackTop - (void *)&HeapMemory[0]))
|
if (ToLose > (HeapStackTop - (void *)&HeapMemory[0]))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
StackTop -= ToLose;
|
HeapStackTop -= ToLose;
|
||||||
assert(StackTop == Addr);
|
if (HeapStackTop != Addr)
|
||||||
|
printf("fail\n");
|
||||||
|
assert(HeapStackTop == Addr);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -78,11 +80,11 @@ int HeapPopStack(void *Addr, int Size)
|
||||||
void HeapPushStackFrame()
|
void HeapPushStackFrame()
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_HEAP
|
#ifdef DEBUG_HEAP
|
||||||
printf("Adding stack frame at 0x%lx\n", (unsigned long)StackTop);
|
printf("Adding stack frame at 0x%lx\n", (unsigned long)HeapStackTop);
|
||||||
#endif
|
#endif
|
||||||
*(void **)StackTop = StackFrame;
|
*(void **)HeapStackTop = StackFrame;
|
||||||
StackFrame = StackTop;
|
StackFrame = HeapStackTop;
|
||||||
StackTop += sizeof(void *);
|
HeapStackTop += sizeof(void *);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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 */
|
||||||
|
@ -90,10 +92,10 @@ int HeapPopStackFrame()
|
||||||
{
|
{
|
||||||
if (*(void **)StackFrame != NULL)
|
if (*(void **)StackFrame != NULL)
|
||||||
{
|
{
|
||||||
StackTop = StackFrame;
|
HeapStackTop = StackFrame;
|
||||||
StackFrame = *(void **)StackFrame;
|
StackFrame = *(void **)StackFrame;
|
||||||
#ifdef DEBUG_HEAP
|
#ifdef DEBUG_HEAP
|
||||||
printf("Popping stack frame back to 0x%lx\n", (unsigned long)StackTop);
|
printf("Popping stack frame back to 0x%lx\n", (unsigned long)HeapStackTop);
|
||||||
#endif
|
#endif
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +171,7 @@ void *HeapAlloc(int Size)
|
||||||
#ifdef DEBUG_HEAP
|
#ifdef DEBUG_HEAP
|
||||||
printf("allocating %d(%d) at bottom of heap (0x%lx-0x%lx)", Size, AllocSize, (long)(HeapBottom - AllocSize), (long)HeapBottom);
|
printf("allocating %d(%d) at bottom of heap (0x%lx-0x%lx)", Size, AllocSize, (long)(HeapBottom - AllocSize), (long)HeapBottom);
|
||||||
#endif
|
#endif
|
||||||
if (HeapBottom - AllocSize < StackTop)
|
if (HeapBottom - AllocSize < HeapStackTop)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
HeapBottom -= AllocSize;
|
HeapBottom -= AllocSize;
|
||||||
|
|
1
parse.c
1
parse.c
|
@ -465,6 +465,7 @@ int ParseStatement(struct ParseState *Parser)
|
||||||
// XXX - make assignment a separate function
|
// XXX - make assignment a separate function
|
||||||
// XXX - also arrays need cleverer assignment
|
// XXX - also arrays need cleverer assignment
|
||||||
memcpy((void *)TopStackFrame->ReturnValue->Val, (void *)CValue->Val, TypeSizeValue(CValue));
|
memcpy((void *)TopStackFrame->ReturnValue->Val, (void *)CValue->Val, TypeSizeValue(CValue));
|
||||||
|
VariableStackPop(Parser, CValue);
|
||||||
Parser->Mode = RunModeReturn;
|
Parser->Mode = RunModeReturn;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
1
picoc.h
1
picoc.h
|
@ -226,6 +226,7 @@ struct LibraryFunction
|
||||||
typedef void CharWriter(unsigned char);
|
typedef void CharWriter(unsigned char);
|
||||||
|
|
||||||
/* globals */
|
/* globals */
|
||||||
|
extern void *HeapStackTop;
|
||||||
extern struct Table GlobalTable;
|
extern struct Table GlobalTable;
|
||||||
extern struct StackFrame *TopStackFrame;
|
extern struct StackFrame *TopStackFrame;
|
||||||
extern struct ValueType UberType;
|
extern struct ValueType UberType;
|
||||||
|
|
|
@ -14,8 +14,8 @@ d = 78;
|
||||||
e = 0;
|
e = 0;
|
||||||
f = 1;
|
f = 1;
|
||||||
|
|
||||||
printf("%d\n", (a + b, c + d));
|
printf("%d\n", c + d);
|
||||||
printf("%d\n", (x = a + b, y = c + d));
|
printf("%d\n", (y = c + d));
|
||||||
printf("%d\n", a ? b+c : c+d);
|
printf("%d\n", a ? b+c : c+d);
|
||||||
printf("%d\n", a ? b+c : c+d);
|
printf("%d\n", a ? b+c : c+d);
|
||||||
printf("%d\n", a || b ? b+c : c+d);
|
printf("%d\n", a || b ? b+c : c+d);
|
||||||
|
|
|
@ -3,7 +3,7 @@ int factorial(int i)
|
||||||
if (i < 2)
|
if (i < 2)
|
||||||
return i;
|
return i;
|
||||||
else
|
else
|
||||||
return (i * factorial(i - 1));
|
return i * factorial(i - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Count;
|
int Count;
|
||||||
|
|
Loading…
Reference in a new issue