More work on expressions
git-svn-id: http://picoc.googlecode.com/svn/trunk@213 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
8baa2f71f7
commit
45a378231b
19
expression.c
19
expression.c
|
@ -422,6 +422,7 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
|
|
||||||
enum OperatorOrder
|
enum OperatorOrder
|
||||||
{
|
{
|
||||||
|
OrderNone,
|
||||||
OrderPrefix,
|
OrderPrefix,
|
||||||
OrderInfix,
|
OrderInfix,
|
||||||
OrderPostfix
|
OrderPostfix
|
||||||
|
@ -447,6 +448,7 @@ struct OpPrecedence
|
||||||
|
|
||||||
static struct OpPrecedence OperatorPrecedence[] =
|
static struct OpPrecedence OperatorPrecedence[] =
|
||||||
{
|
{
|
||||||
|
/* TokenNone, */ { 0, 0, 0 },
|
||||||
/* TokenComma, */ { 0, 0, 1 },
|
/* TokenComma, */ { 0, 0, 1 },
|
||||||
/* TokenAssign, */ { 0, 0, 2 }, /* TokenAddAssign, */ { 0, 0, 2 }, /* TokenSubtractAssign, */ { 0, 0, 2 },
|
/* TokenAssign, */ { 0, 0, 2 }, /* TokenAddAssign, */ { 0, 0, 2 }, /* TokenSubtractAssign, */ { 0, 0, 2 },
|
||||||
/* TokenMultiplyAssign, */ { 0, 0, 2 }, /* TokenDivideAssign, */ { 0, 0, 2 }, /* TokenModulusAssign, */ { 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 *TopStackNode = *StackTop;
|
||||||
struct ExpressionStack *TopOperatorNode;
|
struct ExpressionStack *TopOperatorNode;
|
||||||
|
|
||||||
while (TopStackNode != NULL && FoundPrecedence >= Precedence)
|
while (TopStackNode != NULL && TopStackNode->Next != NULL && FoundPrecedence >= Precedence)
|
||||||
{
|
{
|
||||||
/* find the top operator on the stack */
|
/* find the top operator on the stack */
|
||||||
if (TopStackNode->Val != NULL)
|
if (TopStackNode->Val != NULL)
|
||||||
|
@ -743,7 +745,7 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
|
||||||
TopOperatorNode = TopStackNode;
|
TopOperatorNode = TopStackNode;
|
||||||
|
|
||||||
/* does it have a high enough precedence? */
|
/* does it have a high enough precedence? */
|
||||||
if (FoundPrecedence >= Precedence)
|
if (FoundPrecedence >= Precedence && TopOperatorNode != NULL)
|
||||||
{
|
{
|
||||||
/* execute this operator */
|
/* execute this operator */
|
||||||
switch (TopOperatorNode->Order)
|
switch (TopOperatorNode->Order)
|
||||||
|
@ -783,6 +785,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack *
|
||||||
/* do the infix operation */
|
/* do the infix operation */
|
||||||
ExpressionInfixOperator(Parser, StackTop, TopOperatorNode->Op, BottomValue, TopValue);
|
ExpressionInfixOperator(Parser, StackTop, TopOperatorNode->Op, BottomValue, TopValue);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case OrderNone:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -803,7 +808,7 @@ void ExpressionStackPushOperator(struct ParseState *Parser, struct ExpressionSta
|
||||||
int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
{
|
{
|
||||||
struct Value *LexValue;
|
struct Value *LexValue;
|
||||||
int PrefixState = FALSE;
|
int PrefixState = TRUE;
|
||||||
int Done = FALSE;
|
int Done = FALSE;
|
||||||
int BracketPrecedence = 0;
|
int BracketPrecedence = 0;
|
||||||
int LocalPrecedence;
|
int LocalPrecedence;
|
||||||
|
@ -906,7 +911,13 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
|
||||||
|
|
||||||
/* scan and collapse the stack to precedence 0 */
|
/* scan and collapse the stack to precedence 0 */
|
||||||
ExpressionStackCollapse(Parser, &StackTop, 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;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
3
picoc.h
3
picoc.h
|
@ -31,6 +31,7 @@ struct Table;
|
||||||
/* lexical tokens */
|
/* lexical tokens */
|
||||||
enum LexToken
|
enum LexToken
|
||||||
{
|
{
|
||||||
|
TokenNone,
|
||||||
TokenComma,
|
TokenComma,
|
||||||
TokenAssign, TokenAddAssign, TokenSubtractAssign, TokenMultiplyAssign, TokenDivideAssign, TokenModulusAssign,
|
TokenAssign, TokenAddAssign, TokenSubtractAssign, TokenMultiplyAssign, TokenDivideAssign, TokenModulusAssign,
|
||||||
TokenShiftLeftAssign, TokenShiftRightAssign, TokenArithmeticAndAssign, TokenArithmeticOrAssign, TokenArithmeticExorAssign,
|
TokenShiftLeftAssign, TokenShiftRightAssign, TokenArithmeticAndAssign, TokenArithmeticOrAssign, TokenArithmeticExorAssign,
|
||||||
|
@ -55,7 +56,7 @@ enum LexToken
|
||||||
TokenLongType, TokenSignedType, TokenShortType, TokenStructType, TokenUnionType, TokenUnsignedType, TokenTypedef,
|
TokenLongType, TokenSignedType, TokenShortType, TokenStructType, TokenUnionType, TokenUnsignedType, TokenTypedef,
|
||||||
TokenContinue, TokenDo, TokenElse, TokenFor, TokenIf, TokenWhile, TokenBreak, TokenSwitch, TokenCase, TokenDefault, TokenReturn,
|
TokenContinue, TokenDo, TokenElse, TokenFor, TokenIf, TokenWhile, TokenBreak, TokenSwitch, TokenCase, TokenDefault, TokenReturn,
|
||||||
TokenHashDefine, TokenHashInclude, TokenNew, TokenDelete,
|
TokenHashDefine, TokenHashInclude, TokenNew, TokenDelete,
|
||||||
TokenNone, TokenEOF, TokenEndOfLine, TokenEndOfFunction
|
TokenEOF, TokenEndOfLine, TokenEndOfFunction
|
||||||
};
|
};
|
||||||
|
|
||||||
/* used in dynamic memory allocation */
|
/* used in dynamic memory allocation */
|
||||||
|
|
Loading…
Reference in a new issue