From 45a378231bd82e15d152edb3c882bb6aafce60d6 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sun, 29 Mar 2009 11:08:03 +0000 Subject: [PATCH] More work on expressions git-svn-id: http://picoc.googlecode.com/svn/trunk@213 21eae674-98b7-11dd-bd71-f92a316d2d60 --- expression.c | 19 +++++++++++++++---- picoc.h | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/expression.c b/expression.c index 1da7a60..f9f1ca5 100644 --- a/expression.c +++ b/expression.c @@ -422,6 +422,7 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result) enum OperatorOrder { + OrderNone, OrderPrefix, OrderInfix, OrderPostfix @@ -447,6 +448,7 @@ struct OpPrecedence static struct OpPrecedence OperatorPrecedence[] = { + /* TokenNone, */ { 0, 0, 0 }, /* TokenComma, */ { 0, 0, 1 }, /* TokenAssign, */ { 0, 0, 2 }, /* TokenAddAssign, */ { 0, 0, 2 }, /* TokenSubtractAssign, */ { 0, 0, 2 }, /* TokenMultiplyAssign, */ { 0, 0, 2 }, /* TokenDivideAssign, */ { 0, 0, 2 }, /* TokenModulusAssign, */ { 0, 0, 2 }, @@ -734,7 +736,7 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack * struct ExpressionStack *TopStackNode = *StackTop; struct ExpressionStack *TopOperatorNode; - while (TopStackNode != NULL && FoundPrecedence >= Precedence) + while (TopStackNode != NULL && TopStackNode->Next != NULL && FoundPrecedence >= Precedence) { /* find the top operator on the stack */ if (TopStackNode->Val != NULL) @@ -743,7 +745,7 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack * TopOperatorNode = TopStackNode; /* does it have a high enough precedence? */ - if (FoundPrecedence >= Precedence) + if (FoundPrecedence >= Precedence && TopOperatorNode != NULL) { /* execute this operator */ switch (TopOperatorNode->Order) @@ -783,6 +785,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack * /* do the infix operation */ ExpressionInfixOperator(Parser, StackTop, TopOperatorNode->Op, BottomValue, TopValue); break; + + case OrderNone: + break; } } } @@ -803,7 +808,7 @@ void ExpressionStackPushOperator(struct ParseState *Parser, struct ExpressionSta int ExpressionParse(struct ParseState *Parser, struct Value **Result) { struct Value *LexValue; - int PrefixState = FALSE; + int PrefixState = TRUE; int Done = FALSE; int BracketPrecedence = 0; int LocalPrecedence; @@ -906,7 +911,13 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result) /* scan and collapse the stack to precedence 0 */ ExpressionStackCollapse(Parser, &StackTop, 0); - //XXX - fix up the stack and return the result if we're in run mode + + /* fix up the stack and return the result if we're in run mode */ + if (StackTop != NULL) + { + /* all that should be left is a single value on the stack */ + HeapPopStack((void *)StackTop - sizeof(struct ExpressionStack), sizeof(struct ExpressionStack)); + } return TRUE; } diff --git a/picoc.h b/picoc.h index faaad7b..5599357 100644 --- a/picoc.h +++ b/picoc.h @@ -31,6 +31,7 @@ struct Table; /* lexical tokens */ enum LexToken { + TokenNone, TokenComma, TokenAssign, TokenAddAssign, TokenSubtractAssign, TokenMultiplyAssign, TokenDivideAssign, TokenModulusAssign, TokenShiftLeftAssign, TokenShiftRightAssign, TokenArithmeticAndAssign, TokenArithmeticOrAssign, TokenArithmeticExorAssign, @@ -55,7 +56,7 @@ enum LexToken TokenLongType, TokenSignedType, TokenShortType, TokenStructType, TokenUnionType, TokenUnsignedType, TokenTypedef, TokenContinue, TokenDo, TokenElse, TokenFor, TokenIf, TokenWhile, TokenBreak, TokenSwitch, TokenCase, TokenDefault, TokenReturn, TokenHashDefine, TokenHashInclude, TokenNew, TokenDelete, - TokenNone, TokenEOF, TokenEndOfLine, TokenEndOfFunction + TokenEOF, TokenEndOfLine, TokenEndOfFunction }; /* used in dynamic memory allocation */