|
+1 I had to look up Duff's device. I've done it before, but i never knew it had a name.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I believe this was the function that made me completely reevaluate my approach to parsing. Ultimately, I came up with something FAR simpler, that wasn't going to be a nightmare to change when adding another condition to the code.
bool parser::parseExecute()
{
tokStruct_ *workTokStack;
precedence_ *workPrecStack;
int numTok=0, numPrec=0;
tokStackPos = -1;
if (!setupTokStackMem(&workTokStack, ansStackAlloc) ||
!setupPrecStackMem(&workPrecStack, ansStackAlloc)) {
MsgBox("Unable to allocate memory in ExecuteToken");
return false;
}
workPrecStack[0] = PrecGet;
workTokStack[0].token = TokGet;
do {
++tokStackPos;
if (tokStack[tokStackPos].token == TokValue) {
ansStack[++ansStackPos] = valStack[tokStack[tokStackPos].varListPos];
continue;
}
if (tokStack[tokStackPos].token == Tok0DimVar) {
ansStack[++ansStackPos] = getValFromVarList();
continue;
}
if (tokStack[tokStackPos].token == TokMDimVarEnd) {
if (workTokStack[tokStackPos-1].token == TokMDimVarAssign) break;
if (workTokStack[tokStackPos-1].token != TokMDimVar) {
MsgBox("']' encountered without arrayed variable");
return moveCursorToError(origStr);
}
int Y, Z, pos, dim;
Y = varList[workTokStack[numTok-1].varListPos].Y;
Z = varList[workTokStack[numTok-1].varListPos].Z;
dim = varList[workTokStack[numTok-1].varListPos].varDim;
if (dim == 1) pos = ansStack[ansStackPos];
else if (dim == 2) pos = ansStack[ansStackPos-1] +
ansStack[ansStackPos]*Y;
else if (dim == 3) pos = ansStack[ansStackPos-2] +
ansStack[ansStackPos-1]*Y + ansStack[ansStackPos]*Y*Z;
ansStackPos = ansStackPos-dim+1;
if (varList[workTokStack[numTok-1].varListPos].varType == Int) {
ansStack[ansStackPos] =
*((int*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos);
}
else if (varList[workTokStack[numTok-1].varListPos].varType == Double) {
ansStack[ansStackPos] =
*((double*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos);
}
else {
MsgBox("Bad varType placed on ansStack");
return moveCursorToError(origStr);
}
ansStackPos++;
numTok--; continue;
}
++numTok; ++numPrec;
workTokStack[numTok] = tokStack[tokStackPos];
workPrecStack[numPrec] = precStack[tokStackPos];
if (workTokStack[numTok].token == TokLParen) continue;
while (workPrecStack[numPrec-1] >= workPrecStack[numPrec]) {
workPrecStack[numPrec-1] = workPrecStack[numPrec];
numPrec--;
switch (workTokStack[numTok-1].token) {
case TokValue: ansStack[++ansStackPos] =
*(double*)varList[workTokStack[numTok-1].varListPos].varAddress;
numTok++;
numPrec++;
tokStackPos++;
break;
case TokVarAssign: if (ansStackPos != 0) {
MsgBox("Malformed statement");
delete workTokStack;
delete workPrecStack;
return false;
}
*(double*)varList[workTokStack[numTok-1].varListPos].varAddress = ansStack[0];
numTok=numTok-2;
numPrec--;
ansStackPos--;
goto pETBreakout;
case TokAdd: ansStack[ansStackPos-1] = ansStack[ansStackPos-1] + ansStack[ansStackPos];
ansStackPos--;
numTok--;
break;
case TokSubtract:
ansStack[ansStackPos-1] = ansStack[ansStackPos-1] - ansStack[ansStackPos];
ansStackPos--;
numTok--;
break;
case TokMultiply:
ansStack[ansStackPos-1] = ansStack[ansStackPos-1] * ansStack[ansStackPos];
ansStackPos--;
numTok--;
break;
case TokDivide:
ansStack[ansStackPos-1] = ansStack[ansStackPos-1] / ansStack[ansStackPos];
ansStackPos--;
numTok--;
break;
case TokPower:
ansStack[ansStackPos-1] = pow(ansStack[ansStackPos-1], ansStack[ansStackPos]);
ansStackPos--;
numTok--;
break;
case TokSemiCol: if (ansStackPos != 0) {
MsgBox("Misformed Statement");
delete workTokStack;
delete workPrecStack;
return false;
}
ansStackPos--;
break;
case TokComma:
numTok--;
break;
case TokLParen:
if (tokStack[tokStackPos].token == TokLParen &&
tokStack[tokStackPos-1].token == TokLParen) {
numPrec++;
goto pETBreakout;
}
numTok=numTok-2;
numPrec--;
goto pETBreakout;
case TokRParen:
break;
case TokMDimVar:
int Y, Z, pos, dim;
Y = varList[workTokStack[numTok-1].varListPos].Y;
Z = varList[workTokStack[numTok-1].varListPos].Z;
dim = varList[workTokStack[numTok-1].varListPos].varDim;
if (dim == 1) pos = ansStack[ansStackPos-1];
else if (dim == 2) pos = ansStack[ansStackPos-2] +
ansStack[ansStackPos-1]*Y;
else if (dim == 3) pos = ansStack[ansStackPos-3] +
ansStack[ansStackPos-2]*Y + ansStack[ansStackPos-1]*Y*Z;
if (varList[workTokStack[numTok-1].varListPos].varType == Int) {
*((int*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos) =
ansStack[ansStackPos];
}
else if (varList[workTokStack[numTok-1].varListPos].varType == Double) {
*((double*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos) =
ansStack[ansStackPos];
}
else {
MsgBox("Bad varType placed on ansStack");
return moveCursorToError(origStr);
}
ansStackPos = ansStackPos-dim;
numTok--; continue;
default:
MsgBox("Still Programming!");
delete workTokStack;
delete workPrecStack;
return false;
}
workTokStack[numTok] = workTokStack[numTok+1];
} pETBreakout:
} while (workPrecStack[numPrec] != PrecEnd);
delete workPrecStack; workPrecStack = NULL;
delete workTokStack; workTokStack = NULL;
return true;
}
|
|
|
|
|
yeah, two stacks is complicated.
you should just need the one at most (not even that if you're borrowing *the* stack (call stack) using recursive descent.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Pfft. That's art to the stuff I've written. My stuff is so bad if I posted it here your computer would keel over in fright and refuse to reboot.
cheers
Chris Maunder
|
|
|
|
|
I'm glad i'm not the only target of Sander and his pitchfork.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Yes you are.
Speaking of which...
|
|
|
|
|
i'll turn you into a VB6 developer
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Been there done that
|
|
|
|
|
In that case I'll hex your boss so you get stuck on front end work.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I'm my own boss
|
|
|
|
|
Even better.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Is it part of the code behind CP...?
|
|
|
|
|
dandy72 wrote: the code behind CP
No.
That code's even worse
cheers
Chris Maunder
|
|
|
|
|
Yes, and it's a work of art. Picasso is also art mind you.
I spent two weeks on it and it solved quite a few problems in one go.
<edit>Two different ways to parse a tree and a statemachine for the nodes.</edit>
I was very proud of it at the moment.
Today I look at it and think WTF!
modified 13-Sep-19 18:37pm.
|
|
|
|
|
And I thought ugly functions are only written in ... VB.
|
|
|
|
|
I started writing one about a month ago, and I'm still not finished.
|
|
|
|
|
sounds familiar. the one in the OP took two weeks to get to that.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
PITCHFORK TIME!
I can't remember the ugliest I've ever written, but a recent one was something like this (I can't post the actual code):
if (...)
{
if (...)
{
}
if (...)
{
}
foreach (...)
{
}
}
else
{
if (...)
{
foreach (...)
{
if (...)
{
}
foreach (...)
{
}
}
}
else
{
if (...)
{
foreach (...)
{
}
}
}
} Total of 64 lines long
And that's already the refactored function.
That entire class is a bit of a mess.
I have to convert a flat CSV file to some multi-layered standard with mandatory fields (that aren't necessary in the CSV), default values, some interpreted values, etc.
The class has 37 if's in total.
That's about as much as I usually have in an entire project
Compared to your multi layered mess of single line if statements it's still a work of beauty though
|
|
|
|
|
but can it parse html?
can it index a million rows of data?
*raises eyebrow*
when you do witchy things, your code will finally look witchy as a result.
just like when you're writing your code that deals with a mess of input your code looks like a mess.
we are a product of our environment, and so is our code.
my code comes from a crazy person. it shall reflect that.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
|
Oh i was insane to pick up coding in the first place.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
|
Nazdrovje !
|
|
|
|
|
|
As a programmer, every day is Programmer's Day.
Acknowledgement from the normal humans in unnecessary.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|