Click here to Skip to main content
14,361,490 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: okay. I ... really like TypeScript Pin
Sander Rossel14-Sep-19 6:08
professionalSander Rossel14-Sep-19 6:08 
GeneralRe: okay. I ... really like TypeScript Pin
honey the codewitch14-Sep-19 6:10
memberhoney the codewitch14-Sep-19 6:10 
GeneralDo you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 9:20
memberhoney the codewitch13-Sep-19 9:20 
JokeRe: Do you still have the ugliest function you've ever written? Pin
W Balboos13-Sep-19 9:54
mveW Balboos13-Sep-19 9:54 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 9:55
memberhoney the codewitch13-Sep-19 9:55 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
MarkTJohnson13-Sep-19 10:09
professionalMarkTJohnson13-Sep-19 10:09 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 10:10
memberhoney the codewitch13-Sep-19 10:10 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
David O'Neil13-Sep-19 10:12
professionalDavid O'Neil13-Sep-19 10:12 
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()
{
   //Start at the bottom of the token and precedence stack.  Place these values onto the
   //workTokenStack and workPrecStack.  Check if the precedence value is lower than the
   //previous one and take action if it is the case

   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;
      //pop on the next precedece and token
      if (tokStack[tokStackPos].token == TokValue) {
         //must place it on the ansStack and go for the next one
         ansStack[++ansStackPos] = valStack[tokStack[tokStackPos].varListPos];
         continue;
      }
      if (tokStack[tokStackPos].token == Tok0DimVar) {
         ansStack[++ansStackPos] = getValFromVarList();
         continue;
      }
      if (tokStack[tokStackPos].token == TokMDimVarEnd) {
         //need to place the correct array variable onto ansStack.
         //first, check to make sure that the last token on workTokStack is TokMDimVar
         if (workTokStack[tokStackPos-1].token == TokMDimVarAssign) break;
         if (workTokStack[tokStackPos-1].token != TokMDimVar) {
            MsgBox("']' encountered without arrayed variable");
            return moveCursorToError(origStr);
         }
         //now, need to place the appropriate value on the stack
         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;

         //finally,
         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--;   //take the variable off of the workTokStack
         continue;
      }

      ++numTok; ++numPrec;
      workTokStack[numTok] = tokStack[tokStackPos];
      workPrecStack[numPrec] = precStack[tokStackPos];
      if (workTokStack[numTok].token == TokLParen) continue;
      while (workPrecStack[numPrec-1] >= workPrecStack[numPrec]) {
         //pop the precedence stack down
         workPrecStack[numPrec-1] = workPrecStack[numPrec];
         numPrec--;
         switch (workTokStack[numTok-1].token) {
            case TokValue: //place the value on the answer stack
               ansStack[++ansStackPos] =
                     *(double*)varList[workTokStack[numTok-1].varListPos].varAddress;
               numTok++;
               numPrec++;
               tokStackPos++;
               break;
            case TokVarAssign: //place the value in the answer stack into the variable
               if (ansStackPos != 0) {
                  MsgBox("Malformed statement");
                  delete workTokStack;
                  delete workPrecStack;
                  return false;
               }
               *(double*)varList[workTokStack[numTok-1].varListPos].varAddress = ansStack[0];
               //pop the semiColon off the stacks
               numTok=numTok-2;
               numPrec--;
               ansStackPos--;
               goto pETBreakout;
            case TokAdd: //add the last two values in the answer stack
               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:  //it will reach here when the End token is reached
               if (ansStackPos != 0) {
                  MsgBox("Misformed Statement");
                  delete workTokStack;
                  delete workPrecStack;
                  return false;
               }
               ansStackPos--;
               break;
            case TokComma:
               //need to shrink workPrecStack and workTokStack to eliminate the comma.
               //workTokStack is popped down below.  workPrecStack has been popped down
               //up above
               numTok--;
               break;
            case TokLParen:
               //There are only two times this will be entered: 1) there are two or more
               //  left parenthesis on the stack back to back, or 2) a closing parenthesis
               //  and a opening parenthesis are back to back on the workTokStack
               if (tokStack[tokStackPos].token == TokLParen &&
                        tokStack[tokStackPos-1].token == TokLParen) {
                  numPrec++;
                  goto pETBreakout;
               }
               numTok=numTok-2;
               numPrec--;
               goto pETBreakout;
            case TokRParen:
               break;
            case TokMDimVar:
               //it only reaches here when assigning the last value on ansStack to the
               //appropriate location in memory for the variable.
                  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;

                  //finally,
                  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--;   //take the assignment off of the workTokStack
                  continue;

            default:
               MsgBox("Still Programming!");
               delete workTokStack;
               delete workPrecStack;
               return false;
         }
         //pop the workTokStack down
         workTokStack[numTok] = workTokStack[numTok+1];
      } //end of while loop for processing previous tokens.
pETBreakout:
   } //end of do loop
   while (workPrecStack[numPrec] != PrecEnd);
   delete workPrecStack;   workPrecStack = NULL;
   delete workTokStack;      workTokStack = NULL;
   return true;
}

GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 10:14
memberhoney the codewitch13-Sep-19 10:14 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Chris Maunder13-Sep-19 10:26
adminChris Maunder13-Sep-19 10:26 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 10:27
memberhoney the codewitch13-Sep-19 10:27 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 2:31
professionalSander Rossel14-Sep-19 2:31 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 2:42
memberhoney the codewitch14-Sep-19 2:42 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 3:01
professionalSander Rossel14-Sep-19 3:01 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 3:10
memberhoney the codewitch14-Sep-19 3:10 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 3:18
professionalSander Rossel14-Sep-19 3:18 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 3:19
memberhoney the codewitch14-Sep-19 3:19 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
dandy7214-Sep-19 9:06
memberdandy7214-Sep-19 9:06 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Chris Maunder16-Sep-19 6:00
adminChris Maunder16-Sep-19 6:00 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Jörgen Andersson13-Sep-19 12:24
protectorJörgen Andersson13-Sep-19 12:24 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Amarnath S13-Sep-19 15:54
professionalAmarnath S13-Sep-19 15:54 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
GKP199214-Sep-19 2:03
professionalGKP199214-Sep-19 2:03 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 3:11
memberhoney the codewitch14-Sep-19 3:11 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 2:44
professionalSander Rossel14-Sep-19 2:44 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 2:47
memberhoney the codewitch14-Sep-19 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.