More work on operator precedence. Plenty to do yet.

git-svn-id: http://picoc.googlecode.com/svn/trunk@171 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-03-08 08:40:05 +00:00
parent dc04ba8dd0
commit fab9d6ed1e
2 changed files with 39 additions and 18 deletions

1
TODO
View file

@ -20,6 +20,7 @@ Improvements:
* change heap to use a single consistent freelist node struct * change heap to use a single consistent freelist node struct
* 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
Need test/debug: Need test/debug:
* all break/continue variations * all break/continue variations

View file

@ -396,6 +396,21 @@ static struct OpPrecedence OperatorPrecedence[] =
/* TokenOpenBracket, */ { 15, 0, 0 }, /* TokenCloseBracket, */ { 0, 15, 0 } /* TokenOpenBracket, */ { 15, 0, 0 }, /* TokenCloseBracket, */ { 0, 15, 0 }
}; };
/* take the contents of the expression stack and compute the top until there's nothing greater than the given precedence */
void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack **StackTop, int Precedence)
{
}
/* push an operator on to the expression stack */
void ExpressionStackPushOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum OperatorOrder Order, enum LexToken Token, int Precedence)
{
}
/* push a value on to the expression stack */
void ExpressionStackPushValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue)
{
}
/* parse an expression with operator precedence */ /* parse an expression with operator precedence */
int ExpressionParse(struct ParseState *Parser, struct Value **Result) int ExpressionParse(struct ParseState *Parser, struct Value **Result)
{ {
@ -404,6 +419,7 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
bool Done = false; bool Done = false;
int BracketPrecedence = 0; int BracketPrecedence = 0;
int Precedence = 0; int Precedence = 0;
struct ExpressionStack *StackTop = NULL;
do do
{ {
@ -417,16 +433,16 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
if (Parser->Mode == RunModeRun) if (Parser->Mode == RunModeRun)
{ {
Precedence = BracketPrecedence + OperatorPrecedence[(int)Token].PrefixPrecedence;
if (Token == TokenOpenBracket || Token == TokenLeftSquareBracket) if (Token == TokenOpenBracket || Token == TokenLeftSquareBracket)
{ { /* boost the bracket operator precedence, then push */
/* boost the bracket operator precedence */
BracketPrecedence += BRACKET_PREDECENCE; BracketPrecedence += BRACKET_PREDECENCE;
/* push the operator */ ExpressionStackPushOperator(Parser, &StackTop, OrderPrefix, Token, Precedence);
} }
else else
{ { /* scan and collapse the stack to the precedence of this operator, then push */
/* scan and collapse the stack to the precedence of this operator */ ExpressionStackCollapse(Parser, &StackTop, Precedence);
/* push the operator */ ExpressionStackPushOperator(Parser, &StackTop, OrderPrefix, Token, Precedence);
} }
} }
} }
@ -439,7 +455,6 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
case TokenCloseBracket: case TokenCloseBracket:
if (BracketPrecedence == 0) if (BracketPrecedence == 0)
{ /* assume this bracket is after the end of the expression */ { /* assume this bracket is after the end of the expression */
/* scan and collapse the stack to precedence 0 */
Done = true; Done = true;
} }
else else
@ -451,21 +466,26 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
ProgramFail(Parser, "no matching open square bracket"); ProgramFail(Parser, "no matching open square bracket");
/* scan and collapse the stack to bracket precedence */ /* scan and collapse the stack to bracket precedence */
ExpressionStackCollapse(Parser, &StackTop, BracketPrecedence);
BracketPrecedence -= BRACKET_PRECEDENCE; BracketPrecedence -= BRACKET_PRECEDENCE;
/* apply the array index operator */ /* apply the array index operator */
// XXX
break; break;
default: default:
/* scan and collapse the stack to the precedence of this operator */ /* scan and collapse the stack to the precedence of this operator, then push */
/* push the operator */ Precedence = BracketPrecedence + OperatorPrecedence[(int)Token].PostfixPrecedence;
ExpressionStackCollapse(Parser, &StackTop, Precedence);
ExpressionStackPushOperator(Parser, &StackTop, OrderPostfix, Token, Precedence);
break; break;
} }
} }
else if (OperatorPrecedence[(int)Token].InfixPrecedence != 0) else if (OperatorPrecedence[(int)Token].InfixPrecedence != 0)
{ { /* scan and collapse the stack to the precedence of this operator, then push */
/* scan and collapse the stack to the precedence of this operator */ Precedence = BracketPrecedence + OperatorPrecedence[(int)Token].InfixPrecedence;
/* push the operator */ ExpressionStackCollapse(Parser, &StackTop, Precedence);
ExpressionStackPushOperator(Parser, &StackTop, OrderInfix, Token, Precedence);
PrefixState = true; PrefixState = true;
} }
else else
@ -473,23 +493,23 @@ int ExpressionParse(struct ParseState *Parser, struct Value **Result)
} }
} }
else if ((int)Token <= (int)TokenCharacterConstant) else if ((int)Token <= (int)TokenCharacterConstant)
{ /* it's a value of some sort */ { /* it's a value of some sort, push it */
if (!PrefixState) if (!PrefixState)
ProgramFail(Parser, "value not expected here"); ProgramFail(Parser, "value not expected here");
PrefixState = false; PrefixState = false;
if (Parser->Mode == RunModeRun) ExpressionStackPushValue(Parser, &StackTop, LexValue);
{
/* push the value */
}
} }
else else
{ /* it isn't a token from an expression */ { /* it isn't a token from an expression */
/* scan and collapse the stack to precedence 0 */
Done = true; Done = true;
} }
} while (!Done); } while (!Done);
/* 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
} }
#endif #endif